Skip to content

Commit

Permalink
make-migrations: Fix version numbers and imports
Browse files Browse the repository at this point in the history
Closes #3345
  • Loading branch information
simolus3 committed Nov 19, 2024
1 parent d9968e1 commit 7d577c0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fix generated code missing question marks for nullable types in some instances.
- Add (opt-in) `row_class_constructor_all_required` builder option.
- Support the `dbstat` table in drift-file queries when enabling the `dbstat` module.
- Fix `make-migrations` using invalid import URIs in generated test on Windows.

## 2.21.2

Expand Down
8 changes: 4 additions & 4 deletions drift_dev/lib/src/cli/commands/make_migrations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ targets:
${blue.wrap("another_db")}: ${lightRed.wrap("lib/database2.dart")}
${green.wrap("# Optional: The directory where the test files are stored")}:
${green.wrap("# Optional: The directory where the test files are stored")}:
${blue.wrap("test_dir")}: ${lightRed.wrap("test/drift/")} ${green.wrap("# (default)")}
${green.wrap("# Optional: The directory where the schema files are stored")}:
Expand Down Expand Up @@ -344,7 +344,7 @@ ${blue.wrap("class")} ${green.wrap(dbClassName)} ${blue.wrap("extends")} ${green
migrations.sorted((a, b) => a.from.compareTo(b.from)).first;

final packageName = cli.project.buildConfig.packageName;
final relativeDbPath = p.relative(dbClassFile.path,
final relativeDbPath = p.posix.relative(dbClassFile.path,
from: p.join(cli.project.directory.path, 'lib'));

final code = """
Expand Down Expand Up @@ -472,8 +472,8 @@ final expectedNew${table.dbGetterName.pascalCase}Data = <v$to.${table.nameOfRowC
await verifier.testWithDataIntegrity(
oldVersion: $from,
newVersion: $to,
createOld: v1.DatabaseAtV$from.new,
createNew: v2.DatabaseAtV$to.new,
createOld: v$from.DatabaseAtV$from.new,
createNew: v$to.DatabaseAtV$to.new,
openTestedDatabase: $dbClassName.new,
createItems: (batch, oldDb) {
${tables.map(
Expand Down
39 changes: 34 additions & 5 deletions drift_dev/test/cli/make_migrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ void main() {
group(
'make-migrations',
() {
tearDown(() async {
try {
await project.root.delete(recursive: true);
} catch (_) {}
});
test('default', () async {
project = await TestDriftProject.create([
d.dir('lib', [d.file('db.dart', _dbContent)]),
Expand Down Expand Up @@ -127,6 +122,40 @@ targets:
});
},
);

test('supports migrations from higher starting numbers', () async {
project = await TestDriftProject.create([
d.dir('lib', [
d.file('db.dart',
_dbContent.replaceAll('schemaVersion => 1', 'schemaVersion => 3'))
]),
d.file('build.yaml', """
targets:
\$default:
builders:
drift_dev:
options:
databases:
my_database: lib/db.dart""")
]);
await project.runDriftCli(['make-migrations']);
File(p.join(project.root.path, 'lib/db.dart')).writeAsStringSync(
_dbWithNewColumnBump.replaceAll(
'schemaVersion => 2', 'schemaVersion => 4'));
await project.runDriftCli(['make-migrations']);
await d
.file(
'app/test/drift/my_database/migration_test.dart',
allOf([
IsValidDartFile(anything),
// Make sure the generated example test works.
contains('oldVersion: 3,'),
contains('newVersion: 4,'),
contains('createOld: v3.DatabaseAtV3.new,'),
contains('createNew: v4.DatabaseAtV4.new'),
]))
.validate();
});
}

const _dbContent = '''
Expand Down

0 comments on commit 7d577c0

Please sign in to comment.