瀏覽代碼

fix: db migration not working

pplulee 2 月之前
父節點
當前提交
d8835c6d1e

+ 1 - 0
config/.config.example.php

@@ -37,6 +37,7 @@ $_ENV['db_prefix'] = '';
 //Redis设置--------------------------------------------------------------------------------------------------------------
 $_ENV['redis_host'] = '127.0.0.1';    // Redis地址,使用unix domain socket时填写文件路径
 $_ENV['redis_port'] = 6379;           // Redis端口,使用unix domain socket时填写-1
+$_ENV['redis_db'] = 0;                // Redis数据库编号,留空则使用默认的0
 $_ENV['redis_connect_timeout'] = 2.0; // Redis连接超时时间,单位秒
 $_ENV['redis_read_timeout'] = 8.0;    // Redis读取超时时间,单位秒
 $_ENV['redis_username'] = '';         // Redis用户名,留空则不使用用户名连接

+ 1 - 1
db/migrations/2023071700-add_user_is_shadow_banned.php

@@ -22,7 +22,7 @@ return new class() implements MigrationInterface {
     {
         DB::getPdo()->exec("
             ALTER TABLE user DROP COLUMN IF EXISTS `is_shadow_banned`;
-            ALTER TABLE user ADD COLUMN IF NOT EXISTS `use_new_shop` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '是否启用新商店',
+            ALTER TABLE user ADD COLUMN IF NOT EXISTS `use_new_shop` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '是否启用新商店';
         ");
 
         return 2023071600;

+ 16 - 7
src/Command/Migration.php

@@ -45,7 +45,19 @@ END;
             $tables = DB::select('SHOW TABLES');
 
             if ($tables === []) {
-                $max_version = PHP_INT_MAX;
+                $files = scandir(BASE_PATH . '/db/migrations/', SCANDIR_SORT_NONE);
+                $min_migration_version = PHP_INT_MAX;
+                foreach ($files as $file) {
+                    if ($file === '.' || $file === '..' || ! str_ends_with($file, '.php')) {
+                        continue;
+                    }
+                    $version = (int) strstr($file, '-', true);
+                    if ($version < $min_migration_version) {
+                        $min_migration_version = $version;
+                    }
+                }
+                $min_version = $min_migration_version - 1;
+                $max_version = $min_migration_version;
             } else {
                 echo 'Database is not empty, do not use "new" as version.' . PHP_EOL;
 
@@ -82,14 +94,11 @@ END;
                 $version = (int) strstr($file, '-', true);
                 echo 'Found migration version ' . $version;
 
-                if ($version > $latest) {
+                if ($target !== 'new' && $version > $latest) {
                     $latest = $version;
                 }
 
-                if ($version <= $min_version ||
-                    $version > $max_version ||
-                    ($target === 'new' && $version !== 2023020100)
-                ) {
+                if ($version <= $min_version || $version > $max_version) {
                     echo '...skip' . PHP_EOL;
                     continue;
                 }
@@ -132,7 +141,7 @@ END;
         $stat = DB::getPdo()->prepare($sql);
 
         if ($target === 'new') {
-            $stat->execute([$latest]);
+            $stat->execute([array_key_first($queue)]);
         } else {
             $stat->execute([$current]);
         }

+ 1 - 0
src/Services/Cache.php

@@ -20,6 +20,7 @@ final class Cache
             'port' => $_ENV['redis_port'],
             'connectTimeout' => $_ENV['redis_connect_timeout'],
             'readTimeout' => $_ENV['redis_read_timeout'],
+            'database' => $_ENV['redis_db'] ?? 0,
         ];
 
         if ($_ENV['redis_username'] !== '') {

+ 2 - 2
src/Services/DB.php

@@ -19,10 +19,10 @@ final class DB extends Manager
             $db->getConnection()->getPdo();
         } catch (Exception $e) {
             if ($_ENV['debug']) {
-                die('Databse Error' . PHP_EOL . 'Reason: ' . $e->getMessage());
+                die('Database Error' . PHP_EOL . 'Reason: ' . $e->getMessage());
             }
 
-            die('Databse Error');
+            die('Database Error');
         }
 
         $db->setAsGlobal();