diff options
| -rw-r--r-- | app/DB.php | 19 | ||||
| -rw-r--r-- | app/Schema/SeedGedcomTable.php | 29 | ||||
| -rw-r--r-- | app/Schema/SeedUserTable.php | 35 |
3 files changed, 36 insertions, 47 deletions
diff --git a/app/DB.php b/app/DB.php index d2f4d4745a..e872a21eee 100644 --- a/app/DB.php +++ b/app/DB.php @@ -19,6 +19,7 @@ declare(strict_types=1); namespace Fisharebest\Webtrees; +use Closure; use Illuminate\Database\Capsule\Manager; use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Expression; @@ -167,6 +168,24 @@ class DB extends Manager return parent::connection()->getTablePrefix() . $identifier; } + /** + * SQL-Server needs to be told that we are going to insert into an identity column. + * + * @param Closure(): void $callback + */ + public static function identityInsert(string $table, Closure $callback): void + { + if (self::driverName() === self::SQL_SERVER) { + self::exec('SET IDENTITY_INSERT [' . self::prefix(identifier: $table) . '] ON'); + } + + $callback(); + + if (self::driverName() === self::SQL_SERVER) { + self::exec('SET IDENTITY_INSERT [' . self::prefix(identifier: $table) . '] OFF'); + } + } + public static function rollBack(): void { parent::connection()->rollBack(); diff --git a/app/Schema/SeedGedcomTable.php b/app/Schema/SeedGedcomTable.php index 3448fa0ce1..860b18cbb2 100644 --- a/app/Schema/SeedGedcomTable.php +++ b/app/Schema/SeedGedcomTable.php @@ -21,32 +21,17 @@ namespace Fisharebest\Webtrees\Schema; use Fisharebest\Webtrees\DB; -/** - * Populate the gedcom table - */ class SeedGedcomTable implements SeedInterface { - /** - * Run the seeder. - * - * @return void - */ public function run(): void { // Add a "default" tree, to store default settings - - if (DB::driverName() === DB::SQL_SERVER) { - DB::exec('SET IDENTITY_INSERT [' . DB::prefix() . 'gedcom] ON'); - } - - DB::table('gedcom')->updateOrInsert([ - 'gedcom_id' => -1, - ], [ - 'gedcom_name' => 'DEFAULT_TREE', - ]); - - if (DB::driverName() === DB::SQL_SERVER) { - DB::exec('SET IDENTITY_INSERT [' . DB::prefix() . 'gedcom] OFF'); - } + DB::identityInsert(table: 'gedcom', callback: static function (): void { + DB::table(table: 'gedcom')->updateOrInsert(attributes: [ + 'gedcom_id' => -1, + ], values: [ + 'gedcom_name' => 'DEFAULT_TREE', + ]); + }); } } diff --git a/app/Schema/SeedUserTable.php b/app/Schema/SeedUserTable.php index 17b147237d..2de18fe606 100644 --- a/app/Schema/SeedUserTable.php +++ b/app/Schema/SeedUserTable.php @@ -21,35 +21,20 @@ namespace Fisharebest\Webtrees\Schema; use Fisharebest\Webtrees\DB; -/** - * Populate the user table - */ class SeedUserTable implements SeedInterface { - /** - * Run the seeder. - * - * @return void - */ public function run(): void { // Add a "default" user, to store default settings - - if (DB::driverName() === DB::SQL_SERVER) { - DB::exec('SET IDENTITY_INSERT [' . DB::prefix() . 'user] ON'); - } - - DB::table('user')->updateOrInsert([ - 'user_id' => -1, - ], [ - 'user_name' => 'DEFAULT_USER', - 'real_name' => 'DEFAULT_USER', - 'email' => 'DEFAULT_USER', - 'password' => 'DEFAULT_USER', - ]); - - if (DB::driverName() === DB::SQL_SERVER) { - DB::exec('SET IDENTITY_INSERT [' . DB::prefix() . 'user] OFF'); - } + DB::identityInsert(table: 'user', callback: static function (): void { + DB::table(table: 'user')->updateOrInsert(attributes: [ + 'user_id' => -1, + ], values: [ + 'user_name' => 'DEFAULT_USER', + 'real_name' => 'DEFAULT_USER', + 'email' => 'DEFAULT_USER', + 'password' => 'DEFAULT_USER', + ]); + }); } } |
