Fixed mariadb issue

This commit is contained in:
Thomas Hii 2024-07-21 11:16:49 +08:00
parent 19a7f8316d
commit d3df89c0a3
5 changed files with 34 additions and 21 deletions

View file

@ -1,5 +1,9 @@
# Change Log # Change Log
## 8.2.2
Fixed `MariaDbMigrationRunner` migration issues
## 8.2.1 ## 8.2.1
* Updated README * Updated README

View file

@ -14,18 +14,15 @@ This package contains the implementation of the database migration for the follo
## Usage ## Usage
* Use `PostgresMigrationRunner` to perform database migration for PostgreSQL. * Use `PostgresMigrationRunner` to perform database migration for PostgreSQL.
* Use `MySqlMigrationRunner` to perform database migration for MySQL and MariaDB. This runner is using [`mysql_client`](https://pub.dev/packages?q=mysql_client) driver. * Use `MySqlMigrationRunner` to perform database migration for MySQL and MariaDB. This runner is using [`mysql_client`](https://pub.dev/packages?q=mysql_client) driver.
* Use `MariaDbMigrationRunner` to perform database migration for MariaDB. This runner is using [`mysql1`](https://pub.dev/packages?q=mysql1) driver. * Use `MariaDbMigrationRunner` to perform database migration for MariaDB. This runner is using [`mysql1`](https://pub.dev/packages?q=mysql1) driver.
## Supported Operations ## Supported Operations
* reset - Clear out all records in the `migrations` table and drop all the ORM related tables.
* up - Generate all the tables based on the ORM models. * up - Generate all the tables based on the ORM models.
* reset - Clear out all records in the `migrations` table and drop all the ORM related tables.
* refresh - Run `reset` follow by `up` * refresh - Run `reset` follow by `up`
## To do ## Limitation
* Update existing tables * Update schema changes is not supported
* Reverse engineer tables into models

View file

@ -14,12 +14,12 @@ class MariaDbMigrationRunner implements MigrationRunner {
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;
MariaDbMigrationRunner(this.connection, MariaDbMigrationRunner(this.connection,
{Iterable<Migration> migrations = const [], bool connected = false}) { {Iterable<Migration> migrations = const [], bool connected = true}) {
if (migrations.isNotEmpty) migrations.forEach(addMigration); if (migrations.isNotEmpty) migrations.forEach(addMigration);
_connected = connected; //_connected = connected;
} }
@override @override
@ -34,20 +34,18 @@ class MariaDbMigrationRunner implements MigrationRunner {
migrations.putIfAbsent(path.replaceAll('\\', '\\\\'), () => migration); migrations.putIfAbsent(path.replaceAll('\\', '\\\\'), () => migration);
} }
if (!_connected) {
_connected = true;
}
await connection.query(''' await connection.query('''
CREATE TABLE IF NOT EXISTS migrations ( CREATE TABLE IF NOT EXISTS migrations (
id integer NOT NULL AUTO_INCREMENT, id integer NOT NULL AUTO_INCREMENT,
batch integer, batch integer,
path varchar(255), path varchar(500),
PRIMARY KEY(id) PRIMARY KEY(id)
); );
''').then((result) { ''').then((result) {
//print(result.affectedRows);
_log.fine('Check and create "migrations" table'); _log.fine('Check and create "migrations" table');
}).catchError((e) { }).catchError((e) {
//print(e);
_log.severe('Failed to create "migrations" table.', e); _log.severe('Failed to create "migrations" table.', e);
}); });
} }
@ -59,7 +57,10 @@ class MariaDbMigrationRunner implements MigrationRunner {
var existing = <String>[]; var existing = <String>[];
if (result.isNotEmpty) { if (result.isNotEmpty) {
existing = result.expand((x) => x).cast<String>().toList(); var pathList = result.expand((x) => x).cast<String>().toList();
for (var path in pathList) {
existing.add(path.replaceAll("\\", "\\\\"));
}
} }
var toRun = <String>[]; var toRun = <String>[];
@ -121,7 +122,10 @@ class MariaDbMigrationRunner implements MigrationRunner {
.query('SELECT path from migrations WHERE batch = $curBatch;'); .query('SELECT path from migrations WHERE batch = $curBatch;');
var existing = <String>[]; var existing = <String>[];
if (result.isNotEmpty) { if (result.isNotEmpty) {
existing = result.expand((x) => x).cast<String>().toList(); var pathList = result.expand((x) => x).cast<String>().toList();
for (var path in pathList) {
existing.add(path.replaceAll("\\", "\\\\"));
}
} }
var toRun = <String>[]; var toRun = <String>[];
@ -153,7 +157,10 @@ class MariaDbMigrationRunner implements MigrationRunner {
.query('SELECT path from migrations ORDER BY batch DESC;'); .query('SELECT path from migrations ORDER BY batch DESC;');
var existing = <String>[]; var existing = <String>[];
if (r.isNotEmpty) { if (r.isNotEmpty) {
existing = r.expand((x) => x).cast<String>().toList(); var pathList = r.expand((x) => x).cast<String>().toList();
for (var path in pathList) {
existing.add(path.replaceAll("\\", "\\\\"));
}
} }
var toRun = existing.where(migrations.containsKey).toList(); var toRun = existing.where(migrations.containsKey).toList();

View file

@ -1,5 +1,5 @@
name: angel3_migration_runner name: angel3_migration_runner
version: 8.2.1 version: 8.2.2
description: The implementation of database migration for Angel3 framework. Designed to work with Angel3 ORM. description: The implementation of database migration for Angel3 framework. Designed to work with Angel3 ORM.
homepage: https://angel3-framework.web.app/ homepage: https://angel3-framework.web.app/
repository: https://github.com/dart-backend/angel/tree/master/packages/orm/angel_migration_runner repository: https://github.com/dart-backend/angel/tree/master/packages/orm/angel_migration_runner

View file

@ -37,10 +37,15 @@ void main() async {
); );
}); });
group('MariaDB', () { group('MariaDB migrate tables', () {
test('migrate tables', () async { test('up', () async {
print("Test migration up"); print("Test migration up");
runner.up(); await runner.up();
});
test('reset', () async {
print("Test migration reset");
await runner.reset();
}); });
}); });