Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mysql database driver #16

Merged
merged 50 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
513682b
add string types to mysql driver table blueprint
codekeyz Dec 22, 2023
aca283a
tiny fix
codekeyz Dec 22, 2023
ad92753
add numeric types for mysql_driver
codekeyz Dec 22, 2023
cdd3b4f
add more types to table blueprint
codekeyz Dec 22, 2023
cb2dd11
add more date time types
codekeyz Dec 22, 2023
82080a2
add tests for mysql driver
codekeyz Dec 22, 2023
8f9910f
should use mysqldriver for mariadb connections too
codekeyz Dec 22, 2023
fcb6731
implement all interfaces for mysql & mariadb driver
codekeyz Dec 22, 2023
f7a8d8e
make driver only needed at the point of execution
codekeyz Dec 22, 2023
b184c08
rework usage of Query.query
codekeyz Dec 22, 2023
5f7a1bf
use drivable mixin instead of driver
codekeyz Dec 22, 2023
b7f9c1a
fix migration delete statement
codekeyz Dec 22, 2023
0a82755
tiny fix
codekeyz Dec 22, 2023
07d1b15
refactor tests for mysql & sqlite
codekeyz Dec 23, 2023
7cb5882
improve tests
codekeyz Dec 23, 2023
64508f2
tiny fix:
codekeyz Dec 23, 2023
9343ed5
restore ci config
codekeyz Dec 23, 2023
1f9d164
tiny fix
codekeyz Dec 23, 2023
208f2b4
fix interface for update, insert & delete
codekeyz Dec 23, 2023
fbdd895
tiny fix
codekeyz Dec 23, 2023
d559dca
add more tests
codekeyz Dec 23, 2023
f424a4b
add more tests
codekeyz Dec 23, 2023
19348a4
add base integration test for all drivers
codekeyz Dec 23, 2023
50ed918
add more integration tests
codekeyz Dec 23, 2023
0af56af
tiny fix
codekeyz Dec 23, 2023
813b407
remove unnessary code in where
codekeyz Dec 23, 2023
f657205
add more integration tests
codekeyz Dec 23, 2023
5cb35d5
add test for dropping tables after integration tests
codekeyz Dec 23, 2023
a7990de
add integration tests for mysql
codekeyz Dec 23, 2023
b784bc5
setup mariadb database for integration tests
codekeyz Dec 23, 2023
8d2d470
tiny fix
codekeyz Dec 23, 2023
0dbf245
use connection :secure false for mariadb
codekeyz Dec 23, 2023
009b584
fix tests for mariadb
codekeyz Dec 23, 2023
a786f7d
tiny fix
codekeyz Dec 23, 2023
5805765
tiny fix
codekeyz Dec 23, 2023
9c82303
exclude e2e tests from dart test
codekeyz Dec 23, 2023
39bf7d0
make username & password only required when :secure
codekeyz Dec 23, 2023
29fbcd6
tiny fix
codekeyz Dec 23, 2023
2c8539e
use root as username
codekeyz Dec 23, 2023
d8d7538
tiny fix
codekeyz Dec 23, 2023
25441e1
use github runner as username
codekeyz Dec 23, 2023
2964086
tiny fix
codekeyz Dec 23, 2023
4955d1e
use password for mariadb
codekeyz Dec 23, 2023
0264b33
tiny fix
codekeyz Dec 23, 2023
52c8166
debug
codekeyz Dec 23, 2023
4e04bbd
tiny fix
codekeyz Dec 23, 2023
eee2ebc
test fix
codekeyz Dec 23, 2023
3ec7b4a
improve tests
codekeyz Dec 23, 2023
b50a436
tiny oversight
codekeyz Dec 23, 2023
ab775dd
add test for disconnecting from db
codekeyz Dec 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,24 @@ jobs:
melos exec -- "dart run build_runner build"

- name: Run Unit tests
run: melos tests:ci
run: melos tests:unit


- name: Setup MariaDB For Integration Test
uses: ankane/setup-mariadb@v1
with:
mariadb-version: "10.11"
database: "maria_test_db"


- name: Run Integration tests
run: |
sudo mysql -e "ALTER USER '$USER'@'localhost' IDENTIFIED BY 'password'; FLUSH PRIVILEGES;"
melos tests:integration

- name: Combine Coverage
run: melos coverage


- name: Upload Coverage
uses: codecov/codecov-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

**/melos_**
**/coverage
**.reflectable.dart
**.reflectable.dart
11 changes: 9 additions & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ scripts:
run: |
melos exec -c 1 -- "dart test" --fail-fast

tests:ci:
tests:unit:
run: |
melos exec -c 1 -- "dart test --coverage=coverage --exclude-tags integration" --fail-fast

tests:integration:
run: |
melos exec -c 1 -- "dart test --coverage=coverage --tags integration" --fail-fast

coverage:
run: |
melos exec -c 1 -- "dart test --coverage=coverage" --fail-fast
melos exec -- "dart pub global run coverage:format_coverage --check-ignore --report-on=lib --lcov -o "$MELOS_ROOT_PATH/coverage/$(echo "\$MELOS_PACKAGE_NAME")_lcov.info" -i ./coverage"
find $MELOS_ROOT_PATH/coverage -type f -empty -print -delete

Expand Down
2 changes: 1 addition & 1 deletion yaroo/lib/db/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

UseDatabaseConnection(this.name) : _driver = DB.driver(name);

Query query(String table) => Query.query(table, _driver);
Query query(String table) => Query.table(table).driver(_driver);

Check warning on line 14 in yaroo/lib/db/db.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/db/db.dart#L14

Added line #L14 was not covered by tests
}

class DB {
Expand Down
20 changes: 10 additions & 10 deletions yaroo/lib/db/migration/_migrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class Migrator {
transactor.execute(serialized);
}

transactor.insert(Migrator.tableName, {'migration': fileName, 'batch': batchNos});
await Query.table(Migrator.tableName)
.driver(transactor)
.insert({'migration': fileName, 'batch': batchNos}).exec();

await transactor.commit();

Expand All @@ -61,15 +63,15 @@ class Migrator {
static Future<void> resetMigrations(DatabaseDriver driver, Iterable<MigrationTask> allTasks) async {
await ensureMigrationsTableReady(driver);

final migrationInfoFromDB = await Query.query(Migrator.tableName, driver).orderByDesc('batch').all();
final migrationInfoFromDB = await Query.table(Migrator.tableName).driver(driver).orderByDesc('batch').all();
if (migrationInfoFromDB.isEmpty) {
print('𐄂 skipped: reason: no migrations to reset');
return;
}

print('------- Resetting migrations 📦 -------\n');

final Iterable<Rollback> rollbacks = migrationInfoFromDB.map((e) => _MigrationDbData.from(e)).map((e) {
final rollbacks = migrationInfoFromDB.map((e) => _MigrationDbData.from(e)).map<Rollback>((e) {
final found = allTasks.firstWhereOrNull((m) => m.name == e.migration);
return (batch: e.batch, name: e.migration, migration: found);
}).whereNotNull();
Expand All @@ -80,7 +82,7 @@ class Migrator {
}

static Future<void> rollBackMigration(DatabaseDriver driver, Iterable<MigrationTask> allTasks) async {
final lastBatch = await Query.query(Migrator.tableName, driver).orderByDesc('batch').get();
final lastBatch = await Query.table(Migrator.tableName).driver(driver).orderByDesc('batch').get();
if (lastBatch == null) {
print('𐄂 skipped: reason: no migration to rollback');
return;
Expand All @@ -107,12 +109,10 @@ class Migrator {
schemas.forEach((e) => transactor.execute(e.toScript(driver.blueprint)));
}

final deleteSql = DeleteQuery(
Migrator.tableName,
driver,
whereClause: Query.query(Migrator.tableName, driver).where('migration', '=', rollback.name),
).statement;
transactor.execute(deleteSql);
await Query.table(Migrator.tableName)
.driver(transactor)
.delete((where) => where.where('migration', '=', rollback.name))
.exec();

await transactor.commit();
});
Expand Down
2 changes: 1 addition & 1 deletion yaroo/lib/db/migration/_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Future<void> ensureMigrationsTableReady(DatabaseDriver driver) async {
}

Future<bool> hasAlreadyMigratedScript(String scriptName, DatabaseDriver driver) async {
final result = await Query.query(Migrator.tableName, driver).where('migration', '=', scriptName).findOne();
final result = await Query.table(Migrator.tableName).driver(driver).where('migration', '=', scriptName).findOne();
return result != null;
}

Expand Down
5 changes: 5 additions & 0 deletions yaroo/test/integration_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@Skip('integration tests are not ready yet')
@Tags(['integration'])
import 'package:spookie/spookie.dart';

void main() {}
1 change: 1 addition & 0 deletions yaroorm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
*.sqlite
51 changes: 24 additions & 27 deletions yaroorm/lib/src/database/driver/driver.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import 'package:yaroorm/src/database/driver/mysql_driver.dart';

import '../../query/primitives/serializer.dart';
import '../../query/query.dart';
import '../migration.dart';
import 'sqlite_driver.dart';

enum DatabaseDriverType { sqlite, pgsql, mongo }
enum DatabaseDriverType { sqlite, pgsql, mysql, mariadb }

String wrapString(String value) => "'$value'";

class DatabaseConnection {
final String name;
final String? url;
final String? host;
final String? port;
final int? port;
final String? username;
final String? password;
final String database;
Expand Down Expand Up @@ -39,7 +43,7 @@
charset: connInfo['charset'],
collation: connInfo['collation'],
host: connInfo['host'],
port: connInfo['port'],
port: connInfo['port'] == null ? null : int.parse(connInfo['port']),
password: connInfo['password'],
username: connInfo['username'],
url: connInfo['url'],
Expand All @@ -53,7 +57,8 @@
return switch (value) {
'sqlite' => DatabaseDriverType.sqlite,
'pgsql' => DatabaseDriverType.pgsql,
'mongo' => DatabaseDriverType.mongo,
'mysql' => DatabaseDriverType.mysql,
'mariadb' => DatabaseDriverType.mariadb,
null => throw ArgumentError.notNull('Database Driver'),
_ => throw ArgumentError.value(value, null, 'Invalid Database Driver provided in configuration')
};
Expand All @@ -69,28 +74,25 @@
/// Execute scripts on the database.
///
/// Execution varies across drivers
Future<void> execute(String script);
Future<dynamic> execute(String script);

/// Perform update on the database
update(UpdateQuery query);
Future<void> update(UpdateQuery query);

/// Perform delete on the database
delete(DeleteQuery query);
Future<void> delete(DeleteQuery query);

/// Perform insert on the database
Future<dynamic> insert(InsertQuery query);

insert(String tableName, Map<String, dynamic> data);
/// Perform insert on the database
Future<dynamic> insertMany(InsertManyQuery query);

PrimitiveSerializer get serializer;
}

abstract class DriverTransactor with DriverAble {
Future<List<Object?>> commit();

@override
void insert(String tableName, Map<String, dynamic> data);

@override
void update(UpdateQuery query);

@override
void delete(DeleteQuery query);
}

abstract interface class DatabaseDriver with DriverAble {
Expand All @@ -99,6 +101,9 @@
switch (driver) {
case DatabaseDriverType.sqlite:
return SqliteDriver(dbConn);
case DatabaseDriverType.mariadb:
case DatabaseDriverType.mysql:

Check warning on line 105 in yaroorm/lib/src/database/driver/driver.dart

View check run for this annotation

Codecov / codecov/patch

yaroorm/lib/src/database/driver/driver.dart#L105

Added line #L105 was not covered by tests
return MySqlDriver(dbConn, driver);
default:
throw ArgumentError.value(driver, null, 'Driver not yet supported');
}
Expand All @@ -113,7 +118,7 @@
/// Performs connection to the database.
///
/// Depend on driver type it may create a connection pool.
Future<DatabaseDriver> connect();
Future<DatabaseDriver> connect({int? maxConnections, bool? singleConnection, bool? secure});

/// Performs connection to the database.
///
Expand All @@ -124,17 +129,9 @@
Future<bool> hasTable(String tableName);

@override
Future<int> insert(String tableName, Map<String, dynamic> data);

@override
Future<List<Map<String, dynamic>>> update(UpdateQuery query);

@override
Future<List<Map<String, dynamic>>> delete(DeleteQuery query);
Future<int> insert(InsertQuery query);

TableBlueprint get blueprint;

PrimitiveSerializer get serializer;

Future<void> transaction(void Function(DriverTransactor transactor) transaction);
}
Loading