Skip to content

Commit

Permalink
feature: split migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 11, 2023
1 parent 9cf6b2b commit 8ab44c9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Migration/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gt\Database\Database;
use Gt\Database\Connection\Settings;
use Gt\Database\DatabaseException;
use PHPSQLParser\lexer\PHPSQLLexer;
use SplFileInfo;
use SplFileObject;

Expand Down Expand Up @@ -200,11 +201,30 @@ public function performMigration(

$this->output("Migration $fileNumber: `$file`.");

$sql = file_get_contents($file);
$totalSql = file_get_contents($file);
$md5 = md5_file($file);

$lexer = new PHPSQLLexer();
$splitSqlQueryList = [];
$currentQuery = "";
foreach($lexer->split($totalSql) as $token) {
if($token === ";") {
array_push($splitSqlQueryList, $currentQuery);
$currentQuery = "";
continue;
}

$currentQuery .= $token;
}
if($currentQuery) {
array_push($splitSqlQueryList, $currentQuery);
}

try {
$this->dbClient->executeSql($sql);
foreach($splitSqlQueryList as $sql) {
$this->dbClient->executeSql($sql);
}

$this->recordMigrationSuccess($fileNumber, $md5);
}
catch(DatabaseException $exception) {
Expand Down

0 comments on commit 8ab44c9

Please sign in to comment.