Explorar o código

先清洗数据,然后转换uuid

visuddhinanda hai 2 meses
pai
achega
25e63a8b7c

+ 12 - 10
api-v12/database/migrations/2025_12_17_165147_change_channel_uid_type_in_sentences.php

@@ -14,16 +14,20 @@ return new class extends Migration
      */
     public function up(): void
     {
-        Schema::table('sentences', function (Blueprint $table) {
-            DB::statement("
+        // ① 先清洗数据(保证可转换)
+        DB::statement("
+            UPDATE sentences
+            SET channel_uid = NULL
+            WHERE channel_uid IS NOT NULL
+              AND trim(channel_uid) = ''
+        ");
+
+        // ② 再修改字段类型
+        DB::statement("
             ALTER TABLE sentences
             ALTER COLUMN channel_uid TYPE uuid
-            USING CASE
-                WHEN channel_uid = '' THEN NULL
-                ELSE channel_uid::uuid
-            END
+            USING channel_uid::uuid
         ");
-        });
     }
 
     /**
@@ -33,8 +37,6 @@ return new class extends Migration
      */
     public function down(): void
     {
-        Schema::table('sentences', function (Blueprint $table) {
-            DB::statement('ALTER TABLE sentences ALTER COLUMN channel_uid TYPE varchar(255) USING channel_uid::varchar');
-        });
+        DB::statement('ALTER TABLE sentences ALTER COLUMN channel_uid TYPE varchar(36) USING channel_uid::text');
     }
 };