-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate schema in DevTools extension
- Loading branch information
Showing
17 changed files
with
413 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,4 @@ dev_dependencies: | |
shelf: ^1.3.0 | ||
stack_trace: ^1.10.0 | ||
test_descriptor: ^2.0.1 | ||
vm_service: ^13.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:drift/native.dart'; | ||
|
||
import '../../generated/todos.dart'; | ||
|
||
void main() { | ||
TodoDb(NativeDatabase.memory()); | ||
print('database created'); | ||
|
||
// Keep the process alive | ||
Stream<void>.periodic(const Duration(seconds: 10)).listen(null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:vm_service/vm_service.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:vm_service/vm_service_io.dart'; | ||
|
||
void main() { | ||
late Process child; | ||
late VmService vm; | ||
late String isolateId; | ||
|
||
setUpAll(() async { | ||
final socket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0); | ||
final port = socket.port; | ||
await socket.close(); | ||
|
||
String sdk = p.dirname(p.dirname(Platform.resolvedExecutable)); | ||
child = await Process.start(p.join(sdk, 'bin', 'dart'), [ | ||
'run', | ||
'--enable-vm-service=$port', | ||
'--disable-service-auth-codes', | ||
'--enable-asserts', | ||
'test/integration_tests/devtools/app.dart', | ||
]); | ||
|
||
final vmServiceListening = Completer<void>(); | ||
final databaseOpened = Completer<void>(); | ||
|
||
child.stdout | ||
.map(utf8.decode) | ||
.transform(const LineSplitter()) | ||
.listen((line) { | ||
if (line.startsWith('The Dart VM service is listening')) { | ||
vmServiceListening.complete(); | ||
} else if (line == 'database created') { | ||
databaseOpened.complete(); | ||
} else if (!line.startsWith('The Dart DevTools')) { | ||
print('[child]: $line'); | ||
} | ||
}); | ||
|
||
await vmServiceListening.future; | ||
vm = await vmServiceConnectUri('ws://localhost:$port/ws'); | ||
|
||
final state = await vm.getVM(); | ||
isolateId = state.isolates!.single.id!; | ||
|
||
await databaseOpened.future; | ||
}); | ||
|
||
tearDownAll(() async { | ||
child.kill(); | ||
}); | ||
|
||
test('can list create statements', () async { | ||
final response = await vm.callServiceExtension( | ||
'ext.drift.database', | ||
args: {'action': 'collect-expected-schema', 'db': '0'}, | ||
isolateId: isolateId, | ||
); | ||
|
||
expect( | ||
response.json!['r'], | ||
containsAll([ | ||
'CREATE TABLE IF NOT EXISTS "categories" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "desc" TEXT NOT NULL UNIQUE, "priority" INTEGER NOT NULL DEFAULT 0, "description_in_upper_case" TEXT NOT NULL GENERATED ALWAYS AS (UPPER("desc")) VIRTUAL);', | ||
'CREATE TABLE IF NOT EXISTS "todos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "title" TEXT NULL, "content" TEXT NOT NULL, "target_date" INTEGER NULL UNIQUE, "category" INTEGER NULL REFERENCES categories (id), "status" TEXT NULL, UNIQUE ("title", "category"), UNIQUE ("title", "target_date"));', | ||
'CREATE TABLE IF NOT EXISTS "shared_todos" ("todo" INTEGER NOT NULL, "user" INTEGER NOT NULL, PRIMARY KEY ("todo", "user"), FOREIGN KEY (todo) REFERENCES todos(id), FOREIGN KEY (user) REFERENCES users(id));' | ||
])); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'find_differences.dart'; | ||
|
||
/// Attempts to recognize whether [name] is likely the name of an internal | ||
/// sqlite3 table (like `sqlite3_sequence`) that we should not consider when | ||
/// comparing schemas. | ||
bool isInternalElement(String name, List<String> virtualTables) { | ||
// Skip sqlite-internal tables, https://www.sqlite.org/fileformat2.html#intschema | ||
if (name.startsWith('sqlite_')) return true; | ||
if (virtualTables.any((v) => name.startsWith('${v}_'))) return true; | ||
|
||
// This file is added on some Android versions when using the native Android | ||
// database APIs, https://github.com/simolus3/drift/discussions/2042 | ||
if (name == 'android_metadata') return true; | ||
|
||
return false; | ||
} | ||
|
||
void verify(List<Input> referenceSchema, List<Input> actualSchema, | ||
bool validateDropped) { | ||
final result = | ||
FindSchemaDifferences(referenceSchema, actualSchema, validateDropped) | ||
.compare(); | ||
|
||
if (!result.noChanges) { | ||
throw SchemaMismatch(result.describe()); | ||
} | ||
} | ||
|
||
/// Thrown when the actual schema differs from the expected schema. | ||
class SchemaMismatch implements Exception { | ||
final String explanation; | ||
|
||
SchemaMismatch(this.explanation); | ||
|
||
@override | ||
String toString() { | ||
return 'Schema does not match\n$explanation'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.