Skip to content

Commit ffdba9a

Browse files
DominicGBauerDominicGBauer
andauthored
fix: schema not running table validation (#52)
Co-authored-by: DominicGBauer <[email protected]>
1 parent 416f54f commit ffdba9a

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,20 @@ import kotlinx.serialization.Serializable
44

55
@Serializable
66
public data class Schema(val tables: List<Table>) {
7+
init {
8+
validate()
9+
}
10+
711
public constructor(vararg tables: Table) : this(tables.asList())
12+
13+
14+
public fun validate() {
15+
val tableNames = mutableSetOf<String>()
16+
tables.forEach { table ->
17+
if (!tableNames.add(table.name)) {
18+
throw AssertionError("Duplicate table name: ${table.name}")
19+
}
20+
table.validate()
21+
}
22+
}
823
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.powersync.db.schema
2+
3+
import kotlin.test.*
4+
5+
class SchemaTest {
6+
7+
@Test
8+
fun schemaConstructionWithValidTablesShouldSucceed() {
9+
val table1 = Table("table1", listOf(Column("column1", ColumnType.TEXT)))
10+
val table2 = Table("table2", listOf(Column("column2", ColumnType.INTEGER)))
11+
12+
val schema = Schema(table1, table2)
13+
14+
assertEquals(2, schema.tables.size)
15+
assertEquals("table1", schema.tables[0].name)
16+
assertEquals("table2", schema.tables[1].name)
17+
}
18+
19+
@Test
20+
fun schemaConstructionWithInvalidTableShouldThrowException() {
21+
val validTable = Table("validTable", listOf(Column("column1", ColumnType.TEXT)))
22+
val invalidTable = Table("#invalid-table", listOf(Column("column1", ColumnType.TEXT)))
23+
24+
val exception = assertFailsWith<AssertionError> {
25+
Schema(validTable, invalidTable)
26+
}
27+
assertEquals(exception.message, "Invalid characters in table name: #invalid-table")
28+
}
29+
30+
@Test
31+
fun schemaShouldDetectDuplicateTableNames() {
32+
val table1 = Table("table1", listOf(Column("column1", ColumnType.TEXT)))
33+
val table2 = Table("table1", listOf(Column("column2", ColumnType.INTEGER)))
34+
35+
val exception = assertFailsWith<AssertionError> {
36+
Schema(table1, table2)
37+
}
38+
assertEquals(exception.message, "Duplicate table name: table1")
39+
}
40+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,17 @@ class TableTest {
154154

155155
assertEquals(exception.message,"Duplicate index users.name_index")
156156
}
157+
158+
159+
@Test
160+
fun testValidationOfIdColumn() {
161+
val columns = listOf(Column("id", ColumnType.TEXT))
162+
val table = Table("users", columns)
163+
164+
val exception = assertFailsWith<AssertionError> {
165+
table.validate()
166+
}
167+
168+
assertEquals(exception.message,"users: id column is automatically added, custom id columns are not supported")
169+
}
157170
}

0 commit comments

Comments
 (0)