Skip to content

Commit

Permalink
fix: sql server migration
Browse files Browse the repository at this point in the history
  • Loading branch information
WatheqAlshowaiter committed Aug 22, 2024
1 parent f91b234 commit e722341
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ public function up(): void
$table->string('status')->storedAs("CASE WHEN active THEN 'Active' ELSE 'Inactive' END");
}

if ((float) App::version() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT && DB::getDriverName() == 'sqlsrv') {
$table->string('full_name')->persisted("CONCAT(first_name, ' ', last_name)");
}

$table->timestamps(); // created_at, updated_at => ignored because they are nullable
});

if ((float)App::version() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT && DB::getDriverName() == 'sqlsrv') {
DB::statement(/**@lang TSQL */ "
ALTER TABLE fathers
ADD full_name AS CONCAT(first_name, ' ', last_name) PERSISTED;
");
}
}

public function down(): void
Expand Down
10 changes: 5 additions & 5 deletions src/BackupTablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ function restoreTable($tableName, $backupName)
protected function backupTablesForSqlite($newTableName, $table)
{
// Step 1: Create the new table structure, excluding generated columns
DB::statement(/**@lang SQLite* */ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");
DB::statement(/**@lang SQLite */ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");

//$allColumns = DB::selectOne(/**@lang SQLite* */ "select * from $table");

DB::statement(/**@lang SQLite* */ "INSERT INTO $newTableName SELECT * FROM $table");
DB::statement(/**@lang SQLite */ "INSERT INTO $newTableName SELECT * FROM $table");

$newCreatedTables[] = $newTableName;
$response[] = " Table '$table' cloned successfully.";
Expand All @@ -121,7 +121,7 @@ protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): arr
{
logger('mariadb');

DB::statement(/**@lang MySQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table");
DB::statement(/**@lang MySQL*/ "CREATE TABLE $newTableName AS SELECT * FROM $table");

$newCreatedTables[] = $newTableName;
$response[] = " Table '$table' cloned successfully.";
Expand All @@ -134,7 +134,7 @@ protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): arr

protected function backupTablesForForPostgres($newTableName, $table)
{
DB::statement(/**@lang PostgreSQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table");
DB::statement(/**@lang PostgreSQL*/ "CREATE TABLE $newTableName AS SELECT * FROM $table");

$newCreatedTables[] = $newTableName;
$response[] = " Table '$table' cloned successfully.";
Expand All @@ -147,7 +147,7 @@ protected function backupTablesForForPostgres($newTableName, $table)

protected function backupTablesForForSqlServer($newTableName, $table)
{
DB::statement("SELECT * INTO $newTableName FROM $table");
DB::statement(/**@lang TSQL*/"SELECT * INTO $newTableName FROM $table");

$newCreatedTables[] = $newTableName;
$response[] = " Table '$table' cloned successfully.";
Expand Down

0 comments on commit e722341

Please sign in to comment.