mirror of https://github.com/AMT-Cheif/drift.git
Verify that triggers can be created
This commit is contained in:
parent
8a54fd4729
commit
ddabf21d01
|
@ -21,7 +21,7 @@ typedef Future<void> OnMigrationFinished();
|
||||||
/// populate initial data or issue `PRAGMA` statements that you want to use.
|
/// populate initial data or issue `PRAGMA` statements that you want to use.
|
||||||
typedef OnBeforeOpen = Future<void> Function(OpeningDetails details);
|
typedef OnBeforeOpen = Future<void> Function(OpeningDetails details);
|
||||||
|
|
||||||
Future<void> _defaultOnCreate(Migrator m) => m.createAllTables();
|
Future<void> _defaultOnCreate(Migrator m) => m.createAll();
|
||||||
Future<void> _defaultOnUpdate(Migrator m, int from, int to) async =>
|
Future<void> _defaultOnUpdate(Migrator m, int from, int to) async =>
|
||||||
throw Exception("You've bumped the schema version for your moor database "
|
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 "
|
"but didn't provide a strategy for schema updates. Please do that by "
|
||||||
|
@ -64,12 +64,27 @@ class Migrator {
|
||||||
Migrator(this._db, this._executor);
|
Migrator(this._db, this._executor);
|
||||||
|
|
||||||
/// Creates all tables specified for the database, if they don't exist
|
/// Creates all tables specified for the database, if they don't exist
|
||||||
|
@Deprecated('Use createAll() instead')
|
||||||
Future<void> createAllTables() async {
|
Future<void> createAllTables() async {
|
||||||
for (var table in _db.allTables) {
|
for (var table in _db.allTables) {
|
||||||
await createTable(table);
|
await createTable(table);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates all tables, triggers, views, indexes and everything else defined
|
||||||
|
/// in the database, if they don't exist.
|
||||||
|
Future<void> 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() {
|
GenerationContext _createContext() {
|
||||||
return GenerationContext(
|
return GenerationContext(
|
||||||
_db.typeSystem,
|
_db.typeSystem,
|
||||||
|
@ -134,6 +149,11 @@ class Migrator {
|
||||||
return issueCustomQuery(context.sql, context.boundVariables);
|
return issueCustomQuery(context.sql, context.boundVariables);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Executes the `CREATE TRIGGER` statement that created the [trigger].
|
||||||
|
Future<void> createTrigger(Trigger trigger) {
|
||||||
|
return issueCustomQuery(trigger.createTriggerStmt);
|
||||||
|
}
|
||||||
|
|
||||||
/// Deletes the table with the given name. Note that this function does not
|
/// Deletes the table with the given name. Note that this function does not
|
||||||
/// escape the [name] parameter.
|
/// escape the [name] parameter.
|
||||||
Future<void> deleteTable(String name) async {
|
Future<void> deleteTable(String name) async {
|
||||||
|
|
|
@ -25,19 +25,26 @@ const _createMyTable = 'CREATE TABLE IF NOT EXISTS mytable ('
|
||||||
'somebool INTEGER, '
|
'somebool INTEGER, '
|
||||||
'somedate 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() {
|
void main() {
|
||||||
// see ../data/tables/tables.moor
|
// 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 mockExecutor = MockExecutor();
|
||||||
|
|
||||||
final mockQueryExecutor = MockQueryExecutor();
|
final mockQueryExecutor = MockQueryExecutor();
|
||||||
final db = CustomTablesDb(mockExecutor);
|
final db = CustomTablesDb(mockExecutor);
|
||||||
await Migrator(db, mockQueryExecutor).createAllTables();
|
await Migrator(db, mockQueryExecutor).createAll();
|
||||||
|
|
||||||
verify(mockQueryExecutor.call(_createNoIds, []));
|
verify(mockQueryExecutor.call(_createNoIds, []));
|
||||||
verify(mockQueryExecutor.call(_createWithDefaults, []));
|
verify(mockQueryExecutor.call(_createWithDefaults, []));
|
||||||
verify(mockQueryExecutor.call(_createWithConstraints, []));
|
verify(mockQueryExecutor.call(_createWithConstraints, []));
|
||||||
verify(mockQueryExecutor.call(_createConfig, []));
|
verify(mockQueryExecutor.call(_createConfig, []));
|
||||||
verify(mockQueryExecutor.call(_createMyTable, []));
|
verify(mockQueryExecutor.call(_createMyTable, []));
|
||||||
|
verify(mockQueryExecutor.call(_createMyTrigger));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('infers primary keys correctly', () async {
|
test('infers primary keys correctly', () async {
|
||||||
|
|
Loading…
Reference in New Issue