Skip to content

Commit e722341

Browse files
fix: sql server migration
1 parent f91b234 commit e722341

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

database/migrations/2024_07_13_090447_create_fathers_table.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ public function up(): void
3333
$table->string('status')->storedAs("CASE WHEN active THEN 'Active' ELSE 'Inactive' END");
3434
}
3535

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

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

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

4548
public function down(): void

src/BackupTablesService.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ function restoreTable($tableName, $backupName)
102102
protected function backupTablesForSqlite($newTableName, $table)
103103
{
104104
// Step 1: Create the new table structure, excluding generated columns
105-
DB::statement(/**@lang SQLite* */ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");
105+
DB::statement(/**@lang SQLite */ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");
106106

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

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

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

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

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

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

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

148148
protected function backupTablesForForSqlServer($newTableName, $table)
149149
{
150-
DB::statement("SELECT * INTO $newTableName FROM $table");
150+
DB::statement(/**@lang TSQL*/"SELECT * INTO $newTableName FROM $table");
151151

152152
$newCreatedTables[] = $newTableName;
153153
$response[] = " Table '$table' cloned successfully.";

0 commit comments

Comments
 (0)