Fixed migration for MySQL
This commit is contained in:
parent
4ba42fca73
commit
da1faa3ce3
3 changed files with 31 additions and 30 deletions
|
@ -34,13 +34,11 @@ class MySqlMigrationRunner implements MigrationRunner {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_connected) {
|
if (!_connected) {
|
||||||
//connection = await MySQLConnection.connect(settings);
|
|
||||||
await connection.connect();
|
|
||||||
_connected = true;
|
_connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
await connection.execute('''
|
await connection.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS "migrations" (
|
CREATE TABLE IF NOT EXISTS migrations (
|
||||||
id serial,
|
id serial,
|
||||||
batch integer,
|
batch integer,
|
||||||
path varchar(255),
|
path varchar(255),
|
||||||
|
@ -76,18 +74,19 @@ class MySqlMigrationRunner implements MigrationRunner {
|
||||||
var schema = MySqlSchema();
|
var schema = MySqlSchema();
|
||||||
migration.up(schema);
|
migration.up(schema);
|
||||||
_log.info('Added "$k" into "migrations" table.');
|
_log.info('Added "$k" into "migrations" table.');
|
||||||
await schema.run(connection).then((_) {
|
await schema.run(connection).then((_) async {
|
||||||
return connection.transactional((ctx) async {
|
var result = await connection
|
||||||
var result = await ctx.execute(
|
.execute(
|
||||||
"INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, '$k')");
|
"INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, '$k')")
|
||||||
|
.catchError((e) {
|
||||||
|
_log.severe('Failed to insert into "migrations" table.', e);
|
||||||
|
});
|
||||||
|
|
||||||
return result.affectedRows;
|
return result.affectedRows.toInt();
|
||||||
});
|
});
|
||||||
//return connection.execute(
|
//return connection.execute(
|
||||||
// 'INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\');');
|
// 'INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, \'$k\');');
|
||||||
}).catchError((e) {
|
|
||||||
_log.severe('Failed to insert into "migrations" table.');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_log.warning('Nothing to add into "migrations" table.');
|
_log.warning('Nothing to add into "migrations" table.');
|
||||||
|
|
|
@ -17,15 +17,18 @@ class MySqlSchema extends Schema {
|
||||||
|
|
||||||
Future<int> run(MySQLConnection connection) async {
|
Future<int> run(MySQLConnection connection) async {
|
||||||
//return connection.execute(compile());
|
//return connection.execute(compile());
|
||||||
var result = await connection.transactional((ctx) async {
|
int affectedRows = 0;
|
||||||
|
await connection.transactional((ctx) async {
|
||||||
var sql = compile();
|
var sql = compile();
|
||||||
var result = await ctx.execute(sql).catchError((e) {
|
var result = await ctx.execute(sql).catchError((e) {
|
||||||
_log.severe('Failed to run query: [ $sql ]', 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();
|
String compile() => _buf.toString();
|
||||||
|
@ -41,14 +44,14 @@ class MySqlSchema extends Schema {
|
||||||
@override
|
@override
|
||||||
void drop(String tableName, {bool cascade = false}) {
|
void drop(String tableName, {bool cascade = false}) {
|
||||||
var c = cascade == true ? ' CASCADE' : '';
|
var c = cascade == true ? ' CASCADE' : '';
|
||||||
_writeln('DROP TABLE "$tableName"$c;');
|
_writeln('DROP TABLE $tableName$c;');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void alter(String tableName, void Function(MutableTable table) callback) {
|
void alter(String tableName, void Function(MutableTable table) callback) {
|
||||||
var tbl = MysqlAlterTable(tableName);
|
var tbl = MysqlAlterTable(tableName);
|
||||||
callback(tbl);
|
callback(tbl);
|
||||||
_writeln('ALTER TABLE "$tableName"');
|
_writeln('ALTER TABLE $tableName');
|
||||||
tbl.compile(_buf, _indent + 1);
|
tbl.compile(_buf, _indent + 1);
|
||||||
_buf.write(';');
|
_buf.write(';');
|
||||||
}
|
}
|
||||||
|
@ -58,7 +61,7 @@ class MySqlSchema extends Schema {
|
||||||
var op = ifNotExists ? ' IF NOT EXISTS' : '';
|
var op = ifNotExists ? ' IF NOT EXISTS' : '';
|
||||||
var tbl = MysqlTable();
|
var tbl = MysqlTable();
|
||||||
callback(tbl);
|
callback(tbl);
|
||||||
_writeln('CREATE TABLE$op "$tableName" (');
|
_writeln('CREATE TABLE$op $tableName (');
|
||||||
tbl.compile(_buf, _indent + 1);
|
tbl.compile(_buf, _indent + 1);
|
||||||
_buf.writeln();
|
_buf.writeln();
|
||||||
_writeln(');');
|
_writeln(');');
|
||||||
|
|
|
@ -53,8 +53,7 @@ abstract class MySqlGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
static String compileReference(MigrationColumnReference ref) {
|
static String compileReference(MigrationColumnReference ref) {
|
||||||
var buf =
|
var buf = StringBuffer('REFERENCES ${ref.foreignTable}(${ref.foreignKey})');
|
||||||
StringBuffer('REFERENCES "${ref.foreignTable}"("${ref.foreignKey}")');
|
|
||||||
if (ref.behavior != null) buf.write(' ' + ref.behavior!);
|
if (ref.behavior != null) buf.write(' ' + ref.behavior!);
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
@ -84,7 +83,7 @@ class MysqlTable extends Table {
|
||||||
buf.write(' ');
|
buf.write(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.write('"$name" $col');
|
buf.write('$name $col');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,14 +121,14 @@ class MysqlAlterTable extends Table implements MutableTable {
|
||||||
buf.write(' ');
|
buf.write(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.write('ADD COLUMN "$name" $col');
|
buf.write('ADD COLUMN $name $col');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MigrationColumn declareColumn(String name, Column column) {
|
MigrationColumn declareColumn(String name, Column column) {
|
||||||
if (_columns.containsKey(name)) {
|
if (_columns.containsKey(name)) {
|
||||||
throw StateError('Cannot redeclare column "$name".');
|
throw StateError('Cannot redeclare column $name.');
|
||||||
}
|
}
|
||||||
var col = MigrationColumn.from(column);
|
var col = MigrationColumn.from(column);
|
||||||
_columns[name] = col;
|
_columns[name] = col;
|
||||||
|
@ -138,32 +137,32 @@ class MysqlAlterTable extends Table implements MutableTable {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dropNotNull(String name) {
|
void dropNotNull(String name) {
|
||||||
_stack.add('ALTER COLUMN "$name" DROP NOT NULL');
|
_stack.add('ALTER COLUMN $name DROP NOT NULL');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void setNotNull(String name) {
|
void setNotNull(String name) {
|
||||||
_stack.add('ALTER COLUMN "$name" SET NOT NULL');
|
_stack.add('ALTER COLUMN $name SET NOT NULL');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void changeColumnType(String name, ColumnType type, {int length = 256}) {
|
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)));
|
MySqlGenerator.columnType(MigrationColumn(type, length: length)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void renameColumn(String name, String newName) {
|
void renameColumn(String name, String newName) {
|
||||||
_stack.add('RENAME COLUMN "$name" TO "$newName"');
|
_stack.add('RENAME COLUMN $name TO $newName');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dropColumn(String name) {
|
void dropColumn(String name) {
|
||||||
_stack.add('DROP COLUMN "$name"');
|
_stack.add('DROP COLUMN $name');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void rename(String newName) {
|
void rename(String newName) {
|
||||||
_stack.add('RENAME TO "$newName"');
|
_stack.add('RENAME TO $newName');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue