bloom_migrate library
Database migrations runtime and schema generation library for Bloom applications.
The bloom_migrate package provides transactional database migration capabilities,
schema diffing and SQL generation from bloom_db models, automated migration file
parsing, and migration rollback support.
Overview
- Schema Generation: Converts
ModelMetaentity descriptors into dialect-accurate DDL statements (PostgreSQL, SQLite) including tables, constraints, foreign keys, many-to-many join tables, and indexes using generateMigrationFileContent or generateCreateTableSql. - Topological Sorting: Resolves foreign key dependency graphs automatically using sortModelsTopologically to ensure parent tables are created before child tables and dropped in reverse order.
- File Conventions: Parses and formats SQL migration files following the standard
migrations/<app>/NNNN_name.sqlpattern with-- upand-- down(or-- no-down) blocks. - Transactional Runner: MigrationRunner manages the
bloom_migrationstracking table, discovers pending migrations on disk, and applies or rolls them back inside isolated database transactions.
Running Migrations
import 'package:bloom_db/bloom_db.dart';
import 'package:bloom_migrate/bloom_migrate.dart';
Future<void> runStartupMigrations(DbExecutor db) async {
final runner = MigrationRunner(
db: db,
migrationsDirectory: 'migrations',
);
final pending = await runner.getPendingMigrations();
print('Pending migrations: ${pending.length}');
final applied = await runner.migrate();
for (final record in applied) {
print('Applied: ${record.app}/${record.name} at ${record.appliedAt}');
}
}
Generating SQL from Models
import 'package:bloom_db/bloom_db.dart';
import 'package:bloom_migrate/bloom_migrate.dart';
void generateSql(List<ModelMeta> models, Dialect dialect) {
final sql = generateMigrationFileContent(
models: models,
dialect: dialect,
);
print(sql);
}
Classes
- AppliedMigration
-
Represents a migration record stored in the
bloom_migrationstracking table. - BloomMigration
-
Represents a single parsed database migration file following the
migrations/<app>/NNNN_name.sqlconvention with-- upand-- downsections. - BloomModelRegistry
-
Registry holding
ModelMetametadata definitions for migration generation. - MigrationRunner
- The database migration engine for Bloom applications.
Functions
-
defaultValueToSql(
DefaultValue defaultValue, Dialect dialect) → String? -
Converts a
DefaultValueto its SQL literal string for the targetDialect. -
fieldToSqlType(
FieldMeta field, Dialect dialect, {String? modelName}) → String -
Mapping of a model field's
FieldKindto the corresponding SQL column type string for a givenDialect. -
generateColumnDefinition(
FieldMeta field, Dialect dialect, {String? modelName}) → String -
Generates a single column definition clause for
CREATE TABLE. -
generateCreateTableSql(
ModelMeta model, Dialect dialect, {bool ifNotExists = true}) → String -
Generates the
CREATE TABLEDDL SQL string for a givenModelMeta. -
generateDownSql(
List< ModelMeta> models, Dialect dialect) → String -
Generates the complete
-- downsection SQL for a list ofModelMetas. -
generateIndexesSql(
ModelMeta model, Dialect dialect) → List< String> -
Generates all explicit and implied index SQL statements for a
ModelMeta. -
generateManyToManyTablesSql(
ModelMeta model, Dialect dialect) → List< String> -
Generates join tables for
ManyToManyrelations on aModelMeta. -
generateMigrationFileContent(
{required List< ModelMeta> models, required Dialect dialect}) → String -
Generates a complete migration file content with
-- upand-- downsections. -
generateUpSql(
List< ModelMeta> models, Dialect dialect) → String -
Generates the complete
-- upsection SQL for a list ofModelMetas. -
onDeleteToSql(
OnDelete onDelete) → String -
Converts an
OnDeletereferential integrity action to SQL syntax. -
sortModelsTopologically(
List< ModelMeta> models) → List<ModelMeta> -
Topologically sorts
modelsbased on foreign key relationships. -
splitSqlStatements(
String sql) → List< String> - Splits a raw SQL block into executable statements, properly handling semicolons and ignoring comments and string literals.
Exceptions / Errors
- MigrationCyclicDependencyException
- Thrown when models have a circular foreign key dependency preventing clean topological ordering for table creation.
- MigrationException
- Base class for all migration-related errors in Bloom.
- MigrationExecutionException
- Thrown when a SQL DDL statement execution fails during a migration.
- MigrationFileNotFoundException
- Thrown when a migration file cannot be found or read from disk.
- MigrationNonInvertibleException
-
Thrown when attempting to roll back a migration that has no
-- downsection or has explicitly opted out of reversal with-- no-down. - MissingMaxLengthException
-
Thrown when a field with a character kind is missing a required
max_lengthattribute on PostgreSQL. - UnsupportedDialectOperationException
- Thrown when an operation is not supported by the target SQL dialect.