Skip to content

Commit

Permalink
Fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjenx committed Jan 31, 2025
1 parent 68ac1c9 commit b9f73d4
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.daemon=true
org.gradle.parallel=true
# Maven
GROUP=com.mercury.sqkon
VERSION_NAME=1.0.0-alpha05
VERSION_NAME=1.0.0-alpha06
POM_NAME=Sqkon
POM_INCEPTION_YEAR=2024
POM_URL=https://github.com/MercuryTechnologies/sqkon/
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
androidx-monitor = "1.7.2"
androidx-runner = "1.6.2"
kotlin = "2.1.0"
kotlin = "2.1.10"
agp = "8.8.0"
kotlinx-coroutines = "1.10.1"
kotlinx-serialization = { require = "1.8.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import app.cash.sqldelight.SuspendingTransacterImpl
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlCursor
import app.cash.sqldelight.db.SqlDriver
import com.mercury.sqkon.db.utils.nowMillis
import kotlinx.coroutines.delay
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
Expand Down Expand Up @@ -36,7 +37,8 @@ class EntityQueries(
bindLong(2, entity.added_at)
bindLong(3, entity.updated_at)
bindLong(4, entity.expires_at)
bindLong(5, entity.write_at)
// While write_at is nullable on the db col, we always set it here (sqlite limitation)
bindLong(5, entity.write_at ?: nowMillis())
bindString(6, entity.value_)
}.await()
notifyQueries(identifier) { emit ->
Expand Down Expand Up @@ -99,7 +101,7 @@ class EntityQueries(
updated_at = cursor.getLong(3)!!,
expires_at = cursor.getLong(4),
read_at = cursor.getLong(5),
write_at = cursor.getLong(6)!!,
write_at = cursor.getLong(6),
value_ = cursor.getString(7)!!,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ data class ResultRow<T : Any>(
addedAt = Instant.fromEpochMilliseconds(entity.added_at),
updatedAt = Instant.fromEpochMilliseconds(entity.updated_at),
expiresAt = entity.expires_at?.let { Instant.fromEpochMilliseconds(it) },
readAt = Clock.System.now(), // By reading this value, we are marking it as read, we just
// update the db async
writeAt = Instant.fromEpochMilliseconds(entity.write_at),
// By reading this value, we are marking it as read, we just update the db async
readAt = Clock.System.now(),
// While write_at col is nullable, we always set it. (Sqlite limitation)
writeAt = Instant.fromEpochMilliseconds(entity.write_at!!),
value = value,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CREATE TABLE entity (
-- UTC timestamp in milliseconds
read_at INTEGER,
-- UTC timestamp in milliseconds
write_at INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
write_at INTEGER,
PRIMARY KEY (entity_name, entity_key)
);

Expand Down
4 changes: 3 additions & 1 deletion library/src/commonMain/sqldelight/migrations/1.sqm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ CREATE TABLE metadata (
);

ALTER TABLE entity ADD COLUMN read_at INTEGER;
ALTER TABLE entity ADD COLUMN write_at INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE entity ADD COLUMN write_at INTEGER DEFAULT NULL;

UPDATE entity SET write_at = CURRENT_TIMESTAMP;

-- Index read_at
CREATE INDEX idx_entity_read_at ON entity (read_at);
Expand Down

0 comments on commit b9f73d4

Please sign in to comment.