Angel’s ORM ships with support for running database migrations, using a systemmodeled over that of Laravel.

An example is shown below:

  1. class UserMigration implements Migration {
  2. @override
  3. void up(Schema schema) {
  4. schema.create('users', (table) {
  5. table
  6. ..serial('id').primaryKey()
  7. ..varChar('username', length: 32).unique()
  8. ..varChar('password')
  9. ..boolean('account_confirmed').defaultsTo(false);
  10. });
  11. }
  12. @override
  13. void down(Schema schema) {
  14. schema.drop('users');
  15. }
  16. }

Migrations can be used to either create, alter, or drop database tables.

For more in-depth documentation, consult the angel_migration documentation:

https://github.com/angel-dart/migration

If you use angel_orm_generator, then a migration will be generated by default for each classannotated with @orm.

To disable this:

  1. @Orm(generateMigrations: false)
  2. abstract class _MyModel extends Model {}

Running Migrations

Using package:angel_migration_runner, we can create executables that run our database migrations:

  1. import 'package:angel_migration_runner/angel_migration_runner.dart';
  2. import 'package:angel_migration_runner/postgres.dart';
  3. import 'package:postgres/postgres.dart';
  4. import '../../angel_migration/example/todo.dart';
  5. var migrationRunner = PostgresMigrationRunner(
  6. PostgreSQLConnection('127.0.0.1', 5432, 'test'),
  7. migrations: [
  8. UserMigration(),
  9. TodoMigration(),
  10. ],
  11. );

Running this file will produce output like the following:

  1. Executes Angel migrations.
  2. Usage: migration_runner <command> [arguments]
  3. Global options:
  4. -h, --help Print this usage information.
  5. Available commands:
  6. help Display help information for migration_runner.
  7. refresh Resets the database, and then re-runs all migrations.
  8. reset Resets the database.
  9. rollback Undoes the last batch of migrations.
  10. up Runs outstanding migrations.
  11. Run "migration_runner help <command>" for more information about a command.

The migration runner keeps track of a migrations table, in order to be ableto keep track of which migrations it has run.