Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions core/src/commonMain/kotlin/com/powersync/db/schema/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ import kotlinx.serialization.Serializable

@Serializable
public data class Schema(val tables: List<Table>) {
init {
validate()
}

public constructor(vararg tables: Table) : this(tables.asList())


public fun validate() {
val tableNames = mutableSetOf<String>()
tables.forEach { table ->
if (!tableNames.add(table.name)) {
throw AssertionError("Duplicate table name: ${table.name}")
}
table.validate()
}
}
}
40 changes: 40 additions & 0 deletions core/src/commonTest/kotlin/com/powersync/db/schema/SchemaTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.powersync.db.schema

import kotlin.test.*

class SchemaTest {

@Test
fun schemaConstructionWithValidTablesShouldSucceed() {
val table1 = Table("table1", listOf(Column("column1", ColumnType.TEXT)))
val table2 = Table("table2", listOf(Column("column2", ColumnType.INTEGER)))

val schema = Schema(table1, table2)

assertEquals(2, schema.tables.size)
assertEquals("table1", schema.tables[0].name)
assertEquals("table2", schema.tables[1].name)
}

@Test
fun schemaConstructionWithInvalidTableShouldThrowException() {
val validTable = Table("validTable", listOf(Column("column1", ColumnType.TEXT)))
val invalidTable = Table("#invalid-table", listOf(Column("column1", ColumnType.TEXT)))

val exception = assertFailsWith<AssertionError> {
Schema(validTable, invalidTable)
}
assertEquals(exception.message, "Invalid characters in table name: #invalid-table")
}

@Test
fun schemaShouldDetectDuplicateTableNames() {
val table1 = Table("table1", listOf(Column("column1", ColumnType.TEXT)))
val table2 = Table("table1", listOf(Column("column2", ColumnType.INTEGER)))

val exception = assertFailsWith<AssertionError> {
Schema(table1, table2)
}
assertEquals(exception.message, "Duplicate table name: table1")
}
}
13 changes: 13 additions & 0 deletions core/src/commonTest/kotlin/com/powersync/db/schema/TableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,17 @@ class TableTest {

assertEquals(exception.message,"Duplicate index users.name_index")
}


@Test
fun testValidationOfIdColumn() {
val columns = listOf(Column("id", ColumnType.TEXT))
val table = Table("users", columns)

val exception = assertFailsWith<AssertionError> {
table.validate()
}

assertEquals(exception.message,"users: id column is automatically added, custom id columns are not supported")
}
}
Loading