Exclude internal tables from schema verification

This commit is contained in:
Simon Binder 2022-06-10 11:02:52 +02:00
parent 26d9e79559
commit 7be4e90569
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 33 additions and 2 deletions

View File

@ -83,8 +83,8 @@ Input? _parseInputFromSchemaRow(
Map<String, Object?> row, List<String> virtualTables) {
final name = row['name'] as String;
// Skip sqlite-internal tables
if (name.startsWith('sqlite_autoindex')) return null;
// Skip sqlite-internal tables, https://www.sqlite.org/fileformat2.html#intschema
if (name.startsWith('sqlite_')) return null;
if (virtualTables.any((v) => name.startsWith('${v}_'))) return null;
return Input(name, row['sql'] as String);

View File

@ -1,5 +1,7 @@
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:drift_dev/api/migrations.dart';
import 'package:drift_dev/src/services/schema/verifier_impl.dart';
import 'package:test/test.dart';
void main() {
@ -33,6 +35,35 @@ void main() {
driftRuntimeOptions.dontWarnAboutMultipleDatabases = false;
});
});
group('collectSchemaInput', () {
test('contains only user-defined entities', () async {
final db = _TestDatabase(NativeDatabase.memory(), 1);
addTearDown(db.close);
await db.customStatement('CREATE TABLE x1 (bar UNIQUE);');
await db.customStatement('CREATE VIEW x2 AS SELECT * FROM x1;');
await db.customStatement('CREATE INDEX x3 ON x1 (bar);');
await db.customStatement('CREATE TRIGGER x4 AFTER INSERT ON x1 BEGIN '
'DELETE FROM x1;'
'END;');
final inputs = await db.collectSchemaInput(const []);
expect(inputs.map((e) => e.name), ['x1', 'x2', 'x3', 'x4']);
});
test('does not contain shadow tables', () async {
final db = _TestDatabase(NativeDatabase.memory(), 1);
addTearDown(db.close);
await db.customStatement('CREATE VIRTUAL TABLE x USING fts5(foo, bar);');
await db
.customStatement('INSERT INTO x VALUES (?, ?)', ['test', 'another']);
final inputs = await db.collectSchemaInput(const ['x']);
expect(inputs.map((e) => e.name), ['x']);
});
});
}
class _TestHelper implements SchemaInstantiationHelper {