migrate_water_mini_identity.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /** 增量迁移水泉智配小程序账号绑定菜单与权限。可重复执行。 */
  3. require dirname(__DIR__).'/vendor/autoload.php';
  4. \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
  5. \support\App::loadAllConfig(['route']);
  6. use support\think\Db;
  7. $sql = (new ReflectionClass(Db::class))->getFileName();
  8. $path = dirname(__DIR__) . '/plugin/saiadmin/db/20260923_add_water_mini_identity_menu.sql';
  9. if (!is_file($path)) {
  10. fwrite(STDERR, "Migration file missing: $path\n");
  11. exit(1);
  12. }
  13. $raw = file_get_contents($path);
  14. $statements = array_filter(array_map('trim', preg_split('/;\s*[\r\n]+/', $raw)));
  15. if (!$statements) {
  16. fwrite(STDERR, "No statements parsed.\n");
  17. exit(1);
  18. }
  19. $apply = in_array('--apply', $argv, true);
  20. if (!$apply) {
  21. echo "Plan: " . count($statements) . " statements (idempotent). Re-run with --apply to execute.\n";
  22. exit;
  23. }
  24. $conn = Db::connect();
  25. $conn->startTrans();
  26. try {
  27. foreach ($statements as $stmt) {
  28. $conn->execute($stmt);
  29. }
  30. $conn->commit();
  31. echo "Applied: " . count($statements) . " statements.\n";
  32. } catch (\Throwable $e) {
  33. $conn->rollback();
  34. fwrite(STDERR, "Rollback due to: " . $e->getMessage() . "\n");
  35. exit(1);
  36. }