diff --git a/config/development.yaml b/config/development.yaml index bcfffb6..a4b3d02 100644 --- a/config/development.yaml +++ b/config/development.yaml @@ -1,2 +1,6 @@ # Development-only server configuration. -port: 3000 \ No newline at end of file +port: 3000 +furlong: + database: angel + username: root + password: password \ No newline at end of file diff --git a/lib/migrations/group.dart b/lib/migrations/group.dart new file mode 100644 index 0000000..662fde3 --- /dev/null +++ b/lib/migrations/group.dart @@ -0,0 +1,19 @@ +import 'dart:async'; +import 'package:furlong/furlong.dart'; + +class GroupMigration extends Migration { + String get name => "Groups table"; + + @override + Future create(Migrator migrator) async { + migrator.create("groups", (table) { + table.id(); + table.varChar("name"); + }); + } + + @override + Future destroy(Migrator migrator) async { + migrator.drop(["groups"]); + } +} diff --git a/lib/migrations/migrations.dart b/lib/migrations/migrations.dart new file mode 100644 index 0000000..6f347e8 --- /dev/null +++ b/lib/migrations/migrations.dart @@ -0,0 +1,68 @@ +library angel.migrations; + +import 'dart:async'; +import 'dart:io'; +import 'package:angel_configuration/angel_configuration.dart'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:furlong/furlong.dart'; +import 'package:sqljocky/sqljocky.dart'; +export 'group.dart'; +export 'todo.dart'; + +Future createPool() async { + var app = new Angel(); + await app.configure(loadConfigurationFile()); + Map config = app.properties["furlong"]; + + if (config == null) + throw new Exception( + "Please provide Furlong configuration with your configuration file."); + + String type = config["type"] ?? "mysql"; + + if (type == "mysql") { + return new ConnectionPool( + host: config["host"] ?? "localhost", + port: config["port"] ?? 3306, + user: config["username"], + password: config["password"], + db: config["database"], + useSSL: config["ssl"] == true); + } else + throw new Exception("Unsupported database driver '$type'."); +} + +migrateDown(List migrations) async { + print("Undoing all migrations..."); + var pool = await createPool(); + var furlong = new Furlong(pool, migrations: migrations); + await furlong.down(); + print("Done."); + exit(0); +} + +migrateReset(List migrations) async { + var pool = await createPool(); + var furlong = new Furlong(pool, migrations: migrations); + await furlong.reset(); + print("Done."); + exit(0); +} + +migrateRevert(List migrations) async { + print("Reverting last batch..."); + var pool = await createPool(); + var furlong = new Furlong(pool, migrations: migrations); + await furlong.revert(); + print("Done."); + exit(0); +} + +migrateUp(List migrations) async { + print("Running all outstanding migrations..."); + var pool = await createPool(); + var furlong = new Furlong(pool, migrations: migrations); + await furlong.up(); + print("Done."); + exit(0); +} \ No newline at end of file diff --git a/lib/migrations/todo.dart b/lib/migrations/todo.dart new file mode 100644 index 0000000..139696e --- /dev/null +++ b/lib/migrations/todo.dart @@ -0,0 +1,21 @@ +import 'dart:async'; +import 'package:furlong/furlong.dart'; + +class TodoMigration extends Migration { + String get name => "Todos table"; + + @override + Future create(Migrator migrator) async { + migrator.create("todos", (table) { + table.id(); + table.varChar("group_id").nullable = true; + table.varChar("title").nullable = true; + table.varChar("text"); + }); + } + + @override + Future destroy(Migrator migrator) async { + migrator.drop(["todos"]); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index d46603e..08aa117 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,6 +11,8 @@ dependencies: angel_mongo: ^1.0.0-dev angel_mustache: ^1.0.0-dev angel_static: ^1.0.0 + furlong: + path: /Users/tobe/Source/Dart/furlong json_god: ^2.0.0-beta validate: ^1.5.2 dev_dependencies: diff --git a/tool/grind.dart b/tool/grind.dart index 707ddd6..602f2ce 100644 --- a/tool/grind.dart +++ b/tool/grind.dart @@ -1,5 +1,15 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:angel/migrations/migrations.dart'; +import 'package:furlong/furlong.dart'; import 'package:grinder/grinder.dart'; +final List migrations = [ + // Your migrations here! + new GroupMigration(), + new TodoMigration() +]; + main(args) => grind(args); @Task() @@ -13,3 +23,18 @@ build() { @Task() clean() => defaultClean(); + +@Task("Generates classes from your Furlong migrations.") +generate() async {} + +@Task("Reverts the database state to before any Furlong migrations were run.") +down() => migrateDown(migrations); + +@Task("Undoes and re-runs all Furlong migrations.") +reset() => migrateReset(migrations); + +@Task("Undoes the last batch of Furlong migrations run.") +revert() => migrateRevert(migrations); + +@Task("Runs any outstanding Furlong migrations.") +up() => migrateUp(migrations);