From ed103c224b787f43254deb051265c91a80538a53 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 11 Oct 2025 23:03:59 +0200 Subject: [PATCH] Allow running make-migrations without tests --- drift_dev/CHANGELOG.md | 1 + .../lib/src/cli/commands/make_migrations.dart | 13 ++++- drift_dev/test/cli/make_migrations_test.dart | 47 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/drift_dev/CHANGELOG.md b/drift_dev/CHANGELOG.md index ba58938e5..10a23a21b 100644 --- a/drift_dev/CHANGELOG.md +++ b/drift_dev/CHANGELOG.md @@ -3,6 +3,7 @@ - Fix invalid schema code for views. - Add the `known_tables` option. It allows defining external tables assumed to be available without including them in the drift-managed schema. +- Make-migrations: Add `--no-test` option to disable generating tests. ## 2.28.3 diff --git a/drift_dev/lib/src/cli/commands/make_migrations.dart b/drift_dev/lib/src/cli/commands/make_migrations.dart index 1011a1aad..730959260 100644 --- a/drift_dev/lib/src/cli/commands/make_migrations.dart +++ b/drift_dev/lib/src/cli/commands/make_migrations.dart @@ -17,6 +17,11 @@ import 'package:recase/recase.dart'; class MakeMigrationCommand extends DriftCommand { MakeMigrationCommand(super.cli) { argParser.registerExportSchemaStartupCodeOption(); + argParser.addFlag( + 'test', + help: 'Whether to generate a test file template for migrations', + defaultsTo: true, + ); } @override @@ -96,6 +101,7 @@ targets: cli.exit('`test_dir` must be a relative path. Remove the leading slash'); } + final generateTests = argResults?.flag('test') ?? true; final dumpGeneratedSchemaCode = argResults?.exportSchemaStartupCode; /// The root directory where test files for all databases are stored @@ -142,8 +148,11 @@ targets: await writer.writeStepsFile(); // Write the generated test databases await writer.writeTestDatabases(); - // Write the generated test - await writer.writeTest(); + // Write the generated test (unless that option has been disabled). + if (generateTests) { + await writer.writeTest(); + } + await writer.flush(); writer.suggestDataMigrationTest(); } diff --git a/drift_dev/test/cli/make_migrations_test.dart b/drift_dev/test/cli/make_migrations_test.dart index 4553c8efb..054d67a8d 100644 --- a/drift_dev/test/cli/make_migrations_test.dart +++ b/drift_dev/test/cli/make_migrations_test.dart @@ -76,6 +76,53 @@ targets: .file('app/lib/db.steps.dart', IsValidDartFile(anything)) .validate(); }); + + test('without tests', () async { + project = await TestDriftProject.create([ + d.dir('lib', [d.file('db.dart', _dbContent)]), + d.file('build.yaml', """ +targets: + \$default: + builders: + drift_dev: + options: + databases: + my_database: lib/db.dart""") + ]); + await project.runDriftCli(['make-migrations']); + expect( + d + .file('app/drift_schemas/my_database/drift_schema_v1.json') + .io + .existsSync(), + true); + // No other files should be created for 1st version + expect(d.file('app/test').io.existsSync(), false); + + // Change the db schema and bump the version + File(p.join(project.root.path, 'lib/db.dart')) + .writeAsStringSync(_dbWithNewColumnBump); + await project.runDriftCli(['make-migrations', '--no-test']); + expect( + d + .file('app/drift_schemas/my_database/drift_schema_v2.json') + .io + .existsSync(), + true); + // Test files should be created + await d.dir('app/test/drift/my_database', [ + // No test file, option was disabled + d.nothing('migration_test.dart'), + d.file('generated/schema.dart', IsValidDartFile(anything)), + d.file('generated/schema_v1.dart', IsValidDartFile(anything)), + d.file('generated/schema_v2.dart', IsValidDartFile(anything)), + ]).validate(); + // Steps file should be created + await d + .file('app/lib/db.steps.dart', IsValidDartFile(anything)) + .validate(); + }); + test('schema_dir is respected', () async { project = await TestDriftProject.create([ d.dir('lib', [d.file('db.dart', _dbContent)]),