Skip to content

Commit 47f7188

Browse files
authored
fix: Validate duplicate table names (#192)
1 parent d83755a commit 47f7188

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

packages/powersync/lib/src/schema.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ class Schema {
1313
Map<String, dynamic> toJson() => {'tables': tables};
1414

1515
void validate() {
16+
Set<String> tableNames = {};
1617
for (var table in tables) {
1718
table.validate();
19+
20+
if (tableNames.contains(table.name)) {
21+
throw AssertionError("Duplicate table name: ${table.name}");
22+
}
23+
24+
tableNames.add(table.name);
1825
}
1926
}
2027
}

packages/powersync/test/schema_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,41 @@ void main() {
321321
);
322322
});
323323

324+
test('Schema without duplicate table names', () {
325+
final schema = Schema([
326+
Table('duplicate', [
327+
Column.text('name'),
328+
]),
329+
Table('not_duplicate', [
330+
Column.text('name'),
331+
]),
332+
]);
333+
334+
expect(() => schema.validate(), returnsNormally);
335+
});
336+
337+
test('Schema with duplicate table names', () {
338+
final schema = Schema([
339+
Table('clone', [
340+
Column.text('name'),
341+
]),
342+
Table('clone', [
343+
Column.text('name'),
344+
]),
345+
]);
346+
347+
expect(
348+
() => schema.validate(),
349+
throwsA(
350+
isA<AssertionError>().having(
351+
(e) => e.message,
352+
'message',
353+
'Duplicate table name: clone',
354+
),
355+
),
356+
);
357+
});
358+
324359
test('toJson method', () {
325360
final table = Table('users', [
326361
Column('name', ColumnType.text),

0 commit comments

Comments
 (0)