diff --git a/packages/powersync/lib/src/schema.dart b/packages/powersync/lib/src/schema.dart index 92abee97..efa87abc 100644 --- a/packages/powersync/lib/src/schema.dart +++ b/packages/powersync/lib/src/schema.dart @@ -13,8 +13,15 @@ class Schema { Map toJson() => {'tables': tables}; void validate() { + Set tableNames = {}; for (var table in tables) { table.validate(); + + if (tableNames.contains(table.name)) { + throw AssertionError("Duplicate table name: ${table.name}"); + } + + tableNames.add(table.name); } } } diff --git a/packages/powersync/test/schema_test.dart b/packages/powersync/test/schema_test.dart index cfa2396d..7dfda31c 100644 --- a/packages/powersync/test/schema_test.dart +++ b/packages/powersync/test/schema_test.dart @@ -321,6 +321,41 @@ void main() { ); }); + test('Schema without duplicate table names', () { + final schema = Schema([ + Table('duplicate', [ + Column.text('name'), + ]), + Table('not_duplicate', [ + Column.text('name'), + ]), + ]); + + expect(() => schema.validate(), returnsNormally); + }); + + test('Schema with duplicate table names', () { + final schema = Schema([ + Table('clone', [ + Column.text('name'), + ]), + Table('clone', [ + Column.text('name'), + ]), + ]); + + expect( + () => schema.validate(), + throwsA( + isA().having( + (e) => e.message, + 'message', + 'Duplicate table name: clone', + ), + ), + ); + }); + test('toJson method', () { final table = Table('users', [ Column('name', ColumnType.text),