migrate_water_promotion.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /** Additive schema only. No existing orders, balances or entitlements are changed. */
  4. require dirname(__DIR__) . '/vendor/autoload.php';
  5. \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
  6. \support\App::loadAllConfig(['route']);
  7. use Illuminate\Database\Schema\Blueprint;
  8. use plugin\saiadmin\app\model\water\WaterPromotionSetting;
  9. if (!in_array('--apply', $argv, true)) {
  10. echo "Plan: create three InnoDB promotion tables; insert a disabled policy only. No historical rewards.\n";
  11. exit;
  12. }
  13. $schema = (new WaterPromotionSetting())->getConnection()->getSchemaBuilder();
  14. if (!$schema->hasTable('sa_water_promotion_setting')) {
  15. $schema->create('sa_water_promotion_setting', function (Blueprint $table): void {
  16. $table->engine = 'InnoDB';
  17. $table->unsignedInteger('id')->primary();
  18. $table->boolean('enabled')->default(false);
  19. $table->unsignedBigInteger('sku_id')->nullable();
  20. $table->unsignedInteger('quantity')->default(0);
  21. $table->unsignedBigInteger('min_amount_cents')->default(0);
  22. $table->unsignedInteger('revision')->default(1);
  23. $table->unsignedBigInteger('updated_by')->default(0);
  24. $table->dateTime('updated_at')->nullable();
  25. });
  26. }
  27. if (!$schema->hasTable('sa_water_promotion_code')) {
  28. $schema->create('sa_water_promotion_code', function (Blueprint $table): void {
  29. $table->engine = 'InnoDB';
  30. $table->bigIncrements('id');
  31. $table->unsignedBigInteger('enterprise_id')->unique();
  32. $table->string('code', 24)->unique();
  33. $table->dateTime('created_at');
  34. });
  35. }
  36. if (!$schema->hasTable('sa_water_promotion_referral')) {
  37. $schema->create('sa_water_promotion_referral', function (Blueprint $table): void {
  38. $table->engine = 'InnoDB';
  39. $table->bigIncrements('id');
  40. $table->unsignedBigInteger('inviter_id');
  41. $table->unsignedBigInteger('invited_id')->unique();
  42. $table->unsignedBigInteger('bound_by');
  43. $table->unsignedInteger('policy_revision');
  44. $table->unsignedBigInteger('sku_id');
  45. $table->unsignedInteger('quantity');
  46. $table->unsignedBigInteger('min_amount_cents');
  47. $table->unsignedBigInteger('order_id')->nullable()->unique();
  48. $table->unsignedBigInteger('checkout_id')->nullable();
  49. $table->unsignedBigInteger('purchase_amount_cents')->nullable();
  50. $table->unsignedBigInteger('delivery_floor_id')->default(0);
  51. $table->string('status', 20)->default('bound');
  52. $table->unsignedInteger('revision')->default(1);
  53. $table->dateTime('created_at');
  54. $table->dateTime('qualified_at')->nullable();
  55. $table->unsignedBigInteger('delivery_id')->nullable();
  56. $table->unsignedBigInteger('reviewed_by')->nullable();
  57. $table->dateTime('reviewed_at')->nullable();
  58. $table->string('review_note', 500)->default('');
  59. $table->index(['inviter_id', 'id']);
  60. $table->index(['status', 'id']);
  61. });
  62. }
  63. foreach (['checkout_id', 'purchase_amount_cents'] as $column) {
  64. if (!$schema->hasColumn('sa_water_promotion_referral', $column)) {
  65. $schema->table('sa_water_promotion_referral', function (Blueprint $table) use ($column): void {
  66. $table->unsignedBigInteger($column)->nullable();
  67. });
  68. }
  69. }
  70. if (!$schema->hasColumn('sa_water_promotion_referral', 'delivery_floor_id')) {
  71. $schema->table('sa_water_promotion_referral', function (Blueprint $table): void {
  72. $table->unsignedBigInteger('delivery_floor_id')->default(0);
  73. });
  74. }
  75. WaterPromotionSetting::query()->insertOrIgnore(['id' => 1]);
  76. echo "Applied: promotion schema ready; existing policy preserved, new policy disabled.\n";