Repository navigation
Description
Problem to Solve
To support client-side databases, the migration system must be adapted to analyze and generate migrations for the server and client databases independently. The current system runs based on files, but this is not suitable for the client without a complex asset management on Flutter apps.
The proposal for this feature touches several parts of the migration system, as detailed below.
Create migration
Adapt the create migrations CLI commands to evaluate the server and the client in parallel. Migrations would be generated at once, sharing the same version if both succeeds. If one fails (due to requiring a --force parameter, for example), the other should still be generated successfully, mirroring the same experience with models and endpoints.
Creating migration (0.7s)
✓ Server migration created: migrations/20260302051713422
✗ Client migration skipped: No changes detected.
The flags --server and --client could be introduced later to allow running the command with only one database as target.
Migration assets
To make migration artifacts easily available on Flutter, we could generate them as .dart files instead of .txt/.sql. This would make them importable with no asset management complexity and would work seamlessly in both Dart-only clients or Flutter apps.
A good strategy for the implementation would be to abstract reading and writing migrations, so that server and client can use their specific formats without changing the command logic.
Migration registry
The migration_registry.txt would be replaced by a generated migration_registry.dart similar to:
// migrations/migration_registry.dart (generated)
library client_migrations;
part 'v20231205080937028/migration.dart';
part 'v20231205081959122/migration.dart';
abstract interface class ClientMigration {
String get version;
String get migrationSql;
String get definitionSql;
}
/// Ordered list of all client migrations.
/// New migrations are always appended at the end.
class MigrationRegistry {
static final List<ClientMigration> migrations = [
_Migration20231205080937028(),
_Migration20231205081959122(),
];
static List<String> get versions =>
migrations.map((m) => m.version).toList();
}Since the MigrationRegistry.migrations would be a list where migrations are appended, conflicts would be detected in the same way as with the text file when merging conflicting changes.
Migration version files
The migrations exposed on the MigrationRegistry would be generated as part files that implements the ClientMigration class:
// migrations/v20231205080937028/migration.dart (generated)
part of client_migrations;
class _Migration20231205080937028 implements ClientMigration {
@override
String get version => '20231205080937028';
@override
String get migrationSql => '''
ALTER TABLE "user" ADD COLUMN "bio" text;
''';
@override
String get definitionSql => '''
CREATE TABLE "user" (
"id" serial PRIMARY KEY,
"name" text NOT NULL,
"bio" text
);
''';
}The SQL files would be completely replaced by the unified v<version>/migration.dart file, but the JSON files would still exist, since they serve only for the CLI and are not used in runtime.
Migration execution
Migrations should be executed on the client when the DatabaseSession is created, making it predictable and symmetric with the server. A runMigrations flag (true by default) could be exposed to allow skipping the execution of migrations for more advanced use cases.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
DatabaseSessionon the client #4792