Migration runner support for default values

This commit is contained in:
Tobe O 2019-04-04 16:30:53 -04:00
parent 84fc722cc3
commit 00cffcf200
4 changed files with 45 additions and 1 deletions

View file

@ -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.

View file

@ -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<String> 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');
}

View file

@ -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');

View file

@ -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 <thosakwe@gmail.com>
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"