Skip to content

Commit

Permalink
Merge pull request #46 from shahi5472/shahi_dev
Browse files Browse the repository at this point in the history
migration file updated
  • Loading branch information
javad-zobeidi committed May 9, 2024
2 parents 8479a43 + fcf7d31 commit 187b319
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions lib/src/database/migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import 'package:vania/vania.dart';

class MigrationConnection {
static final MigrationConnection _singleton = MigrationConnection._internal();

factory MigrationConnection() => _singleton;

MigrationConnection._internal();

Connection? dbConnection;
DatabaseDriver? database;

Future<void> setup(DatabaseConfig databaseConfig) async {
database = databaseConfig.driver;
try {
Expand Down Expand Up @@ -47,8 +51,18 @@ class Migration {
}
}

@mustBeOverridden
@mustCallSuper
Future<void> down() async {
if (MigrationConnection().dbConnection == null) {
print('Database is not defined');
throw 'Database is not defined';
}
}

Future<void> createTable(String name, Function callback) async {
try {
Stopwatch stopwatch = Stopwatch()..start();
final query = StringBuffer();
_tableName = name;
callback();
Expand All @@ -70,8 +84,9 @@ class Migration {
?.execute(query.toString().replaceAll(RegExp(r',\s?\)'), ')'));
}

stopwatch.stop();
print(
' Create $name table....................................\x1B[32mDONE\x1B[0m');
' Create $name table....................................\x1B[32m ${stopwatch.elapsedMilliseconds}ms DONE\x1B[0m');
} catch (e) {
print(e);
exit(0);
Expand All @@ -80,6 +95,7 @@ class Migration {

Future<void> createTableNotExists(String name, Function callback) async {
try {
Stopwatch stopwatch = Stopwatch()..start();
final query = StringBuffer();
_tableName = name;
callback();
Expand All @@ -101,8 +117,9 @@ class Migration {
?.execute(query.toString().replaceAll(RegExp(r',\s?\)'), ')'));
}

stopwatch.stop();
print(
' Create $name table....................................\x1B[32mDONE\x1B[0m');
' Create $name table....................................\x1B[32m ${stopwatch.elapsedMilliseconds}ms DONE\x1B[0m');
} catch (e) {
print(e);
exit(0);
Expand Down Expand Up @@ -153,15 +170,20 @@ class Migration {
}

Future<void> dropIfExists(String name) async {
String query = 'DROP TABLE IF EXISTS "$name";';
try {
String query = 'DROP TABLE IF EXISTS `$name`;';

await MigrationConnection().dbConnection?.execute(query.toString());
print(
' Dropping $name table....................................\x1B[32mDONE\x1B[0m');
await MigrationConnection().dbConnection?.execute(query.toString());
print(
' Dropping $name table....................................\x1B[32mDONE\x1B[0m');
} catch (e) {
print(e);
exit(0);
}
}

Future<void> drop(String name) async {
String query = 'DROP TABLE "$name";';
String query = 'DROP TABLE `$name`;';

await MigrationConnection().dbConnection?.execute(query.toString());
print(
Expand Down Expand Up @@ -949,6 +971,11 @@ class Migration {
);
}

void timeStamps() {
timeStamp("created_at", nullable: true);
timeStamp("updated_at", nullable: true);
}

void timeStamp(
String name, {
bool nullable = false,
Expand Down

0 comments on commit 187b319

Please sign in to comment.