diff --git a/database/migrations/2026_11_19_110000_cover_taobao_alipay_opening_balance.php b/database/migrations/2026_11_19_110000_cover_taobao_alipay_opening_balance.php new file mode 100644 index 00000000..e0d4caab --- /dev/null +++ b/database/migrations/2026_11_19_110000_cover_taobao_alipay_opening_balance.php @@ -0,0 +1,64 @@ +hasTable(self::TABLE) || $schema->hasIndex(self::TABLE, self::INDEX)) return; + + $connection = $schema->getConnection(); + if (in_array($connection->getDriverName(), ['mysql', 'mariadb'], true)) { + // The opening balance reads the last row for every Alipay account. + // Keep store flags and amounts in the index so the window query does + // not fetch millions of wide source rows. + $wait = (int) $connection->selectOne('SELECT @@SESSION.lock_wait_timeout AS seconds')->seconds; + $connection->statement('SET SESSION lock_wait_timeout=5'); + try { + $table = $connection->getQueryGrammar()->wrapTable(self::TABLE); + $index = $connection->getQueryGrammar()->wrap(self::INDEX); + $connection->statement( + 'ALTER TABLE ' . $table . ' ADD INDEX ' . $index + . ' (alipay_account, occurred_at, source_row_number, store_name,' + . ' balance_amount, income_amount, expense_amount),' + . ' ALGORITHM=INPLACE, LOCK=NONE', + ); + } finally { + $connection->statement('SET SESSION lock_wait_timeout=' . $wait); + } + } else { + $schema->table(self::TABLE, static fn (Blueprint $table) => $table->index( + [ + 'alipay_account', + 'occurred_at', + 'source_row_number', + 'store_name', + 'balance_amount', + 'income_amount', + 'expense_amount', + ], + self::INDEX, + )); + } + } + + public function down(): void + { + if (config('system_database.mode', 'bootstrap') === 'bootstrap') return; + + $schema = Schema::connection((string) config('upstream_mirrors.connection', 'business_mirror')); + if ($schema->hasTable(self::TABLE) && $schema->hasIndex(self::TABLE, self::INDEX)) { + $schema->table(self::TABLE, static fn (Blueprint $table) => $table->dropIndex(self::INDEX)); + } + } +};