diff --git a/packages/orm/angel_migration_runner/lib/src/mysql/runner.dart b/packages/orm/angel_migration_runner/lib/src/mysql/runner.dart index 8286ffd3..c06ca06c 100644 --- a/packages/orm/angel_migration_runner/lib/src/mysql/runner.dart +++ b/packages/orm/angel_migration_runner/lib/src/mysql/runner.dart @@ -34,13 +34,11 @@ class MySqlMigrationRunner implements MigrationRunner { } if (!_connected) { - //connection = await MySQLConnection.connect(settings); - await connection.connect(); _connected = true; } await connection.execute(''' - CREATE TABLE IF NOT EXISTS "migrations" ( + CREATE TABLE IF NOT EXISTS migrations ( id serial, batch integer, path varchar(255), @@ -76,18 +74,19 @@ class MySqlMigrationRunner implements MigrationRunner { var schema = MySqlSchema(); migration.up(schema); _log.info('Added "$k" into "migrations" table.'); - await schema.run(connection).then((_) { - return connection.transactional((ctx) async { - var result = await ctx.execute( - "INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, '$k')"); - - return result.affectedRows; + await schema.run(connection).then((_) async { + var result = await connection + .execute( + "INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, '$k')") + .catchError((e) { + _log.severe('Failed to insert into "migrations" table.', e); }); - //return connection.execute( - // 'INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\');'); - }).catchError((e) { - _log.severe('Failed to insert into "migrations" table.'); + + return result.affectedRows.toInt(); }); + //return connection.execute( + // 'INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\');'); + } } else { _log.warning('Nothing to add into "migrations" table.'); diff --git a/packages/orm/angel_migration_runner/lib/src/mysql/schema.dart b/packages/orm/angel_migration_runner/lib/src/mysql/schema.dart index 1bef967f..e2143b9c 100644 --- a/packages/orm/angel_migration_runner/lib/src/mysql/schema.dart +++ b/packages/orm/angel_migration_runner/lib/src/mysql/schema.dart @@ -17,15 +17,18 @@ class MySqlSchema extends Schema { Future run(MySQLConnection connection) async { //return connection.execute(compile()); - var result = await connection.transactional((ctx) async { + int affectedRows = 0; + await connection.transactional((ctx) async { var sql = compile(); var result = await ctx.execute(sql).catchError((e) { _log.severe('Failed to run query: [ $sql ]', e); }); - return result.affectedRows.toInt(); + affectedRows = result.affectedRows.toInt(); + }).catchError((e) { + _log.severe('Failed to run query in a transaction', e); }); - return result; + return affectedRows; } String compile() => _buf.toString(); @@ -41,14 +44,14 @@ class MySqlSchema extends Schema { @override void drop(String tableName, {bool cascade = false}) { var c = cascade == true ? ' CASCADE' : ''; - _writeln('DROP TABLE "$tableName"$c;'); + _writeln('DROP TABLE $tableName$c;'); } @override void alter(String tableName, void Function(MutableTable table) callback) { var tbl = MysqlAlterTable(tableName); callback(tbl); - _writeln('ALTER TABLE "$tableName"'); + _writeln('ALTER TABLE $tableName'); tbl.compile(_buf, _indent + 1); _buf.write(';'); } @@ -58,7 +61,7 @@ class MySqlSchema extends Schema { var op = ifNotExists ? ' IF NOT EXISTS' : ''; var tbl = MysqlTable(); callback(tbl); - _writeln('CREATE TABLE$op "$tableName" ('); + _writeln('CREATE TABLE$op $tableName ('); tbl.compile(_buf, _indent + 1); _buf.writeln(); _writeln(');'); diff --git a/packages/orm/angel_migration_runner/lib/src/mysql/table.dart b/packages/orm/angel_migration_runner/lib/src/mysql/table.dart index 402a21e0..22b86ea2 100644 --- a/packages/orm/angel_migration_runner/lib/src/mysql/table.dart +++ b/packages/orm/angel_migration_runner/lib/src/mysql/table.dart @@ -53,8 +53,7 @@ abstract class MySqlGenerator { } static String compileReference(MigrationColumnReference ref) { - var buf = - StringBuffer('REFERENCES "${ref.foreignTable}"("${ref.foreignKey}")'); + var buf = StringBuffer('REFERENCES ${ref.foreignTable}(${ref.foreignKey})'); if (ref.behavior != null) buf.write(' ' + ref.behavior!); return buf.toString(); } @@ -84,7 +83,7 @@ class MysqlTable extends Table { buf.write(' '); } - buf.write('"$name" $col'); + buf.write('$name $col'); }); } } @@ -122,14 +121,14 @@ class MysqlAlterTable extends Table implements MutableTable { buf.write(' '); } - buf.write('ADD COLUMN "$name" $col'); + buf.write('ADD COLUMN $name $col'); }); } @override MigrationColumn declareColumn(String name, Column column) { if (_columns.containsKey(name)) { - throw StateError('Cannot redeclare column "$name".'); + throw StateError('Cannot redeclare column $name.'); } var col = MigrationColumn.from(column); _columns[name] = col; @@ -138,32 +137,32 @@ class MysqlAlterTable extends Table implements MutableTable { @override void dropNotNull(String name) { - _stack.add('ALTER COLUMN "$name" DROP NOT NULL'); + _stack.add('ALTER COLUMN $name DROP NOT NULL'); } @override void setNotNull(String name) { - _stack.add('ALTER COLUMN "$name" SET NOT NULL'); + _stack.add('ALTER COLUMN $name SET NOT NULL'); } @override void changeColumnType(String name, ColumnType type, {int length = 256}) { - _stack.add('ALTER COLUMN "$name" TYPE ' + + _stack.add('ALTER COLUMN $name TYPE ' + MySqlGenerator.columnType(MigrationColumn(type, length: length))); } @override void renameColumn(String name, String newName) { - _stack.add('RENAME COLUMN "$name" TO "$newName"'); + _stack.add('RENAME COLUMN $name TO $newName'); } @override void dropColumn(String name) { - _stack.add('DROP COLUMN "$name"'); + _stack.add('DROP COLUMN $name'); } @override void rename(String newName) { - _stack.add('RENAME TO "$newName"'); + _stack.add('RENAME TO $newName'); } }