From 00cffcf20019947bae544f388bd5271c62b6dbc8 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 4 Apr 2019 16:30:53 -0400 Subject: [PATCH] Migration runner support for default values --- angel_migration_runner/CHANGELOG.md | 3 +++ angel_migration_runner/example/main.dart | 18 +++++++++++++++ .../lib/src/postgres/table.dart | 22 +++++++++++++++++++ angel_migration_runner/pubspec.yaml | 3 ++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/angel_migration_runner/CHANGELOG.md b/angel_migration_runner/CHANGELOG.md index 87b68dd0..a22ee51a 100755 --- a/angel_migration_runner/CHANGELOG.md +++ b/angel_migration_runner/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0-alpha.5 +* Support default values for columns. + # 2.0.0-alpha.4 * Include the names of migration classes when running. diff --git a/angel_migration_runner/example/main.dart b/angel_migration_runner/example/main.dart index f5a4b467..4189dfe3 100755 --- a/angel_migration_runner/example/main.dart +++ b/angel_migration_runner/example/main.dart @@ -1,5 +1,7 @@ +import 'package:angel_migration/angel_migration.dart'; import 'package:angel_migration_runner/angel_migration_runner.dart'; import 'package:angel_migration_runner/postgres.dart'; +import 'package:angel_orm/angel_orm.dart'; import 'package:postgres/postgres.dart'; import '../../angel_migration/example/todo.dart'; @@ -9,7 +11,23 @@ var migrationRunner = new PostgresMigrationRunner( migrations: [ new UserMigration(), new TodoMigration(), + new FooMigration(), ], ); main(List args) => runMigrations(migrationRunner, args); + +class FooMigration extends Migration { + @override + void up(Schema schema) { + schema.create('foos', (table) { + table + ..serial('id').primaryKey() + ..varChar('bar', length: 64) + ..timeStamp('created_at').defaultsTo(currentTimestamp); + }); + } + + @override + void down(Schema schema) => schema.drop('foos'); +} diff --git a/angel_migration_runner/lib/src/postgres/table.dart b/angel_migration_runner/lib/src/postgres/table.dart index 1dbcda21..c5f6d9e1 100755 --- a/angel_migration_runner/lib/src/postgres/table.dart +++ b/angel_migration_runner/lib/src/postgres/table.dart @@ -1,6 +1,7 @@ import 'dart:collection'; import 'package:angel_orm/angel_orm.dart'; import 'package:angel_migration/angel_migration.dart'; +import 'package:charcode/ascii.dart'; abstract class PostgresGenerator { static String columnType(MigrationColumn column) { @@ -15,6 +16,27 @@ abstract class PostgresGenerator { var buf = new StringBuffer(columnType(column)); if (column.isNullable == false) buf.write(' NOT NULL'); + if (column.defaultValue != null) { + String s; + var value = column.defaultValue; + if (value is RawSql) + s = value.value; + else if (value is String) { + var b = StringBuffer(); + for (var ch in value.codeUnits) { + if (ch == $single_quote) { + b.write("\\'"); + } else { + b.writeCharCode(ch); + } + } + s = b.toString(); + } else { + s = value.toString(); + } + + buf.write(' DEFAULT $s'); + } if (column.indexType == IndexType.unique) buf.write(' UNIQUE'); diff --git a/angel_migration_runner/pubspec.yaml b/angel_migration_runner/pubspec.yaml index a1e4a5f8..7ac7d7ab 100755 --- a/angel_migration_runner/pubspec.yaml +++ b/angel_migration_runner/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_migration_runner -version: 2.0.0-alpha.4 +version: 2.0.0-alpha.5 description: Command-line based database migration runner for Angel's ORM. author: Tobe O homepage: https://github.com/angel-dart/migration @@ -9,4 +9,5 @@ dependencies: angel_migration: ^2.0.0-alpha angel_orm: ^2.0.0-dev.2 args: ^1.0.0 + charcode: ^1.0.0 postgres: ">=0.9.5 <2.0.0" \ No newline at end of file