Skip to content

Commit

Permalink
docs: 添加迁移事物,防止迁移失败导致数据库结构被破坏
Browse files Browse the repository at this point in the history
  • Loading branch information
gua committed Nov 17, 2023
1 parent 80d7379 commit 19dbabf
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions app/Console/Commands/MigrateFromV2b.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,33 @@ public function handle()
}

if (array_key_exists($version, $sqlCommands)) {
foreach ($sqlCommands[$version] as $sqlCommand) {
// Execute SQL command
\DB::statement($sqlCommand);
\DB::beginTransaction();

try {
foreach ($sqlCommands[$version] as $sqlCommand) {
// Execute SQL command
\DB::statement($sqlCommand);
}

$this->info('1️⃣、数据库差异矫正成功');

// 初始化数据库迁移
$this->call('db:seed', ['--class' => 'OriginV2bMigrationsTableSeeder']);
$this->info('2️⃣、数据库迁移记录初始化成功');

$this->call('xboard:update');
$this->info('3️⃣、更新成功');

$this->info("🎉:成功从 $version 迁移到Xboard");

\DB::commit();
} catch (\Exception $e) {
// An error occurred, rollback the transaction
\DB::rollback();
$this->error('迁移失败'. $e->getMessage() );
}
$this->info('1️⃣、数据库差异矫正成功');
// 初始化数据库迁移
$this->call('db:seed', [ '--class' => 'OriginV2bMigrationsTableSeeder' ]);
$this->info('2️⃣、数据库迁移记录初始化成功');
$this->call('xboard:update');
$this->info('3️⃣、更新成功');
$this->info("🎉:成功从 $version 迁移到Xboard");


} else {
$this->error("你所输入的版本未找到");
}
Expand Down

0 comments on commit 19dbabf

Please sign in to comment.