From 1c068734032692eab521072a95e0b3b512b5cb7f Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 10 Dec 2022 22:57:03 +0100 Subject: [PATCH] Fix reading triggers in old schema files (#2191) --- .../lib/src/services/schema/schema_files.dart | 8 +++- .../test/generated_migrations/schema_v1.dart | 32 ++++++++------ .../test/generated_migrations/schema_v2.dart | 32 ++++++++------ .../test/generated_migrations/schema_v3.dart | 43 +++++++++++++------ 4 files changed, 74 insertions(+), 41 deletions(-) diff --git a/drift_dev/lib/src/services/schema/schema_files.dart b/drift_dev/lib/src/services/schema/schema_files.dart index be892f86..9bbce728 100644 --- a/drift_dev/lib/src/services/schema/schema_files.dart +++ b/drift_dev/lib/src/services/schema/schema_files.dart @@ -282,14 +282,18 @@ class SchemaReader { final name = content['name'] as String; final sql = content['sql'] as String; + // Old versions of this file used to have a typo when serializing body + // references. + final bodyReferences = + (content['references_in_body'] ?? content['refences_in_body']) as List; + return DriftTrigger( _id(name), _declaration, on: on, onWrite: UpdateKind.delete, references: [ - for (final bodyRef in content['references_in_body'] as List) - _existingEntity(bodyRef) + for (final bodyRef in bodyReferences) _existingEntity(bodyRef) ], createStmt: sql, writes: const [], diff --git a/examples/app/test/generated_migrations/schema_v1.dart b/examples/app/test/generated_migrations/schema_v1.dart index 74a808ad..79142aae 100644 --- a/examples/app/test/generated_migrations/schema_v1.dart +++ b/examples/app/test/generated_migrations/schema_v1.dart @@ -9,9 +9,11 @@ class Categories extends Table with TableInfo { Categories(this.attachedDatabase, [this._alias]); late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, + hasAutoIncrement: true, type: DriftSqlType.int, requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + defaultConstraints: + GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); late final GeneratedColumn name = GeneratedColumn( 'name', aliasedName, false, type: DriftSqlType.string, requiredDuringInsert: true); @@ -35,9 +37,6 @@ class Categories extends Table with TableInfo { Categories createAlias(String alias) { return Categories(attachedDatabase, alias); } - - @override - bool get dontWriteConstraints => false; } class TodoEntries extends Table with TableInfo { @@ -47,9 +46,11 @@ class TodoEntries extends Table with TableInfo { TodoEntries(this.attachedDatabase, [this._alias]); late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, + hasAutoIncrement: true, type: DriftSqlType.int, requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + defaultConstraints: + GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); late final GeneratedColumn description = GeneratedColumn( 'description', aliasedName, false, type: DriftSqlType.string, requiredDuringInsert: true); @@ -73,9 +74,6 @@ class TodoEntries extends Table with TableInfo { TodoEntries createAlias(String alias) { return TodoEntries(attachedDatabase, alias); } - - @override - bool get dontWriteConstraints => false; } class TextEntries extends Table with TableInfo, VirtualTableInfo { @@ -95,7 +93,7 @@ class TextEntries extends Table with TableInfo, VirtualTableInfo { @override String get actualTableName => 'text_entries'; @override - Set get $primaryKey => {}; + Set get $primaryKey => const {}; @override Never map(Map data, {String? tablePrefix}) { throw UnsupportedError('TableInfo.map in schema verification code'); @@ -106,8 +104,6 @@ class TextEntries extends Table with TableInfo, VirtualTableInfo { return TextEntries(attachedDatabase, alias); } - @override - bool get dontWriteConstraints => true; @override String get moduleAndArgs => 'fts5(description, content=todo_entries, content_rowid=id)'; @@ -120,14 +116,24 @@ class DatabaseAtV1 extends GeneratedDatabase { late final TodoEntries todoEntries = TodoEntries(this); late final TextEntries textEntries = TextEntries(this); late final Trigger todosInsert = Trigger( - 'CREATE TRIGGER todos_insert AFTER INSERT ON todo_entries BEGIN\n INSERT INTO text_entries(rowid, description) VALUES (new.id, new.description);\nEND;', + 'CREATE TRIGGER todos_insert AFTER INSERT ON todo_entries BEGIN INSERT INTO text_entries ("rowid", description) VALUES (new.id, new.description);END', 'todos_insert'); @override - Iterable> get allTables => + Iterable> get allTables => allSchemaEntities.whereType>(); @override List get allSchemaEntities => [categories, todoEntries, textEntries, todosInsert]; @override + StreamQueryUpdateRules get streamUpdateRules => const StreamQueryUpdateRules( + [ + WritePropagation( + on: TableUpdateQuery.onTableName('todo_entries', + limitUpdateKind: UpdateKind.delete), + result: [], + ), + ], + ); + @override int get schemaVersion => 1; } diff --git a/examples/app/test/generated_migrations/schema_v2.dart b/examples/app/test/generated_migrations/schema_v2.dart index 9e1c699b..9b7252a7 100644 --- a/examples/app/test/generated_migrations/schema_v2.dart +++ b/examples/app/test/generated_migrations/schema_v2.dart @@ -9,9 +9,11 @@ class Categories extends Table with TableInfo { Categories(this.attachedDatabase, [this._alias]); late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, + hasAutoIncrement: true, type: DriftSqlType.int, requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + defaultConstraints: + GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); late final GeneratedColumn name = GeneratedColumn( 'name', aliasedName, false, type: DriftSqlType.string, requiredDuringInsert: true); @@ -35,9 +37,6 @@ class Categories extends Table with TableInfo { Categories createAlias(String alias) { return Categories(attachedDatabase, alias); } - - @override - bool get dontWriteConstraints => false; } class TodoEntries extends Table with TableInfo { @@ -47,9 +46,11 @@ class TodoEntries extends Table with TableInfo { TodoEntries(this.attachedDatabase, [this._alias]); late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, + hasAutoIncrement: true, type: DriftSqlType.int, requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + defaultConstraints: + GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); late final GeneratedColumn description = GeneratedColumn( 'description', aliasedName, false, type: DriftSqlType.string, requiredDuringInsert: true); @@ -76,9 +77,6 @@ class TodoEntries extends Table with TableInfo { TodoEntries createAlias(String alias) { return TodoEntries(attachedDatabase, alias); } - - @override - bool get dontWriteConstraints => false; } class TextEntries extends Table with TableInfo, VirtualTableInfo { @@ -98,7 +96,7 @@ class TextEntries extends Table with TableInfo, VirtualTableInfo { @override String get actualTableName => 'text_entries'; @override - Set get $primaryKey => {}; + Set get $primaryKey => const {}; @override Never map(Map data, {String? tablePrefix}) { throw UnsupportedError('TableInfo.map in schema verification code'); @@ -109,8 +107,6 @@ class TextEntries extends Table with TableInfo, VirtualTableInfo { return TextEntries(attachedDatabase, alias); } - @override - bool get dontWriteConstraints => true; @override String get moduleAndArgs => 'fts5(description, content=todo_entries, content_rowid=id)'; @@ -123,14 +119,24 @@ class DatabaseAtV2 extends GeneratedDatabase { late final TodoEntries todoEntries = TodoEntries(this); late final TextEntries textEntries = TextEntries(this); late final Trigger todosInsert = Trigger( - 'CREATE TRIGGER todos_insert AFTER INSERT ON todo_entries BEGIN\n INSERT INTO text_entries(rowid, description) VALUES (new.id, new.description);\nEND;', + 'CREATE TRIGGER todos_insert AFTER INSERT ON todo_entries BEGIN INSERT INTO text_entries ("rowid", description) VALUES (new.id, new.description);END', 'todos_insert'); @override - Iterable> get allTables => + Iterable> get allTables => allSchemaEntities.whereType>(); @override List get allSchemaEntities => [categories, todoEntries, textEntries, todosInsert]; @override + StreamQueryUpdateRules get streamUpdateRules => const StreamQueryUpdateRules( + [ + WritePropagation( + on: TableUpdateQuery.onTableName('todo_entries', + limitUpdateKind: UpdateKind.delete), + result: [], + ), + ], + ); + @override int get schemaVersion => 2; } diff --git a/examples/app/test/generated_migrations/schema_v3.dart b/examples/app/test/generated_migrations/schema_v3.dart index 9b98a77d..277ead33 100644 --- a/examples/app/test/generated_migrations/schema_v3.dart +++ b/examples/app/test/generated_migrations/schema_v3.dart @@ -9,9 +9,11 @@ class Categories extends Table with TableInfo { Categories(this.attachedDatabase, [this._alias]); late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, + hasAutoIncrement: true, type: DriftSqlType.int, requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + defaultConstraints: + GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); late final GeneratedColumn name = GeneratedColumn( 'name', aliasedName, false, type: DriftSqlType.string, requiredDuringInsert: true); @@ -35,9 +37,6 @@ class Categories extends Table with TableInfo { Categories createAlias(String alias) { return Categories(attachedDatabase, alias); } - - @override - bool get dontWriteConstraints => false; } class TodoEntries extends Table with TableInfo { @@ -47,9 +46,11 @@ class TodoEntries extends Table with TableInfo { TodoEntries(this.attachedDatabase, [this._alias]); late final GeneratedColumn id = GeneratedColumn( 'id', aliasedName, false, + hasAutoIncrement: true, type: DriftSqlType.int, requiredDuringInsert: false, - defaultConstraints: 'PRIMARY KEY AUTOINCREMENT'); + defaultConstraints: + GeneratedColumn.constraintIsAlways('PRIMARY KEY AUTOINCREMENT')); late final GeneratedColumn description = GeneratedColumn( 'description', aliasedName, false, type: DriftSqlType.string, requiredDuringInsert: true); @@ -57,7 +58,8 @@ class TodoEntries extends Table with TableInfo { 'category', aliasedName, true, type: DriftSqlType.int, requiredDuringInsert: false, - defaultConstraints: 'REFERENCES categories (id)'); + defaultConstraints: + GeneratedColumn.constraintIsAlways('REFERENCES categories (id)')); late final GeneratedColumn dueDate = GeneratedColumn( 'due_date', aliasedName, true, type: DriftSqlType.dateTime, requiredDuringInsert: false); @@ -78,9 +80,6 @@ class TodoEntries extends Table with TableInfo { TodoEntries createAlias(String alias) { return TodoEntries(attachedDatabase, alias); } - - @override - bool get dontWriteConstraints => false; } class TextEntries extends Table with TableInfo, VirtualTableInfo { @@ -100,7 +99,7 @@ class TextEntries extends Table with TableInfo, VirtualTableInfo { @override String get actualTableName => 'text_entries'; @override - Set get $primaryKey => {}; + Set get $primaryKey => const {}; @override Never map(Map data, {String? tablePrefix}) { throw UnsupportedError('TableInfo.map in schema verification code'); @@ -111,8 +110,6 @@ class TextEntries extends Table with TableInfo, VirtualTableInfo { return TextEntries(attachedDatabase, alias); } - @override - bool get dontWriteConstraints => true; @override String get moduleAndArgs => 'fts5(description, content=todo_entries, content_rowid=id)'; @@ -134,7 +131,7 @@ class DatabaseAtV3 extends GeneratedDatabase { 'CREATE TRIGGER todos_update AFTER UPDATE ON todo_entries BEGIN INSERT INTO text_entries (text_entries, "rowid", description) VALUES (\'delete\', new.id, new.description);INSERT INTO text_entries ("rowid", description) VALUES (new.id, new.description);END', 'todos_update'); @override - Iterable> get allTables => + Iterable> get allTables => allSchemaEntities.whereType>(); @override List get allSchemaEntities => [ @@ -146,5 +143,25 @@ class DatabaseAtV3 extends GeneratedDatabase { todosUpdate ]; @override + StreamQueryUpdateRules get streamUpdateRules => const StreamQueryUpdateRules( + [ + WritePropagation( + on: TableUpdateQuery.onTableName('todo_entries', + limitUpdateKind: UpdateKind.delete), + result: [], + ), + WritePropagation( + on: TableUpdateQuery.onTableName('todo_entries', + limitUpdateKind: UpdateKind.delete), + result: [], + ), + WritePropagation( + on: TableUpdateQuery.onTableName('todo_entries', + limitUpdateKind: UpdateKind.delete), + result: [], + ), + ], + ); + @override int get schemaVersion => 3; }