Skip to content

Commit e4fdaca

Browse files
committed
Format
1 parent cf89539 commit e4fdaca

File tree

4 files changed

+87
-65
lines changed

4 files changed

+87
-65
lines changed

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

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,82 @@ import kotlin.test.Test
1010

1111
class CrudTest {
1212
@Test
13-
fun includeMetadata() = databaseTest {
14-
database.updateSchema(Schema(Table("lists", listOf(Column.text("name")), includeMetadata = true)))
13+
fun includeMetadata() =
14+
databaseTest {
15+
database.updateSchema(Schema(Table("lists", listOf(Column.text("name")), includeMetadata = true)))
1516

16-
database.execute("INSERT INTO lists (id, name, _metadata) VALUES (uuid(), ?, ?)", listOf("entry", "so meta"))
17-
val batch = database.getNextCrudTransaction()
18-
batch!!.crud[0].metadata shouldBe "so meta"
19-
}
17+
database.execute("INSERT INTO lists (id, name, _metadata) VALUES (uuid(), ?, ?)", listOf("entry", "so meta"))
18+
val batch = database.getNextCrudTransaction()
19+
batch!!.crud[0].metadata shouldBe "so meta"
20+
}
2021

2122
@Test
22-
fun includeOldValues() = databaseTest {
23-
database.updateSchema(Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), includeOld = IncludeOldOptions())))
23+
fun includeOldValues() =
24+
databaseTest {
25+
database.updateSchema(
26+
Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), includeOld = IncludeOldOptions())),
27+
)
2428

25-
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
26-
database.execute("DELETE FROM ps_crud")
27-
database.execute("UPDATE lists SET name = ?", listOf("new name"))
29+
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
30+
database.execute("DELETE FROM ps_crud")
31+
database.execute("UPDATE lists SET name = ?", listOf("new name"))
2832

29-
val batch = database.getNextCrudTransaction()
30-
batch!!.crud[0].oldData shouldBe mapOf("name" to "entry", "content" to "content")
31-
}
33+
val batch = database.getNextCrudTransaction()
34+
batch!!.crud[0].oldData shouldBe mapOf("name" to "entry", "content" to "content")
35+
}
3236

3337
@Test
34-
fun includeOldValuesWithFilter() = databaseTest {
35-
database.updateSchema(Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), includeOld = IncludeOldOptions(columnFilter = listOf("name")))))
38+
fun includeOldValuesWithFilter() =
39+
databaseTest {
40+
database.updateSchema(
41+
Schema(
42+
Table(
43+
"lists",
44+
listOf(Column.text("name"), Column.text("content")),
45+
includeOld = IncludeOldOptions(columnFilter = listOf("name")),
46+
),
47+
),
48+
)
3649

37-
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
38-
database.execute("DELETE FROM ps_crud")
39-
database.execute("UPDATE lists SET name = ?, content = ?", listOf("new name", "new content"))
50+
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
51+
database.execute("DELETE FROM ps_crud")
52+
database.execute("UPDATE lists SET name = ?, content = ?", listOf("new name", "new content"))
4053

41-
val batch = database.getNextCrudTransaction()
42-
batch!!.crud[0].oldData shouldBe mapOf("name" to "entry")
43-
}
54+
val batch = database.getNextCrudTransaction()
55+
batch!!.crud[0].oldData shouldBe mapOf("name" to "entry")
56+
}
4457

4558
@Test
46-
fun includeOldValuesWhenChanged() = databaseTest {
47-
database.updateSchema(Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), includeOld = IncludeOldOptions(onlyWhenChanged = true))))
59+
fun includeOldValuesWhenChanged() =
60+
databaseTest {
61+
database.updateSchema(
62+
Schema(
63+
Table(
64+
"lists",
65+
listOf(Column.text("name"), Column.text("content")),
66+
includeOld = IncludeOldOptions(onlyWhenChanged = true),
67+
),
68+
),
69+
)
4870

49-
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
50-
database.execute("DELETE FROM ps_crud")
51-
database.execute("UPDATE lists SET name = ?", listOf("new name"))
71+
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
72+
database.execute("DELETE FROM ps_crud")
73+
database.execute("UPDATE lists SET name = ?", listOf("new name"))
5274

53-
val batch = database.getNextCrudTransaction()
54-
batch!!.crud[0].oldData shouldBe mapOf("name" to "entry")
55-
}
75+
val batch = database.getNextCrudTransaction()
76+
batch!!.crud[0].oldData shouldBe mapOf("name" to "entry")
77+
}
5678

5779
@Test
58-
fun ignoreEmptyUpdate() = databaseTest {
59-
database.updateSchema(Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), ignoreEmptyUpdate = true)))
80+
fun ignoreEmptyUpdate() =
81+
databaseTest {
82+
database.updateSchema(Schema(Table("lists", listOf(Column.text("name"), Column.text("content")), ignoreEmptyUpdate = true)))
6083

61-
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
62-
database.execute("DELETE FROM ps_crud")
63-
database.execute("UPDATE lists SET name = ?", listOf("entry"))
84+
database.execute("INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?)", listOf("entry", "content"))
85+
database.execute("DELETE FROM ps_crud")
86+
database.execute("UPDATE lists SET name = ?", listOf("entry"))
6487

65-
val batch = database.getNextCrudTransaction()
66-
batch shouldBe null
67-
}
88+
val batch = database.getNextCrudTransaction()
89+
batch shouldBe null
90+
}
6891
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ public data class CrudEntry(
6464
table = data["type"]!!.jsonPrimitive.content,
6565
transactionId = row.txId,
6666
metadata = data["metadata"]?.jsonPrimitive?.content,
67-
oldData = data["old"]?.jsonObject?.mapValues { (_, value) ->
68-
value.jsonPrimitive.contentOrNull
69-
}
67+
oldData =
68+
data["old"]?.jsonObject?.mapValues { (_, value) ->
69+
value.jsonPrimitive.contentOrNull
70+
},
7071
)
7172
}
7273
}

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import kotlinx.serialization.json.JsonElement
77
import kotlinx.serialization.json.JsonPrimitive
88
import kotlinx.serialization.json.buildJsonArray
99

10-
1110
private const val MAX_AMOUNT_OF_COLUMNS = 1999
1211

1312
/**
1413
* A single table in the schema.
1514
*/
16-
public data class Table (
15+
public data class Table(
1716
/**
1817
* The synced table name, matching sync rules.
1918
*/
@@ -115,7 +114,7 @@ public data class Table (
115114
viewNameOverride = viewName,
116115
ignoreEmptyUpdate = ignoreEmptyUpdate,
117116
includeMetadata = includeMetadata,
118-
includeOld = includeOld
117+
includeOld = includeOld,
119118
)
120119
}
121120

@@ -257,31 +256,32 @@ internal data class SerializableTable(
257256
@SerialName("include_old")
258257
val includeOld: JsonElement = JsonPrimitive(false),
259258
@SerialName("include_old_only_when_changed")
260-
val includeOldOnlyWhenChanged: Boolean = false
259+
val includeOldOnlyWhenChanged: Boolean = false,
261260
)
262261

263262
internal fun Table.toSerializable(): SerializableTable =
264263
with(this) {
265264
SerializableTable(
266-
name=name,
267-
columns=columns.map { it.toSerializable() },
268-
indexes=indexes.map { it.toSerializable() },
269-
localOnly=localOnly,
270-
insertOnly=insertOnly,
271-
viewName=viewName,
265+
name = name,
266+
columns = columns.map { it.toSerializable() },
267+
indexes = indexes.map { it.toSerializable() },
268+
localOnly = localOnly,
269+
insertOnly = insertOnly,
270+
viewName = viewName,
272271
ignoreEmptyUpdate = ignoreEmptyUpdate,
273272
includeMetadata = includeMetadata,
274-
includeOld = includeOld?.let {
275-
if (it.columnFilter != null) {
276-
buildJsonArray {
277-
for (column in it.columnFilter) {
278-
add(JsonPrimitive(column))
273+
includeOld =
274+
includeOld?.let {
275+
if (it.columnFilter != null) {
276+
buildJsonArray {
277+
for (column in it.columnFilter) {
278+
add(JsonPrimitive(column))
279+
}
279280
}
281+
} else {
282+
JsonPrimitive(true)
280283
}
281-
} else {
282-
JsonPrimitive(true)
283-
}
284-
} ?: JsonPrimitive(false),
285-
includeOldOnlyWhenChanged = includeOld?.onlyWhenChanged ?: false
284+
} ?: JsonPrimitive(false),
285+
includeOldOnlyWhenChanged = includeOld?.onlyWhenChanged ?: false,
286286
)
287287
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.powersync.db.schema
33
import com.powersync.utils.JsonUtil
44
import io.kotest.assertions.throwables.shouldThrow
55
import io.kotest.matchers.shouldBe
6-
import kotlinx.serialization.json.JsonElement
76
import kotlinx.serialization.json.JsonObject
87
import kotlinx.serialization.json.boolean
98
import kotlinx.serialization.json.encodeToJsonElement
@@ -209,9 +208,8 @@ class TableTest {
209208

210209
@Test
211210
fun handlesOptions() {
212-
fun serialize(table: Table): JsonObject {
213-
return JsonUtil.json.encodeToJsonElement(serializer<SerializableTable>(), table.toSerializable()) as JsonObject
214-
}
211+
fun serialize(table: Table): JsonObject =
212+
JsonUtil.json.encodeToJsonElement(serializer<SerializableTable>(), table.toSerializable()) as JsonObject
215213

216214
serialize(Table("foo", emptyList(), includeMetadata = true))["include_metadata"]!!.jsonPrimitive.boolean shouldBe true
217215
serialize(Table("foo", emptyList(), ignoreEmptyUpdate = true))["ignore_empty_update"]!!.jsonPrimitive.boolean shouldBe true

0 commit comments

Comments
 (0)