Skip to content

Commit

Permalink
update schema for mysql and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion committed Dec 30, 2023
1 parent 9557189 commit 83a562c
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 27 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 16 additions & 1 deletion melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ packages:
- packages/**

scripts:

test:
exec: dart test --concurrency=1
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"
9 changes: 7 additions & 2 deletions packages/dox-query-builder/lib/src/schema/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
23 changes: 18 additions & 5 deletions packages/dox-query-builder/lib/src/schema/table.update.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:dox_query_builder/dox_query_builder.dart';

import 'table.column.dart';
import 'table.shared_mixin.dart';

Expand Down Expand Up @@ -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
Expand All @@ -53,17 +59,24 @@ 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) {
queries.add("ADD CONSTRAINT unique_${col.name} UNIQUE (${col.name})");
}

/// 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<void> _runQuery(String query) async {
Expand Down
3 changes: 1 addition & 2 deletions packages/dox-query-builder/test/belongs_to_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
26 changes: 25 additions & 1 deletion packages/dox-query-builder/test/connection.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import 'dart:io';

import 'package:dox_query_builder/dox_query_builder.dart';

import 'mysql.dart';
import 'postgres.dart';

Future<void> initQueryBuilder() async {
SqlQueryBuilder.initialize(
database: await poolConnection(),
driver: getDriver(),
);
}

Future<dynamic> 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;
}
}
3 changes: 1 addition & 2 deletions packages/dox-query-builder/test/has_many_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions packages/dox-query-builder/test/has_one_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages/dox-query-builder/test/many_to_many_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
5 changes: 3 additions & 2 deletions packages/dox-query-builder/test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions packages/dox-query-builder/test/schema_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
13 changes: 9 additions & 4 deletions packages/dox-query-builder/test/sql_query_builder_test.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 {
Expand Down Expand Up @@ -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<Results>('select * from blog');
expect(b.first.fields['title'], 'Awesome blog');
}
});

test('group by', () async {
Expand Down

0 comments on commit 83a562c

Please sign in to comment.