Fixed MariaDB migration
This commit is contained in:
parent
d845259edb
commit
4ba42fca73
11 changed files with 424 additions and 18 deletions
|
@ -1,5 +1,9 @@
|
|||
# Change Log
|
||||
|
||||
## 6.0.1
|
||||
|
||||
* Fixed MariaDb migration
|
||||
|
||||
## 6.0.0
|
||||
|
||||
* Updated to SDK 2.16.x
|
||||
|
|
|
@ -27,7 +27,7 @@ void main(List<String> args) async {
|
|||
password: "Test123*",
|
||||
secure: false);
|
||||
|
||||
var mysqlMigrationRunner = MysqlMigrationRunner(
|
||||
var mysqlMigrationRunner = MySqlMigrationRunner(
|
||||
mySQLConn,
|
||||
migrations: [
|
||||
UserMigration(),
|
||||
|
|
3
packages/orm/angel_migration_runner/lib/mariadb.dart
Normal file
3
packages/orm/angel_migration_runner/lib/mariadb.dart
Normal file
|
@ -0,0 +1,3 @@
|
|||
export 'src/mariadb/runner.dart';
|
||||
export 'src/mariadb/schema.dart';
|
||||
export 'src/mariadb/table.dart';
|
154
packages/orm/angel_migration_runner/lib/src/mariadb/runner.dart
Normal file
154
packages/orm/angel_migration_runner/lib/src/mariadb/runner.dart
Normal 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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
168
packages/orm/angel_migration_runner/lib/src/mariadb/table.dart
Normal file
168
packages/orm/angel_migration_runner/lib/src/mariadb/table.dart
Normal 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');
|
||||
}
|
||||
}
|
|
@ -7,15 +7,15 @@ import '../runner.dart';
|
|||
import '../util.dart';
|
||||
import 'schema.dart';
|
||||
|
||||
class MysqlMigrationRunner implements MigrationRunner {
|
||||
final _log = Logger('PostgresMigrationRunner');
|
||||
class MySqlMigrationRunner implements MigrationRunner {
|
||||
final _log = Logger('MysqlMigrationRunner');
|
||||
|
||||
final Map<String, Migration> migrations = {};
|
||||
final Queue<Migration> _migrationQueue = Queue();
|
||||
final MySQLConnection connection;
|
||||
bool _connected = false;
|
||||
|
||||
MysqlMigrationRunner(this.connection,
|
||||
MySqlMigrationRunner(this.connection,
|
||||
{Iterable<Migration> migrations = const [], bool connected = false}) {
|
||||
if (migrations.isNotEmpty == true) migrations.forEach(addMigration);
|
||||
_connected = connected == true;
|
||||
|
@ -43,7 +43,7 @@ class MysqlMigrationRunner implements MigrationRunner {
|
|||
CREATE TABLE IF NOT EXISTS "migrations" (
|
||||
id serial,
|
||||
batch integer,
|
||||
path varchar,
|
||||
path varchar(255),
|
||||
PRIMARY KEY(id)
|
||||
);
|
||||
''').then((result) {
|
||||
|
@ -73,7 +73,7 @@ class MysqlMigrationRunner implements MigrationRunner {
|
|||
|
||||
for (var k in toRun) {
|
||||
var migration = migrations[k]!;
|
||||
var schema = MysqlSchema();
|
||||
var schema = MySqlSchema();
|
||||
migration.up(schema);
|
||||
_log.info('Added "$k" into "migrations" table.');
|
||||
await schema.run(connection).then((_) {
|
||||
|
@ -115,7 +115,7 @@ class MysqlMigrationRunner implements MigrationRunner {
|
|||
if (toRun.isNotEmpty) {
|
||||
for (var k in toRun.reversed) {
|
||||
var migration = migrations[k]!;
|
||||
var schema = MysqlSchema();
|
||||
var schema = MySqlSchema();
|
||||
migration.down(schema);
|
||||
_log.info('Removed "$k" from "migrations" table.');
|
||||
await schema.run(connection).then((_) {
|
||||
|
@ -139,7 +139,7 @@ class MysqlMigrationRunner implements MigrationRunner {
|
|||
if (toRun.isNotEmpty) {
|
||||
for (var k in toRun.reversed) {
|
||||
var migration = migrations[k]!;
|
||||
var schema = MysqlSchema();
|
||||
var schema = MySqlSchema();
|
||||
migration.down(schema);
|
||||
_log.info('Removed "$k" from "migrations" table.');
|
||||
await schema.run(connection).then((_) {
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
import 'dart:async';
|
||||
import 'package:angel3_migration/angel3_migration.dart';
|
||||
import 'package:angel3_migration_runner/src/mysql/table.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
|
||||
class MysqlSchema extends Schema {
|
||||
import 'table.dart';
|
||||
|
||||
class MySqlSchema extends Schema {
|
||||
final _log = Logger('MysqlSchema');
|
||||
|
||||
final int _indent;
|
||||
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 {
|
||||
//return connection.execute(compile());
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'package:angel3_orm/angel3_orm.dart';
|
|||
import 'package:angel3_migration/angel3_migration.dart';
|
||||
import 'package:charcode/ascii.dart';
|
||||
|
||||
abstract class MysqlGenerator {
|
||||
abstract class MySqlGenerator {
|
||||
static String columnType(MigrationColumn column) {
|
||||
var str = column.type.name;
|
||||
if (column.type.hasSize) {
|
||||
|
@ -77,7 +77,7 @@ class MysqlTable extends Table {
|
|||
var i = 0;
|
||||
|
||||
_columns.forEach((name, column) {
|
||||
var col = MysqlGenerator.compileColumn(column);
|
||||
var col = MySqlGenerator.compileColumn(column);
|
||||
if (i++ > 0) buf.writeln(',');
|
||||
|
||||
for (var i = 0; i < indent; i++) {
|
||||
|
@ -115,7 +115,7 @@ class MysqlAlterTable extends Table implements MutableTable {
|
|||
|
||||
i = 0;
|
||||
_columns.forEach((name, column) {
|
||||
var col = MysqlGenerator.compileColumn(column);
|
||||
var col = MySqlGenerator.compileColumn(column);
|
||||
if (i++ > 0) buf.writeln(',');
|
||||
|
||||
for (var i = 0; i < indent; i++) {
|
||||
|
@ -149,7 +149,7 @@ class MysqlAlterTable extends Table implements MutableTable {
|
|||
@override
|
||||
void changeColumnType(String name, ColumnType type, {int length = 256}) {
|
||||
_stack.add('ALTER COLUMN "$name" TYPE ' +
|
||||
MysqlGenerator.columnType(MigrationColumn(type, length: length)));
|
||||
MySqlGenerator.columnType(MigrationColumn(type, length: length)));
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
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';
|
||||
import 'table.dart';
|
||||
|
||||
class PostgresSchema extends Schema {
|
||||
final _log = Logger('PostgresSchema');
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: angel3_migration_runner
|
||||
version: 6.0.0
|
||||
version: 6.0.1
|
||||
description: Command-line based database migration runner for Angel3's ORM.
|
||||
homepage: https://angel3-framework.web.app/
|
||||
repository: https://github.com/dukefirehawk/angel/tree/master/packages/orm/angel_migration_runner
|
||||
|
|
Loading…
Reference in a new issue