| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- use Illuminate\Database\Migrations\Migration;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Support\Facades\DB;
- return new class extends Migration {
- public function up(): void
- {
- Schema::create('recharge_bonuses', function (Blueprint $t) {
- $t->string('channel_type', 100)->primary(); $t->decimal('bonus_percent', 5, 2)->default(0);
- $t->json('translations'); $t->boolean('enabled')->default(false); $t->timestamps();
- });
- Schema::create('recharge_bonus_rewards', function (Blueprint $t) {
- $t->id(); $t->string('source_type', 24); $t->unsignedBigInteger('source_id'); $t->string('dedup_key', 64)->unique();
- $t->string('member_id', 100); $t->string('channel_type', 100); $t->unsignedBigInteger('principal_log_id');
- $t->decimal('principal_amount', 24, 10); $t->decimal('bonus_percent', 5, 2); $t->decimal('amount', 24, 2);
- $t->unsignedBigInteger('credited_log_id')->nullable(); $t->json('rule_snapshot'); $t->timestamps();
- $t->unique(['source_type', 'source_id'], 'recharge_bonus_source');
- });
- Schema::create('recharge_bonus_chain_receipts', function (Blueprint $t) {
- $t->string('dedup_key', 64)->primary(); $t->unsignedBigInteger('recharge_id')->nullable();
- $t->unsignedBigInteger('credited_log_id')->nullable(); $t->timestamps();
- });
- foreach (['payment_orders', 'recharges'] as $table) {
- Schema::table($table, function (Blueprint $t) { $t->json('recharge_bonus_snapshot')->nullable(); });
- }
- }
- public function down(): void
- {
- if (DB::table('recharge_bonus_rewards')->exists() || DB::table('recharge_bonus_chain_receipts')->exists()
- || DB::table('payment_orders')->whereNotNull('recharge_bonus_snapshot')->exists()
- || DB::table('recharges')->whereNotNull('recharge_bonus_snapshot')->exists()) {
- throw new \RuntimeException('已有充值赠送快照或记录,不能回滚此迁移');
- }
- foreach (['payment_orders', 'recharges'] as $table) Schema::table($table, function (Blueprint $t) { $t->dropColumn('recharge_bonus_snapshot'); });
- foreach (['recharge_bonus_rewards', 'recharge_bonus_chain_receipts', 'recharge_bonuses'] as $table) Schema::dropIfExists($table);
- }
- };
|