diff --git a/moor/lib/src/runtime/migration.dart b/moor/lib/src/runtime/migration.dart index 9bd7b4f5..02179eb1 100644 --- a/moor/lib/src/runtime/migration.dart +++ b/moor/lib/src/runtime/migration.dart @@ -21,7 +21,7 @@ typedef Future OnMigrationFinished(); /// populate initial data or issue `PRAGMA` statements that you want to use. typedef OnBeforeOpen = Future Function(OpeningDetails details); -Future _defaultOnCreate(Migrator m) => m.createAllTables(); +Future _defaultOnCreate(Migrator m) => m.createAll(); Future _defaultOnUpdate(Migrator m, int from, int to) async => throw Exception("You've bumped the schema version for your moor database " "but didn't provide a strategy for schema updates. Please do that by " @@ -64,12 +64,27 @@ class Migrator { Migrator(this._db, this._executor); /// Creates all tables specified for the database, if they don't exist + @Deprecated('Use createAll() instead') Future createAllTables() async { for (var table in _db.allTables) { await createTable(table); } } + /// Creates all tables, triggers, views, indexes and everything else defined + /// in the database, if they don't exist. + Future createAll() async { + for (var entity in _db.allEntities) { + if (entity is TableInfo) { + await createTable(entity); + } else if (entity is Trigger) { + await createTrigger(entity); + } else { + throw AssertionError('Unknown entity: $entity'); + } + } + } + GenerationContext _createContext() { return GenerationContext( _db.typeSystem, @@ -134,6 +149,11 @@ class Migrator { return issueCustomQuery(context.sql, context.boundVariables); } + /// Executes the `CREATE TRIGGER` statement that created the [trigger]. + Future createTrigger(Trigger trigger) { + return issueCustomQuery(trigger.createTriggerStmt); + } + /// Deletes the table with the given name. Note that this function does not /// escape the [name] parameter. Future deleteTable(String name) async { diff --git a/moor/test/parsed_sql/moor_files_integration_test.dart b/moor/test/parsed_sql/moor_files_integration_test.dart index 18378727..69fbb870 100644 --- a/moor/test/parsed_sql/moor_files_integration_test.dart +++ b/moor/test/parsed_sql/moor_files_integration_test.dart @@ -25,19 +25,26 @@ const _createMyTable = 'CREATE TABLE IF NOT EXISTS mytable (' 'somebool INTEGER, ' 'somedate INTEGER);'; +const _createMyTrigger = ''' +CREATE TRIGGER my_trigger AFTER INSERT ON config BEGIN + INSERT INTO with_defaults VALUES (new.config_key, LENGTH(new.config_value)); +END;'''; + void main() { // see ../data/tables/tables.moor - test('creates tables as specified in .moor files', () async { + test('creates entities as specified in .moor files', () async { final mockExecutor = MockExecutor(); + final mockQueryExecutor = MockQueryExecutor(); final db = CustomTablesDb(mockExecutor); - await Migrator(db, mockQueryExecutor).createAllTables(); + await Migrator(db, mockQueryExecutor).createAll(); verify(mockQueryExecutor.call(_createNoIds, [])); verify(mockQueryExecutor.call(_createWithDefaults, [])); verify(mockQueryExecutor.call(_createWithConstraints, [])); verify(mockQueryExecutor.call(_createConfig, [])); verify(mockQueryExecutor.call(_createMyTable, [])); + verify(mockQueryExecutor.call(_createMyTrigger)); }); test('infers primary keys correctly', () async {