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. }
13. @override
14. void down(Schema schema) {
15. schema.drop('users');
16. }
17. }
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';
6. var migrationRunner = PostgresMigrationRunner(
7. PostgreSQLConnection('127.0.0.1', 5432, 'test'),
8. migrations: [
9. UserMigration(),
10. TodoMigration(),
11. ],
12. );
Running this file will produce output like the following:
1. Executes Angel migrations.
3. Usage: migration_runner <command> [arguments]
5. Global options:
6. -h, --help Print this usage information.
8. Available commands:
9. help Display help information for migration_runner.
10. refresh Resets the database, and then re-runs all migrations.
11. reset Resets the database.
12. rollback Undoes the last batch of migrations.
13. up Runs outstanding migrations.
15. 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.
