Skip to content

Commit 30259e7

Browse files
committed
Rename options
1 parent 5acd01c commit 30259e7

File tree

4 files changed

+48
-30
lines changed

4 files changed

+48
-30
lines changed

core/src/commonIntegrationTest/kotlin/com/powersync/CrudTest.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.powersync
22

33
import com.powersync.db.schema.Column
4-
import com.powersync.db.schema.IncludeOldOptions
54
import com.powersync.db.schema.Schema
65
import com.powersync.db.schema.Table
6+
import com.powersync.db.schema.TrackPreviousValuesOptions
77
import com.powersync.testutils.databaseTest
88
import io.kotest.matchers.shouldBe
99
import kotlin.test.Test
@@ -12,7 +12,7 @@ class CrudTest {
1212
@Test
1313
fun includeMetadata() =
1414
databaseTest {
15-
database.updateSchema(Schema(Table("lists", listOf(Column.text("name")), includeMetadata = true)))
15+
database.updateSchema(Schema(Table("lists", listOf(Column.text("name")), trackMetadata = true)))
1616

1717
database.execute("INSERT INTO lists (id, name, _metadata) VALUES (uuid(), ?, ?)", listOf("entry", "so meta"))
1818
val batch = database.getNextCrudTransaction()
@@ -23,7 +23,9 @@ class CrudTest {
2323
fun includeOldValues() =
2424
databaseTest {
2525
database.updateSchema(
26-
Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), includeOld = IncludeOldOptions())),
26+
Schema(
27+
Table("lists", listOf(Column.text("name"), Column.text("content")), trackPreviousValues = TrackPreviousValuesOptions()),
28+
),
2729
)
2830

2931
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
@@ -42,7 +44,7 @@ class CrudTest {
4244
Table(
4345
"lists",
4446
listOf(Column.text("name"), Column.text("content")),
45-
includeOld = IncludeOldOptions(columnFilter = listOf("name")),
47+
trackPreviousValues = TrackPreviousValuesOptions(columnFilter = listOf("name")),
4648
),
4749
),
4850
)
@@ -63,7 +65,7 @@ class CrudTest {
6365
Table(
6466
"lists",
6567
listOf(Column.text("name"), Column.text("content")),
66-
includeOld = IncludeOldOptions(onlyWhenChanged = true),
68+
trackPreviousValues = TrackPreviousValuesOptions(onlyWhenChanged = true),
6769
),
6870
),
6971
)
@@ -79,7 +81,7 @@ class CrudTest {
7981
@Test
8082
fun ignoreEmptyUpdate() =
8183
databaseTest {
82-
database.updateSchema(Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), ignoreEmptyUpdate = true)))
84+
database.updateSchema(Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), ignoreEmptyUpdates = true)))
8385

8486
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
8587
database.execute("DELETE FROM ps_crud")

core/src/commonMain/kotlin/com/powersync/db/crud/CrudEntry.kt

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.powersync.db.crud
22

3+
import com.powersync.PowerSyncDatabase
4+
import com.powersync.db.schema.Table
35
import com.powersync.utils.JsonUtil
46
import kotlinx.serialization.json.contentOrNull
57
import kotlinx.serialization.json.jsonObject
@@ -37,6 +39,14 @@ public data class CrudEntry(
3739
* This may change in the future.
3840
*/
3941
val transactionId: Int?,
42+
/**
43+
* User-defined metadata that can be attached to writes.
44+
*
45+
* This is the value the `_metadata` column had when the write to the database was made,
46+
* allowing backend connectors to e.g. identify a write and treat it specially.
47+
*
48+
* Note that the `_metadata` column is only available when [Table.trackMetadata] is enabled.
49+
*/
4050
val metadata: String? = null,
4151
/**
4252
* Data associated with the change.
@@ -48,6 +58,12 @@ public data class CrudEntry(
4858
* For DELETE, this is null.
4959
*/
5060
val opData: Map<String, String?>?,
61+
/**
62+
* Previous values before this change.
63+
*
64+
* These values can be tracked for `UPDATE` statements when [Table.trackPreviousValues] is
65+
* enabled.
66+
*/
5167
val oldData: Map<String, String?>? = null,
5268
) {
5369
public companion object {

core/src/commonMain/kotlin/com/powersync/db/schema/Table.kt

+17-17
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ public data class Table(
4141
* Whether to add a hidden `_metadata` column that will be enabled for updates to attach custom
4242
* information about writes that will be reported through [CrudEntry.metadata].
4343
*/
44-
val includeMetadata: Boolean = false,
44+
val trackMetadata: Boolean = false,
4545
/**
4646
* When set to a non-null value, track old values of columns for [CrudEntry.oldData].
4747
*
48-
* See [IncludeOldOptions] for details.
48+
* See [TrackPreviousValuesOptions] for details.
4949
*/
50-
val includeOld: IncludeOldOptions? = null,
50+
val trackPreviousValues: TrackPreviousValuesOptions? = null,
5151
/**
5252
* Whether an `UPDATE` statement that doesn't change any values should be ignored when creating
5353
* CRUD entries.
5454
*/
55-
val ignoreEmptyUpdate: Boolean = false,
55+
val ignoreEmptyUpdates: Boolean = false,
5656
) {
5757
init {
5858
/**
@@ -101,9 +101,9 @@ public data class Table(
101101
name: String,
102102
columns: List<Column>,
103103
viewName: String? = null,
104-
ignoreEmptyUpdate: Boolean = false,
105-
includeMetadata: Boolean = false,
106-
includeOld: IncludeOldOptions? = null,
104+
ignoreEmptyUpdates: Boolean = false,
105+
trackMetadata: Boolean = false,
106+
trackPreviousValues: TrackPreviousValuesOptions? = null,
107107
): Table =
108108
Table(
109109
name,
@@ -112,9 +112,9 @@ public data class Table(
112112
localOnly = false,
113113
insertOnly = true,
114114
viewNameOverride = viewName,
115-
ignoreEmptyUpdate = ignoreEmptyUpdate,
116-
includeMetadata = includeMetadata,
117-
includeOld = includeOld,
115+
ignoreEmptyUpdates = ignoreEmptyUpdates,
116+
trackMetadata = trackMetadata,
117+
trackPreviousValues = trackPreviousValues,
118118
)
119119
}
120120

@@ -161,10 +161,10 @@ public data class Table(
161161
throw AssertionError("Invalid characters in view name: $viewNameOverride")
162162
}
163163

164-
check(!localOnly || !includeMetadata) {
164+
check(!localOnly || !trackMetadata) {
165165
"Can't track metadata for local-only tables."
166166
}
167-
check(!localOnly || includeOld == null) {
167+
check(!localOnly || trackPreviousValues == null) {
168168
"Can't track old values for local-only tables."
169169
}
170170

@@ -223,7 +223,7 @@ public data class Table(
223223
*
224224
* These options are enabled by passing them to a non-local [Table] constructor.
225225
*/
226-
public data class IncludeOldOptions(
226+
public data class TrackPreviousValuesOptions(
227227
/**
228228
* A filter of column names for which updates should be tracked.
229229
*
@@ -268,10 +268,10 @@ internal fun Table.toSerializable(): SerializableTable =
268268
localOnly = localOnly,
269269
insertOnly = insertOnly,
270270
viewName = viewName,
271-
ignoreEmptyUpdate = ignoreEmptyUpdate,
272-
includeMetadata = includeMetadata,
271+
ignoreEmptyUpdate = ignoreEmptyUpdates,
272+
includeMetadata = trackMetadata,
273273
includeOld =
274-
includeOld?.let {
274+
trackPreviousValues?.let {
275275
if (it.columnFilter != null) {
276276
buildJsonArray {
277277
for (column in it.columnFilter) {
@@ -282,6 +282,6 @@ internal fun Table.toSerializable(): SerializableTable =
282282
JsonPrimitive(true)
283283
}
284284
} ?: JsonPrimitive(false),
285-
includeOldOnlyWhenChanged = includeOld?.onlyWhenChanged ?: false,
285+
includeOldOnlyWhenChanged = trackPreviousValues?.onlyWhenChanged ?: false,
286286
)
287287
}

core/src/commonTest/kotlin/com/powersync/db/schema/TableTest.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ class TableTest {
192192

193193
@Test
194194
fun testValidationLocalOnlyWithMetadata() {
195-
val table = Table("foo", listOf(Column.text("bar")), localOnly = true, includeMetadata = true)
195+
val table = Table("foo", listOf(Column.text("bar")), localOnly = true, trackMetadata = true)
196196

197197
val exception = shouldThrow<IllegalStateException> { table.validate() }
198198
exception.message shouldBe "Can't track metadata for local-only tables."
199199
}
200200

201201
@Test
202202
fun testValidationLocalOnlyWithIncludeOld() {
203-
val table = Table("foo", listOf(Column.text("bar")), localOnly = true, includeOld = IncludeOldOptions())
203+
val table = Table("foo", listOf(Column.text("bar")), localOnly = true, trackPreviousValues = TrackPreviousValuesOptions())
204204

205205
val exception = shouldThrow<IllegalStateException> { table.validate() }
206206
exception.message shouldBe "Can't track old values for local-only tables."
@@ -211,20 +211,20 @@ class TableTest {
211211
fun serialize(table: Table): JsonObject =
212212
JsonUtil.json.encodeToJsonElement(serializer<SerializableTable>(), table.toSerializable()) as JsonObject
213213

214-
serialize(Table("foo", emptyList(), includeMetadata = true))["include_metadata"]!!.jsonPrimitive.boolean shouldBe true
215-
serialize(Table("foo", emptyList(), ignoreEmptyUpdate = true))["ignore_empty_update"]!!.jsonPrimitive.boolean shouldBe true
214+
serialize(Table("foo", emptyList(), trackMetadata = true))["include_metadata"]!!.jsonPrimitive.boolean shouldBe true
215+
serialize(Table("foo", emptyList(), ignoreEmptyUpdates = true))["ignore_empty_update"]!!.jsonPrimitive.boolean shouldBe true
216216

217-
serialize(Table("foo", emptyList(), includeOld = IncludeOldOptions())).let {
217+
serialize(Table("foo", emptyList(), trackPreviousValues = TrackPreviousValuesOptions())).let {
218218
it["include_old"]!!.jsonPrimitive.boolean shouldBe true
219219
it["include_old_only_when_changed"]!!.jsonPrimitive.boolean shouldBe false
220220
}
221221

222-
serialize(Table("foo", emptyList(), includeOld = IncludeOldOptions(columnFilter = listOf("foo", "bar")))).let {
222+
serialize(Table("foo", emptyList(), trackPreviousValues = TrackPreviousValuesOptions(columnFilter = listOf("foo", "bar")))).let {
223223
it["include_old"]!!.jsonArray.map { e -> e.jsonPrimitive.content } shouldBe listOf("foo", "bar")
224224
it["include_old_only_when_changed"]!!.jsonPrimitive.boolean shouldBe false
225225
}
226226

227-
serialize(Table("foo", emptyList(), includeOld = IncludeOldOptions(onlyWhenChanged = true))).let {
227+
serialize(Table("foo", emptyList(), trackPreviousValues = TrackPreviousValuesOptions(onlyWhenChanged = true))).let {
228228
it["include_old"]!!.jsonPrimitive.boolean shouldBe true
229229
it["include_old_only_when_changed"]!!.jsonPrimitive.boolean shouldBe true
230230
}

0 commit comments

Comments
 (0)