Skip to content

Commit

Permalink
fix(drift_dev): Deserialization of view triggers
Browse files Browse the repository at this point in the history
Follow up from #3190. Fixes another spot where either a `DriftTable` or `DriftView` could be stored for a trigger's `on` entity.

Adds tests for view triggers in the `schema/` folder.
  • Loading branch information
dnys1 authored and simolus3 committed Sep 9, 2024
1 parent 5e8b254 commit 3288a18
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions drift_dev/lib/src/services/schema/schema_files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'package:pub_semver/pub_semver.dart';
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart' hide PrimaryKeyColumn;

import '../../analysis/options.dart';
import '../../analysis/resolver/shared/data_class.dart';
import '../../analysis/results/results.dart';
import '../../analysis/options.dart';
import '../../writer/utils/column_constraints.dart';

class _ExportedSchemaVersion {
Expand Down Expand Up @@ -346,7 +346,7 @@ class SchemaReader {
}

DriftTrigger _readTrigger(Map<String, dynamic> content) {
final on = _existingEntity<DriftTable>(content['on']);
final on = _existingEntity<DriftElementWithResultSet>(content['on']);
final name = content['name'] as String;
final sql = content['sql'] as String;

Expand Down
6 changes: 6 additions & 0 deletions drift_dev/test/services/schema/sqlite_to_drift_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ void main() {
..execute('CREATE INDEX my_idx ON foo (bar)')
..execute('CREATE VIEW my_view AS SELECT bar FROM foo')
..execute('CREATE TRIGGER my_trigger AFTER UPDATE ON foo BEGIN '
'UPDATE foo SET bar = old.bar; '
'END;')
..execute(
'CREATE TRIGGER my_view_trigger INSTEAD OF UPDATE ON my_view BEGIN '
'UPDATE foo SET bar = old.bar; '
'END;');
addTearDown(database.dispose);
Expand All @@ -23,6 +27,8 @@ void main() {
isA<DriftView>().having((e) => e.schemaName, 'schemaName', 'my_view'),
isA<DriftTrigger>()
.having((e) => e.schemaName, 'schemaName', 'my_trigger'),
isA<DriftTrigger>()
.having((e) => e.schemaName, 'schemaName', 'my_view_trigger'),
]),
);
});
Expand Down
6 changes: 5 additions & 1 deletion drift_dev/test/services/schema/verifier_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ void main() {
await db.customStatement('CREATE TRIGGER x4 AFTER INSERT ON x1 BEGIN '
'DELETE FROM x1;'
'END;');
await db
.customStatement('CREATE TRIGGER x5 INSTEAD OF INSERT ON x2 BEGIN '
'DELETE FROM x1;'
'END;');

final inputs = await db.collectSchemaInput(const []);
expect(inputs.map((e) => e.name), ['x1', 'x2', 'x3', 'x4']);
expect(inputs.map((e) => e.name), ['x1', 'x2', 'x3', 'x4', 'x5']);
});

test('does not contain shadow tables', () async {
Expand Down
15 changes: 15 additions & 0 deletions drift_dev/test/services/schema/writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ CREATE INDEX groups_name ON "groups"(name, upper(name));
CREATE VIEW my_view WITH MyViewRow AS SELECT id FROM "groups";
CREATE TRIGGER my_view_trigger INSTEAD OF UPDATE ON my_view BEGIN
UPDATE "groups" SET id = old.id;
END;
simple_query: SELECT * FROM my_view; -- not part of the schema
''',
'a|lib/main.dart': '''
Expand Down Expand Up @@ -416,6 +420,17 @@ const expected = r'''
}
]
}
},
{
"id": 7,
"references": [6, 0],
"type": "trigger",
"data": {
"on": 6,
"references_in_body": [6, 0],
"name": "my_view_trigger",
"sql": "CREATE TRIGGER my_view_trigger INSTEAD OF UPDATE ON my_view BEGIN\n UPDATE \"groups\" SET id = old.id;\nEND;"
}
}
]
}
Expand Down

0 comments on commit 3288a18

Please sign in to comment.