Skip to content

Commit

Permalink
Fix invalid schema steps code without migrations
Browse files Browse the repository at this point in the history
`stepByStep` and `migrationSteps` take a callback for each schema
upgrade as named callbacks.
When no arguments are necessary because no schema versions are there, an
invalid empty named argument list would be generated (`steps({})`).
This fixes the generation to not generate any arguments in that case.

Closes #3064
  • Loading branch information
simolus3 committed Jun 27, 2024
1 parent 9873680 commit 482b7c7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
4 changes: 4 additions & 0 deletions drift_dev/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

- Fix generated `CREATE VIEW` statements containing existing row class syntax
only supposed to be used during static analysis.
- Fix Dart views referencing the same column from different table aliases using
columns with the same name.
- Fix `drift_dev schema steps` generating invalid code when no migrations have
been defined yet.

## 2.18.0

Expand Down
16 changes: 12 additions & 4 deletions drift_dev/lib/src/writer/schema_version_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,21 @@ class SchemaVersionWriter {
}

void _writeCallbackArgsForStep(TextEmitter text) {
if (versions.withNext.isEmpty) {
return;
}

text.write('{');

for (final (current, next) in versions.withNext) {
text
..write('required Future<void> Function(')
..writeDriftRef('Migrator')
..write(' m, ${_nameForSchemaClass(next.version)} schema)')
..writeln('from${current.version}To${next.version},');
}

text.write('}');
}

void write() {
Expand Down Expand Up @@ -374,10 +382,10 @@ class SchemaVersionWriter {
// to the numbered Schema<x> class used to lookup elements.
final steps = libraryScope.leaf()
..writeUriRef(_schemaLibrary, 'MigrationStepWithVersion')
..write(' migrationSteps({');
..write(' migrationSteps(');
_writeCallbackArgsForStep(steps);
steps
..writeln('}) {')
..writeln(') {')
..writeln('return (currentVersion, database) async {')
..writeln('switch (currentVersion) {');

Expand All @@ -403,10 +411,10 @@ class SchemaVersionWriter {

final stepByStep = libraryScope.leaf()
..writeDriftRef('OnUpgrade')
..write(' stepByStep({');
..write(' stepByStep(');
_writeCallbackArgsForStep(stepByStep);
stepByStep
..writeln('}) => ')
..writeln(') => ')
..writeUriRef(_schemaLibrary, 'VersionedSchema')
..write('.stepByStepHelper(step: migrationSteps(');

Expand Down
90 changes: 90 additions & 0 deletions drift_dev/test/cli/schema/steps_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

import '../../utils.dart';
import '../utils.dart';

void main() {
test('generates valid code when no schemas have been saved', () async {
final project = await TestDriftProject.create([d.dir('drift_schemas/')]);
final path = project.root.path;
await project.runDriftCli([
'schema',
'steps',
'$path/drift_schemas/',
'$path/schema_versions.dart',
]);

await d
.file('app/schema_versions.dart', IsValidDartFile(anything))
.validate();
});

test('generates valid code when only one schema version is saved', () async {
final project = await TestDriftProject.create([
d.dir(
'drift_schemas/',
[
d.file('v1.json', '''
{
"meta": {
"version": "1.0.0"
},
"options": {
"store_date_time_values_as_text": false
},
"entities": [
{
"id": 0,
"references": [],
"type": "table",
"data": {
"name": "groups",
"was_declared_in_moor": true,
"columns": [
{
"name": "id",
"getter_name": "id",
"moor_type": "int",
"nullable": false,
"customConstraints": "NOT NULL PRIMARY KEY AUTOINCREMENT",
"default_dart": null,
"default_client_dart": null,
"dsl_features": [
"auto-increment"
]
},
{
"name": "name",
"getter_name": "name",
"moor_type": "string",
"nullable": false,
"customConstraints": "NOT NULL",
"default_dart": null,
"default_client_dart": null,
"dsl_features": []
}
],
"is_virtual": false,
"without_rowid": false
}
},
],
}
'''),
],
),
]);
final path = project.root.path;
await project.runDriftCli([
'schema',
'steps',
'$path/drift_schemas/',
'$path/schema_versions.dart',
]);

await d
.file('app/schema_versions.dart', IsValidDartFile(anything))
.validate();
});
}
2 changes: 1 addition & 1 deletion drift_dev/test/cli/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TestDriftProject {
name: app
environment:
sdk: ^2.12.0
sdk: ^3.0.0
dependencies:
drift:
Expand Down

0 comments on commit 482b7c7

Please sign in to comment.