From 83a562cc6dde59816ba09f30d1f24a253986a0eb Mon Sep 17 00:00:00 2001 From: necessarylion Date: Sun, 31 Dec 2023 00:43:00 +0630 Subject: [PATCH] update schema for mysql and update tests --- .github/workflows/test.yaml | 6 ++++- melos.yaml | 17 +++++++++++- .../lib/src/schema/table.dart | 9 +++++-- .../lib/src/schema/table.update.dart | 23 ++++++++++++---- .../test/belongs_to_test.dart | 3 +-- .../dox-query-builder/test/connection.dart | 26 ++++++++++++++++++- .../dox-query-builder/test/has_many_test.dart | 3 +-- .../dox-query-builder/test/has_one_test.dart | 3 +-- .../test/many_to_many_test.dart | 2 +- .../test/model_custom_table_name_test.dart | 5 ++-- .../dox-query-builder/test/model_test.dart | 5 ++-- .../dox-query-builder/test/schema_test.dart | 3 +-- .../test/sql_query_builder_test.dart | 13 +++++++--- 13 files changed, 91 insertions(+), 27 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0353d2c..3af6f39 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -42,4 +42,8 @@ jobs: - name: Melos Bootstrap run: melos bs - name: Run Tests - run: melos test + run: melos test --no-select + - name: Run Test (query builder postgres) + run: melos test_query_builder_postgres + - name: Run Test (query builder mysql) + run: melos test_query_builder_mysql diff --git a/melos.yaml b/melos.yaml index 55e9b4c..d14cd28 100644 --- a/melos.yaml +++ b/melos.yaml @@ -4,5 +4,20 @@ packages: - packages/** scripts: + test: - exec: dart test --concurrency=1 \ No newline at end of file + exec: dart test --concurrency=1 + packageFilters: + ignore: "dox_query_builder" + + test_query_builder_postgres: + exec: DRIVER=postgres dart test --concurrency=1 + packageFilters: + noSelect: true + scope: "dox_query_builder" + + test_query_builder_mysql: + exec: DRIVER=mysql dart test --concurrency=1 + packageFilters: + noSelect: true + scope: "dox_query_builder" \ No newline at end of file diff --git a/packages/dox-query-builder/lib/src/schema/table.dart b/packages/dox-query-builder/lib/src/schema/table.dart index 09617ae..83d58ec 100644 --- a/packages/dox-query-builder/lib/src/schema/table.dart +++ b/packages/dox-query-builder/lib/src/schema/table.dart @@ -65,7 +65,10 @@ class Table with TableUpdate { } TableColumn money(String name) { - TableColumn col = TableColumn(name: name, type: "MONEY"); + TableColumn col = TableColumn( + name: name, + type: dbDriver.getName() == Driver.mysql ? "integer" : "MONEY", + ); columns.add(col); return col; } @@ -77,7 +80,9 @@ class Table with TableUpdate { } TableColumn jsonb(String name) { - TableColumn col = TableColumn(name: name, type: "JSONB"); + TableColumn col = TableColumn( + name: name, + type: dbDriver.getName() == Driver.mysql ? "JSON" : 'JSONB'); columns.add(col); return col; } diff --git a/packages/dox-query-builder/lib/src/schema/table.update.dart b/packages/dox-query-builder/lib/src/schema/table.update.dart index 5ff8862..bf74102 100644 --- a/packages/dox-query-builder/lib/src/schema/table.update.dart +++ b/packages/dox-query-builder/lib/src/schema/table.update.dart @@ -1,3 +1,5 @@ +import 'package:dox_query_builder/dox_query_builder.dart'; + import 'table.column.dart'; import 'table.shared_mixin.dart'; @@ -44,7 +46,11 @@ mixin TableUpdate implements TableSharedMixin { /// changing type if (col.type != null) { - queries.add("ALTER COLUMN ${col.name} TYPE ${col.type}"); + if (dbDriver.getName() == Driver.mysql) { + queries.add("MODIFY COLUMN ${col.name} ${col.type}"); + } else { + queries.add("ALTER COLUMN ${col.name} TYPE ${col.type}"); + } } /// changing default value @@ -53,8 +59,13 @@ mixin TableUpdate implements TableSharedMixin { } /// set null - queries.add( - "ALTER COLUMN ${col.name} ${col.isNullable ? 'DROP NOT NULL' : 'SET NOT NULL'}"); + if (dbDriver.getName() == Driver.mysql) { + queries.add( + "MODIFY COLUMN ${col.name} ${col.isNullable ? 'NULL' : 'NOT NULL'}"); + } else { + queries.add( + "ALTER COLUMN ${col.name} ${col.isNullable ? 'DROP NOT NULL' : 'SET NOT NULL'}"); + } /// set unique if (col.isUnique) { @@ -62,8 +73,10 @@ mixin TableUpdate implements TableSharedMixin { } /// run final query - String query = "ALTER TABLE $tableName ${queries.join(',')}"; - return await _runQuery(query); + for (String q in queries) { + String query = "ALTER TABLE $tableName $q"; + return await _runQuery(query); + } } Future _runQuery(String query) async { diff --git a/packages/dox-query-builder/test/belongs_to_test.dart b/packages/dox-query-builder/test/belongs_to_test.dart index 116b23a..84ad553 100644 --- a/packages/dox-query-builder/test/belongs_to_test.dart +++ b/packages/dox-query-builder/test/belongs_to_test.dart @@ -6,11 +6,10 @@ import 'models/blog/blog.model.dart'; import 'models/blog_info/blog_info.model.dart'; void main() async { - SqlQueryBuilder.initialize(database: await poolConnection()); + await initQueryBuilder(); group('Belongs To |', () { setUp(() async { - SqlQueryBuilder.initialize(database: await poolConnection()); await Schema.create('blog', (Table table) { table.id('uid'); table.string('title'); diff --git a/packages/dox-query-builder/test/connection.dart b/packages/dox-query-builder/test/connection.dart index ad8c869..1cc333f 100644 --- a/packages/dox-query-builder/test/connection.dart +++ b/packages/dox-query-builder/test/connection.dart @@ -1,5 +1,29 @@ +import 'dart:io'; + +import 'package:dox_query_builder/dox_query_builder.dart'; + +import 'mysql.dart'; import 'postgres.dart'; +Future initQueryBuilder() async { + SqlQueryBuilder.initialize( + database: await poolConnection(), + driver: getDriver(), + ); +} + Future poolConnection() { - return postgresConnection(); + if (Platform.environment['DRIVER'] == 'postgres') { + return postgresConnection(); + } else { + return mysqlConnection(); + } +} + +Driver getDriver() { + if (Platform.environment['DRIVER'] == 'postgres') { + return Driver.postgres; + } else { + return Driver.mysql; + } } diff --git a/packages/dox-query-builder/test/has_many_test.dart b/packages/dox-query-builder/test/has_many_test.dart index 67f98d4..8e8f110 100644 --- a/packages/dox-query-builder/test/has_many_test.dart +++ b/packages/dox-query-builder/test/has_many_test.dart @@ -6,11 +6,10 @@ import 'models/blog/blog.model.dart'; import 'models/blog_info/blog_info.model.dart'; void main() async { - SqlQueryBuilder.initialize(database: await poolConnection()); + await initQueryBuilder(); group('Has One |', () { setUp(() async { - SqlQueryBuilder.initialize(database: await poolConnection()); await Schema.create('blog', (Table table) { table.id('uid'); table.string('title'); diff --git a/packages/dox-query-builder/test/has_one_test.dart b/packages/dox-query-builder/test/has_one_test.dart index 8a13ee2..3176bc1 100644 --- a/packages/dox-query-builder/test/has_one_test.dart +++ b/packages/dox-query-builder/test/has_one_test.dart @@ -6,11 +6,10 @@ import 'models/blog/blog.model.dart'; import 'models/blog_info/blog_info.model.dart'; void main() async { - SqlQueryBuilder.initialize(database: await poolConnection()); + await initQueryBuilder(); group('Has One |', () { setUp(() async { - SqlQueryBuilder.initialize(database: await poolConnection()); await Schema.create('blog', (Table table) { table.id('uid'); table.string('title'); diff --git a/packages/dox-query-builder/test/many_to_many_test.dart b/packages/dox-query-builder/test/many_to_many_test.dart index 66f7fd8..227c1f0 100644 --- a/packages/dox-query-builder/test/many_to_many_test.dart +++ b/packages/dox-query-builder/test/many_to_many_test.dart @@ -7,7 +7,7 @@ import 'models/artist_song/artist_song.model.dart'; import 'models/song/song.model.dart'; void main() async { - SqlQueryBuilder.initialize(database: await poolConnection()); + await initQueryBuilder(); group('Many To Many |', () { setUp(() async { diff --git a/packages/dox-query-builder/test/model_custom_table_name_test.dart b/packages/dox-query-builder/test/model_custom_table_name_test.dart index f61498c..6e3e303 100644 --- a/packages/dox-query-builder/test/model_custom_table_name_test.dart +++ b/packages/dox-query-builder/test/model_custom_table_name_test.dart @@ -4,10 +4,11 @@ import 'package:test/test.dart'; import 'connection.dart'; import 'models/user/user.model.dart'; -void main() { +void main() async { + await initQueryBuilder(); + group('Model with custom table name', () { setUp(() async { - SqlQueryBuilder.initialize(database: await poolConnection()); await Schema.create('users', (Table table) { table.id(); table.string('name'); diff --git a/packages/dox-query-builder/test/model_test.dart b/packages/dox-query-builder/test/model_test.dart index 1cfcdae..a499ef9 100644 --- a/packages/dox-query-builder/test/model_test.dart +++ b/packages/dox-query-builder/test/model_test.dart @@ -5,10 +5,11 @@ import 'connection.dart'; import 'models/blog/blog.model.dart'; import 'models/blog_info/blog_info.model.dart'; -void main() { +void main() async { + await initQueryBuilder(); + group('Model |', () { setUp(() async { - SqlQueryBuilder.initialize(database: await poolConnection()); await Schema.create('blog', (Table table) { table.id('uid'); table.string('title'); diff --git a/packages/dox-query-builder/test/schema_test.dart b/packages/dox-query-builder/test/schema_test.dart index 693b809..4869b03 100644 --- a/packages/dox-query-builder/test/schema_test.dart +++ b/packages/dox-query-builder/test/schema_test.dart @@ -4,11 +4,10 @@ import 'package:test/test.dart'; import 'connection.dart'; void main() async { - SqlQueryBuilder.initialize(database: await poolConnection()); + await initQueryBuilder(); group('Schema |', () { setUp(() async { - SqlQueryBuilder.initialize(database: await poolConnection()); await Schema.create('schema_test_table', (Table table) { table.id(); table.uuid('uuid'); diff --git a/packages/dox-query-builder/test/sql_query_builder_test.dart b/packages/dox-query-builder/test/sql_query_builder_test.dart index f22573a..dd0d8b3 100644 --- a/packages/dox-query-builder/test/sql_query_builder_test.dart +++ b/packages/dox-query-builder/test/sql_query_builder_test.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:dox_query_builder/dox_query_builder.dart'; +import 'package:mysql1/mysql1.dart'; import 'package:postgres/postgres.dart'; import 'package:test/test.dart'; @@ -9,7 +10,7 @@ import 'models/blog/blog.model.dart'; import 'models/blog_info/blog_info.model.dart'; void main() async { - SqlQueryBuilder.initialize(database: await poolConnection()); + await initQueryBuilder(); group('Query Builder', () { setUp(() async { @@ -131,9 +132,13 @@ void main() async { blog.description = 'Awesome blog body'; await blog.save(); - Result b = await QueryBuilder.query('select * from blog'); - - expect(b.first.toColumnMap()['title'], 'Awesome blog'); + if (getDriver() == Driver.postgres) { + Result b = await QueryBuilder.query('select * from blog'); + expect(b.first.toColumnMap()['title'], 'Awesome blog'); + } else { + Results b = await QueryBuilder.query('select * from blog'); + expect(b.first.fields['title'], 'Awesome blog'); + } }); test('group by', () async {