Updated migration runner
This commit is contained in:
parent
8603d22d97
commit
9c4df63b8f
6 changed files with 55 additions and 13 deletions
|
@ -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
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'runner.dart';
|
|||
|
||||
/// Runs the Angel Migration CLI.
|
||||
Future runMigrations(MigrationRunner migrationRunner, List<String> 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.';
|
||||
|
||||
|
|
|
@ -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<String, Migration> migrations = {};
|
||||
final PostgreSQLConnection connection;
|
||||
final Queue<Migration> _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.');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<int> 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();
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue