Fixed MariaDB migration

This commit is contained in:
thomashii 2022-04-30 10:11:22 +08:00
parent d845259edb
commit 4ba42fca73
11 changed files with 424 additions and 18 deletions

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 6.0.1
* Fixed MariaDb migration
## 6.0.0 ## 6.0.0
* Updated to SDK 2.16.x * Updated to SDK 2.16.x

View file

@ -27,7 +27,7 @@ void main(List<String> args) async {
password: "Test123*", password: "Test123*",
secure: false); secure: false);
var mysqlMigrationRunner = MysqlMigrationRunner( var mysqlMigrationRunner = MySqlMigrationRunner(
mySQLConn, mySQLConn,
migrations: [ migrations: [
UserMigration(), UserMigration(),

View file

@ -0,0 +1,3 @@
export 'src/mariadb/runner.dart';
export 'src/mariadb/schema.dart';
export 'src/mariadb/table.dart';

View file

@ -0,0 +1,154 @@
import 'dart:async';
import 'dart:collection';
import 'package:angel3_migration/angel3_migration.dart';
import 'package:logging/logging.dart';
import 'package:mysql1/mysql1.dart';
import '../runner.dart';
import '../util.dart';
import 'schema.dart';
class MariaDbMigrationRunner implements MigrationRunner {
final _log = Logger('MariaDbMigrationRunner');
final Map<String, Migration> migrations = {};
final Queue<Migration> _migrationQueue = Queue();
final MySqlConnection connection;
bool _connected = false;
MariaDbMigrationRunner(this.connection,
{Iterable<Migration> migrations = const [], bool connected = false}) {
if (migrations.isNotEmpty == true) migrations.forEach(addMigration);
_connected = connected == true;
}
@override
void addMigration(Migration migration) {
_migrationQueue.addLast(migration);
}
Future _init() async {
while (_migrationQueue.isNotEmpty) {
var migration = _migrationQueue.removeFirst();
var path = await absoluteSourcePath(migration.runtimeType);
migrations.putIfAbsent(path.replaceAll('\\', '\\\\'), () => migration);
}
if (!_connected) {
_connected = true;
}
await connection.query('''
CREATE TABLE IF NOT EXISTS migrations (
id serial,
batch integer,
path varchar(255),
PRIMARY KEY(id)
);
''').then((result) {
_log.info('Check and create "migrations" table');
}).catchError((e) {
_log.severe('Failed to create "migrations" table.', e);
});
}
@override
Future up() async {
await _init();
var r = await connection.query('SELECT path from migrations;');
var existing = r.expand((x) => x).cast<String>();
var toRun = <String>[];
migrations.forEach((k, v) {
if (!existing.contains(k)) toRun.add(k);
});
if (toRun.isNotEmpty) {
var r = await connection.query('SELECT MAX(batch) from migrations;');
var rTmp = r.toList();
var curBatch = int.tryParse(rTmp[0][0] ?? '0') as int;
var batch = curBatch + 1;
for (var k in toRun) {
var migration = migrations[k]!;
var schema = MariaDbSchema();
migration.up(schema);
_log.info('Added "$k" into "migrations" table.');
try {
await schema.run(connection).then((_) async {
var result = await connection.query(
"INSERT INTO MIGRATIONS (batch, path) VALUES ($batch, '$k')");
return result.affectedRows;
});
} catch (e) {
_log.severe('Failed to insert into "migrations" table.', e);
}
}
} else {
_log.warning('Nothing to add into "migrations" table.');
}
}
@override
Future rollback() async {
await _init();
var r = await connection.query('SELECT MAX(batch) from migrations;');
var rTmp = r.toList();
var curBatch = int.tryParse(rTmp[0][0] ?? 0) as int;
r = await connection
.query('SELECT path from migrations WHERE batch = $curBatch;');
var existing = r.expand((x) => x).cast<String>();
var toRun = <String>[];
migrations.forEach((k, v) {
if (existing.contains(k)) toRun.add(k);
});
if (toRun.isNotEmpty) {
for (var k in toRun.reversed) {
var migration = migrations[k]!;
var schema = MariaDbSchema();
migration.down(schema);
_log.info('Removed "$k" from "migrations" table.');
await schema.run(connection).then((_) {
return connection
.query('DELETE FROM migrations WHERE path = \'$k\';');
});
}
} else {
_log.warning('Nothing to remove from "migrations" table.');
}
}
@override
Future reset() async {
await _init();
var r = await connection
.query('SELECT path from migrations ORDER BY batch DESC;');
var existing = r.expand((x) => x).cast<String>();
var toRun = existing.where(migrations.containsKey).toList();
if (toRun.isNotEmpty) {
for (var k in toRun.reversed) {
var migration = migrations[k]!;
var schema = MariaDbSchema();
migration.down(schema);
_log.info('Removed "$k" from "migrations" table.');
await schema.run(connection).then((_) {
return connection
.query('DELETE FROM migrations WHERE path = \'$k\';');
});
}
} else {
_log.warning('Nothing to remove from "migrations" table.');
}
}
@override
Future close() {
return connection.close();
}
}

View file

@ -0,0 +1,76 @@
import 'dart:async';
import 'package:angel3_migration/angel3_migration.dart';
import 'package:logging/logging.dart';
import 'package:mysql1/mysql1.dart';
import 'table.dart';
class MariaDbSchema extends Schema {
final _log = Logger('MariaDbSchema');
final int _indent;
final StringBuffer _buf;
MariaDbSchema._(this._buf, this._indent);
factory MariaDbSchema() => MariaDbSchema._(StringBuffer(), 0);
Future<int> run(MySqlConnection connection) async {
int affectedRows = 0;
await connection.transaction((ctx) async {
var sql = compile();
Results? result = await ctx.query(sql).catchError((e) {
_log.severe('Failed to run query: [ $sql ]', e);
throw e;
});
affectedRows = result?.affectedRows ?? 0;
});
return affectedRows;
}
String compile() => _buf.toString();
void _writeln(String str) {
for (var i = 0; i < _indent; i++) {
_buf.write(' ');
}
_buf.writeln(str);
}
@override
void drop(String tableName, {bool cascade = false}) {
var c = cascade == true ? ' CASCADE' : '';
_writeln('DROP TABLE "$tableName"$c;');
}
@override
void alter(String tableName, void Function(MutableTable table) callback) {
var tbl = MariaDbAlterTable(tableName);
callback(tbl);
_writeln('ALTER TABLE $tableName');
tbl.compile(_buf, _indent + 1);
_buf.write(';');
}
void _create(
String tableName, void Function(Table table) callback, bool ifNotExists) {
var op = ifNotExists ? ' IF NOT EXISTS' : '';
var tbl = MariaDbTable();
callback(tbl);
_writeln('CREATE TABLE$op $tableName (');
tbl.compile(_buf, _indent + 1);
_buf.writeln();
_writeln(');');
}
@override
void create(String tableName, void Function(Table table) callback) =>
_create(tableName, callback, false);
@override
void createIfNotExists(
String tableName, void Function(Table table) callback) =>
_create(tableName, callback, true);
}

View file

@ -0,0 +1,168 @@
import 'dart:collection';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_migration/angel3_migration.dart';
import 'package:charcode/ascii.dart';
abstract class MariaDbGenerator {
static String columnType(MigrationColumn column) {
var str = column.type.name;
if (column.type.hasSize) {
return '$str(${column.length})';
} else {
return str;
}
}
static String compileColumn(MigrationColumn column) {
var buf = 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');
} else if (column.indexType == IndexType.primaryKey) {
buf.write(' PRIMARY KEY');
}
for (var ref in column.externalReferences) {
buf.write(' ' + compileReference(ref));
}
return buf.toString();
}
static String compileReference(MigrationColumnReference ref) {
var buf = StringBuffer('REFERENCES ${ref.foreignTable}(${ref.foreignKey})');
if (ref.behavior != null) buf.write(' ' + ref.behavior!);
return buf.toString();
}
}
class MariaDbTable extends Table {
final Map<String, MigrationColumn> _columns = {};
@override
MigrationColumn declareColumn(String name, Column column) {
if (_columns.containsKey(name)) {
throw StateError('Cannot redeclare column "$name".');
}
var col = MigrationColumn.from(column);
_columns[name] = col;
return col;
}
void compile(StringBuffer buf, int indent) {
var i = 0;
_columns.forEach((name, column) {
var col = MariaDbGenerator.compileColumn(column);
if (i++ > 0) buf.writeln(',');
for (var i = 0; i < indent; i++) {
buf.write(' ');
}
buf.write('$name $col');
});
}
}
class MariaDbAlterTable extends Table implements MutableTable {
final Map<String, MigrationColumn> _columns = {};
final String tableName;
final Queue<String> _stack = Queue<String>();
MariaDbAlterTable(this.tableName);
void compile(StringBuffer buf, int indent) {
var i = 0;
while (_stack.isNotEmpty) {
var str = _stack.removeFirst();
if (i++ > 0) buf.writeln(',');
for (var i = 0; i < indent; i++) {
buf.write(' ');
}
buf.write(str);
}
if (i > 0) buf.writeln(';');
i = 0;
_columns.forEach((name, column) {
var col = MariaDbGenerator.compileColumn(column);
if (i++ > 0) buf.writeln(',');
for (var i = 0; i < indent; i++) {
buf.write(' ');
}
buf.write('ADD COLUMN $name $col');
});
}
@override
MigrationColumn declareColumn(String name, Column column) {
if (_columns.containsKey(name)) {
throw StateError('Cannot redeclare column "$name".');
}
var col = MigrationColumn.from(column);
_columns[name] = col;
return col;
}
@override
void dropNotNull(String name) {
_stack.add('ALTER COLUMN $name DROP NOT NULL');
}
@override
void setNotNull(String name) {
_stack.add('ALTER COLUMN $name SET NOT NULL');
}
@override
void changeColumnType(String name, ColumnType type, {int length = 256}) {
_stack.add('ALTER COLUMN $name TYPE ' +
MariaDbGenerator.columnType(MigrationColumn(type, length: length)));
}
@override
void renameColumn(String name, String newName) {
_stack.add('RENAME COLUMN $name TO "$newName"');
}
@override
void dropColumn(String name) {
_stack.add('DROP COLUMN $name');
}
@override
void rename(String newName) {
_stack.add('RENAME TO $newName');
}
}

View file

@ -7,15 +7,15 @@ import '../runner.dart';
import '../util.dart'; import '../util.dart';
import 'schema.dart'; import 'schema.dart';
class MysqlMigrationRunner implements MigrationRunner { class MySqlMigrationRunner implements MigrationRunner {
final _log = Logger('PostgresMigrationRunner'); final _log = Logger('MysqlMigrationRunner');
final Map<String, Migration> migrations = {}; final Map<String, Migration> migrations = {};
final Queue<Migration> _migrationQueue = Queue(); final Queue<Migration> _migrationQueue = Queue();
final MySQLConnection connection; final MySQLConnection connection;
bool _connected = false; bool _connected = false;
MysqlMigrationRunner(this.connection, MySqlMigrationRunner(this.connection,
{Iterable<Migration> migrations = const [], bool connected = false}) { {Iterable<Migration> migrations = const [], bool connected = false}) {
if (migrations.isNotEmpty == true) migrations.forEach(addMigration); if (migrations.isNotEmpty == true) migrations.forEach(addMigration);
_connected = connected == true; _connected = connected == true;
@ -43,7 +43,7 @@ class MysqlMigrationRunner implements MigrationRunner {
CREATE TABLE IF NOT EXISTS "migrations" ( CREATE TABLE IF NOT EXISTS "migrations" (
id serial, id serial,
batch integer, batch integer,
path varchar, path varchar(255),
PRIMARY KEY(id) PRIMARY KEY(id)
); );
''').then((result) { ''').then((result) {
@ -73,7 +73,7 @@ class MysqlMigrationRunner implements MigrationRunner {
for (var k in toRun) { for (var k in toRun) {
var migration = migrations[k]!; var migration = migrations[k]!;
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((_) {
@ -115,7 +115,7 @@ class MysqlMigrationRunner implements MigrationRunner {
if (toRun.isNotEmpty) { if (toRun.isNotEmpty) {
for (var k in toRun.reversed) { for (var k in toRun.reversed) {
var migration = migrations[k]!; var migration = migrations[k]!;
var schema = MysqlSchema(); var schema = MySqlSchema();
migration.down(schema); migration.down(schema);
_log.info('Removed "$k" from "migrations" table.'); _log.info('Removed "$k" from "migrations" table.');
await schema.run(connection).then((_) { await schema.run(connection).then((_) {
@ -139,7 +139,7 @@ class MysqlMigrationRunner implements MigrationRunner {
if (toRun.isNotEmpty) { if (toRun.isNotEmpty) {
for (var k in toRun.reversed) { for (var k in toRun.reversed) {
var migration = migrations[k]!; var migration = migrations[k]!;
var schema = MysqlSchema(); var schema = MySqlSchema();
migration.down(schema); migration.down(schema);
_log.info('Removed "$k" from "migrations" table.'); _log.info('Removed "$k" from "migrations" table.');
await schema.run(connection).then((_) { await schema.run(connection).then((_) {

View file

@ -1,18 +1,19 @@
import 'dart:async'; import 'dart:async';
import 'package:angel3_migration/angel3_migration.dart'; import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_migration_runner/src/mysql/table.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:mysql_client/mysql_client.dart'; import 'package:mysql_client/mysql_client.dart';
class MysqlSchema extends Schema { import 'table.dart';
class MySqlSchema extends Schema {
final _log = Logger('MysqlSchema'); final _log = Logger('MysqlSchema');
final int _indent; final int _indent;
final StringBuffer _buf; final StringBuffer _buf;
MysqlSchema._(this._buf, this._indent); MySqlSchema._(this._buf, this._indent);
factory MysqlSchema() => MysqlSchema._(StringBuffer(), 0); factory MySqlSchema() => MySqlSchema._(StringBuffer(), 0);
Future<int> run(MySQLConnection connection) async { Future<int> run(MySQLConnection connection) async {
//return connection.execute(compile()); //return connection.execute(compile());

View file

@ -3,7 +3,7 @@ import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_migration/angel3_migration.dart'; import 'package:angel3_migration/angel3_migration.dart';
import 'package:charcode/ascii.dart'; import 'package:charcode/ascii.dart';
abstract class MysqlGenerator { abstract class MySqlGenerator {
static String columnType(MigrationColumn column) { static String columnType(MigrationColumn column) {
var str = column.type.name; var str = column.type.name;
if (column.type.hasSize) { if (column.type.hasSize) {
@ -77,7 +77,7 @@ class MysqlTable extends Table {
var i = 0; var i = 0;
_columns.forEach((name, column) { _columns.forEach((name, column) {
var col = MysqlGenerator.compileColumn(column); var col = MySqlGenerator.compileColumn(column);
if (i++ > 0) buf.writeln(','); if (i++ > 0) buf.writeln(',');
for (var i = 0; i < indent; i++) { for (var i = 0; i < indent; i++) {
@ -115,7 +115,7 @@ class MysqlAlterTable extends Table implements MutableTable {
i = 0; i = 0;
_columns.forEach((name, column) { _columns.forEach((name, column) {
var col = MysqlGenerator.compileColumn(column); var col = MySqlGenerator.compileColumn(column);
if (i++ > 0) buf.writeln(','); if (i++ > 0) buf.writeln(',');
for (var i = 0; i < indent; i++) { for (var i = 0; i < indent; i++) {
@ -149,7 +149,7 @@ class MysqlAlterTable extends Table implements MutableTable {
@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

View file

@ -1,8 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'package:angel3_migration/angel3_migration.dart'; import 'package:angel3_migration/angel3_migration.dart';
import 'package:postgres/postgres.dart'; import 'package:postgres/postgres.dart';
import 'package:angel3_migration_runner/src/postgres/table.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'table.dart';
class PostgresSchema extends Schema { class PostgresSchema extends Schema {
final _log = Logger('PostgresSchema'); final _log = Logger('PostgresSchema');

View file

@ -1,5 +1,5 @@
name: angel3_migration_runner name: angel3_migration_runner
version: 6.0.0 version: 6.0.1
description: Command-line based database migration runner for Angel3's ORM. description: Command-line based database migration runner for Angel3's ORM.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_migration_runner repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_migration_runner