From 9c4df63b8f8f3686fdcc55237928bc65d8ab3cd1 Mon Sep 17 00:00:00 2001 From: thomashii Date: Sun, 25 Jul 2021 18:27:26 +0800 Subject: [PATCH] Updated migration runner --- .../orm/angel_migration_runner/CHANGELOG.md | 6 ++++ .../angel_migration_runner/lib/src/cli.dart | 3 +- .../lib/src/postgres/runner.dart | 33 ++++++++++++++----- .../lib/src/postgres/schema.dart | 16 ++++++++- .../lib/src/postgres/table.dart | 6 +++- .../orm/angel_migration_runner/pubspec.yaml | 4 ++- 6 files changed, 55 insertions(+), 13 deletions(-) diff --git a/packages/orm/angel_migration_runner/CHANGELOG.md b/packages/orm/angel_migration_runner/CHANGELOG.md index 5eb68bd4..76efd81e 100755 --- a/packages/orm/angel_migration_runner/CHANGELOG.md +++ b/packages/orm/angel_migration_runner/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## 4.0.0-beta.4 + +* Logged exception of the db query execution to console +* Added transaction to data insertion +* Fixed SQL column generator to remove size from none character based data type + ## 4.0.0-beta.3 * Updated README diff --git a/packages/orm/angel_migration_runner/lib/src/cli.dart b/packages/orm/angel_migration_runner/lib/src/cli.dart index 4c727b8a..37cce4c6 100755 --- a/packages/orm/angel_migration_runner/lib/src/cli.dart +++ b/packages/orm/angel_migration_runner/lib/src/cli.dart @@ -4,7 +4,7 @@ import 'runner.dart'; /// Runs the Angel Migration CLI. Future runMigrations(MigrationRunner migrationRunner, List args) { - var cmd = CommandRunner('migration_runner', 'Executes Angel migrations.') + var cmd = CommandRunner('migration_runner', 'Executes Angel3 migrations.') ..addCommand(_UpCommand(migrationRunner)) ..addCommand(_RefreshCommand(migrationRunner)) ..addCommand(_ResetCommand(migrationRunner)) @@ -17,6 +17,7 @@ class _UpCommand extends Command { @override String get name => 'up'; + @override String get description => 'Runs outstanding migrations.'; diff --git a/packages/orm/angel_migration_runner/lib/src/postgres/runner.dart b/packages/orm/angel_migration_runner/lib/src/postgres/runner.dart index 3ecbbb1c..b6ef48bb 100755 --- a/packages/orm/angel_migration_runner/lib/src/postgres/runner.dart +++ b/packages/orm/angel_migration_runner/lib/src/postgres/runner.dart @@ -2,11 +2,14 @@ import 'dart:async'; import 'dart:collection'; import 'package:angel3_migration/angel3_migration.dart'; import 'package:postgres/postgres.dart'; +import 'package:logging/logging.dart'; import '../runner.dart'; import '../util.dart'; import 'schema.dart'; class PostgresMigrationRunner implements MigrationRunner { + final _log = Logger('PostgresMigrationRunner'); + final Map migrations = {}; final PostgreSQLConnection connection; final Queue _migrationQueue = Queue(); @@ -42,7 +45,11 @@ class PostgresMigrationRunner implements MigrationRunner { path varchar, PRIMARY KEY(id) ); - '''); + ''').then((result) { + _log.info('Check and create "migrations" table'); + }).catchError((e) { + _log.severe('Failed to create "migrations" table.'); + }); } @override @@ -65,14 +72,22 @@ class PostgresMigrationRunner implements MigrationRunner { var migration = migrations[k]!; var schema = PostgresSchema(); migration.up(schema); - print('Bringing up "$k"...'); + _log.info('Added "$k" into "migrations" table.'); await schema.run(connection).then((_) { - return connection.execute( - 'INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\');'); + return connection.transaction((ctx) async { + var result = await ctx.query( + "INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\')"); + + return result.affectedRowCount; + }); + //return connection.execute( + // 'INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\');'); + }).catchError((e) { + _log.severe('Failed to insert into "migrations" table.'); }); } } else { - print('No migrations found to bring up.'); + _log.warning('Nothing to add into "migrations" table.'); } } @@ -96,14 +111,14 @@ class PostgresMigrationRunner implements MigrationRunner { var migration = migrations[k]!; var schema = PostgresSchema(); migration.down(schema); - print('Bringing down "$k"...'); + _log.info('Removed "$k" from "migrations" table.'); await schema.run(connection).then((_) { return connection .execute('DELETE FROM migrations WHERE path = \'$k\';'); }); } } else { - print('No migrations found to roll back.'); + _log.warning('Nothing to remove from "migrations" table.'); } } @@ -120,14 +135,14 @@ class PostgresMigrationRunner implements MigrationRunner { var migration = migrations[k]!; var schema = PostgresSchema(); migration.down(schema); - print('Bringing down "$k"...'); + _log.info('Removed "$k" from "migrations" table.'); await schema.run(connection).then((_) { return connection .execute('DELETE FROM migrations WHERE path = \'$k\';'); }); } } else { - print('No migrations found to roll back.'); + _log.warning('Nothing to remove from "migrations" table.'); } } diff --git a/packages/orm/angel_migration_runner/lib/src/postgres/schema.dart b/packages/orm/angel_migration_runner/lib/src/postgres/schema.dart index 1cf9af08..5f4c90f9 100755 --- a/packages/orm/angel_migration_runner/lib/src/postgres/schema.dart +++ b/packages/orm/angel_migration_runner/lib/src/postgres/schema.dart @@ -2,8 +2,11 @@ import 'dart:async'; import 'package:angel3_migration/angel3_migration.dart'; import 'package:postgres/postgres.dart'; import 'package:angel3_migration_runner/src/postgres/table.dart'; +import 'package:logging/logging.dart'; class PostgresSchema extends Schema { + final _log = Logger('PostgresSchema'); + final int _indent; final StringBuffer _buf; @@ -11,7 +14,18 @@ class PostgresSchema extends Schema { factory PostgresSchema() => PostgresSchema._(StringBuffer(), 0); - Future run(PostgreSQLConnection connection) => connection.execute(compile()); + Future run(PostgreSQLConnection connection) async { + //return connection.execute(compile()); + var result = await connection.transaction((ctx) async { + var sql = compile(); + var result = await ctx.query(sql).catchError((e) { + _log.severe('Failed to run query: [ $sql ]', e); + }); + return result.affectedRowCount; + }); + + return (result is int) ? result : 0; + } String compile() => _buf.toString(); diff --git a/packages/orm/angel_migration_runner/lib/src/postgres/table.dart b/packages/orm/angel_migration_runner/lib/src/postgres/table.dart index 6e68a111..ba53d156 100755 --- a/packages/orm/angel_migration_runner/lib/src/postgres/table.dart +++ b/packages/orm/angel_migration_runner/lib/src/postgres/table.dart @@ -6,7 +6,11 @@ import 'package:charcode/ascii.dart'; abstract class PostgresGenerator { static String columnType(MigrationColumn column) { var str = column.type.name; - return '$str(${column.length})'; + if (column.type.hasSize) { + return '$str(${column.length})'; + } else { + return '$str'; + } } static String compileColumn(MigrationColumn column) { diff --git a/packages/orm/angel_migration_runner/pubspec.yaml b/packages/orm/angel_migration_runner/pubspec.yaml index 3db9db08..8e474074 100755 --- a/packages/orm/angel_migration_runner/pubspec.yaml +++ b/packages/orm/angel_migration_runner/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_migration_runner -version: 4.0.0-beta.3 +version: 4.0.0-beta.4 description: Command-line based database migration runner for Angel3's ORM. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/orm/angel_migration_runner @@ -11,5 +11,7 @@ dependencies: args: ^2.1.0 charcode: ^1.2.0 postgres: ^2.3.2 + logging: ^1.0.0 dev_dependencies: pedantic: ^1.11.0 +