Skip to content

Commit

Permalink
fix: Validate duplicate table names (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
mugikhan authored Oct 16, 2024
1 parent d83755a commit 47f7188
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/powersync/lib/src/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ class Schema {
Map<String, dynamic> toJson() => {'tables': tables};

void validate() {
Set<String> tableNames = {};
for (var table in tables) {
table.validate();

if (tableNames.contains(table.name)) {
throw AssertionError("Duplicate table name: ${table.name}");
}

tableNames.add(table.name);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions packages/powersync/test/schema_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssertionError>().having(
(e) => e.message,
'message',
'Duplicate table name: clone',
),
),
);
});

test('toJson method', () {
final table = Table('users', [
Column('name', ColumnType.text),
Expand Down

0 comments on commit 47f7188

Please sign in to comment.