@@ -2094,25 +2094,43 @@ func (c *Cockroach) transferAccount(
20942094}
20952095
20962096func (c * Cockroach ) InitTables () error {
2097- enumTypes := []string {
2098- `CREATE TYPE IF NOT EXISTS subscription_status AS ENUM ('NORMAL', 'PAUSED', 'DEBT', 'DEBT_PRE_DELETION', 'DEBT_FINAL_DELETION', 'DELETED')` ,
2099- `CREATE TYPE IF NOT EXISTS subscription_operator AS ENUM ('created', 'upgraded', 'downgraded', 'canceled', 'renewed', 'deleted')` ,
2100- `CREATE TYPE IF NOT EXISTS subscription_pay_status AS ENUM ('pending', 'unpaid', 'paid', 'no_need', 'failed', 'expired', 'canceled')` ,
2101- `CREATE TYPE IF NOT EXISTS workspace_traffic_status AS ENUM ('active', 'exhausted', 'used_up', 'expired')` ,
2102- `CREATE TYPE IF NOT EXISTS subscription_transaction_status AS ENUM ('completed', 'pending', 'processing', 'failed', 'canceled')` ,
2097+ createEnumSQL := createEnumTypeSQLForPostgres
2098+ if isCockroachDatabase (c .DB ) {
2099+ createEnumSQL = createEnumTypeSQL
21032100 }
2101+ enumTypes := []string {
2102+ createEnumSQL (
2103+ "subscription_status" ,
2104+ []string {"NORMAL" , "PAUSED" , "DEBT" , "DEBT_PRE_DELETION" , "DEBT_FINAL_DELETION" , "DELETED" },
2105+ ),
2106+ createEnumSQL (
2107+ "subscription_operator" ,
2108+ []string {"created" , "upgraded" , "downgraded" , "canceled" , "renewed" , "deleted" },
2109+ ),
2110+ createEnumSQL (
2111+ "subscription_pay_status" ,
2112+ []string {"pending" , "unpaid" , "paid" , "no_need" , "failed" , "expired" , "canceled" },
2113+ ),
2114+ createEnumSQL (
2115+ "workspace_traffic_status" ,
2116+ []string {"active" , "exhausted" , "used_up" , "expired" },
2117+ ),
2118+ createEnumSQL (
2119+ "subscription_transaction_status" ,
2120+ []string {"completed" , "pending" , "processing" , "failed" , "canceled" },
2121+ )}
21042122 // subscription_pay_status 如果不存在canceled 状态,则添加
21052123 enumTypes = append (
21062124 enumTypes ,
2107- `ALTER TYPE subscription_pay_status ADD VALUE IF NOT EXISTS ' canceled'` ,
2125+ alterEnumAddValueSQL ( " subscription_pay_status" , " canceled" ) ,
21082126 )
21092127 enumTypes = append (
21102128 enumTypes ,
2111- `ALTER TYPE subscription_pay_status ADD VALUE IF NOT EXISTS ' unpaid'` ,
2129+ alterEnumAddValueSQL ( " subscription_pay_status" , " unpaid" ) ,
21122130 )
21132131 enumTypes = append (
21142132 enumTypes ,
2115- `ALTER TYPE subscription_transaction_status ADD VALUE IF NOT EXISTS ' canceled'` ,
2133+ alterEnumAddValueSQL ( " subscription_transaction_status" , " canceled" ) ,
21162134 )
21172135 for _ , query := range enumTypes {
21182136 err := c .DB .Exec (query ).Error
@@ -2170,32 +2188,23 @@ func (c *Cockroach) InitTables() error {
21702188 if ! c .DB .Migrator ().HasColumn (& types.Payment {}, `activityType` ) {
21712189 fmt .Println ("add column activityType" )
21722190 tableName := types.Payment {}.TableName ()
2173- err := c .DB .Exec (
2174- `ALTER TABLE "?" ADD COLUMN "activityType" TEXT;` ,
2175- gorm .Expr (tableName ),
2176- ).Error
2191+ err := c .DB .Exec (alterTableAddColumnSQL (tableName , `"activityType" TEXT` )).Error
21772192 if err != nil {
21782193 return fmt .Errorf ("failed to add column activityType: %w" , err )
21792194 }
21802195 }
21812196 if ! c .DB .Migrator ().HasColumn (& types.Payment {}, "workspace_subscription_id" ) {
21822197 fmt .Println ("add column workspace_subscription_id" )
21832198 tableName := types.Payment {}.TableName ()
2184- err := c .DB .Exec (
2185- `ALTER TABLE "?" ADD COLUMN "workspace_subscription_id" uuid;` ,
2186- gorm .Expr (tableName ),
2187- ).Error
2199+ err := c .DB .Exec (alterTableAddColumnSQL (tableName , `"workspace_subscription_id" uuid` )).Error
21882200 if err != nil {
21892201 return fmt .Errorf ("failed to add column workspace_subscription_id: %w" , err )
21902202 }
21912203 }
21922204 if ! c .DB .Migrator ().HasColumn (& types.PaymentOrder {}, "workspace_subscription_id" ) {
21932205 fmt .Println ("add column workspace_subscription_id to PaymentOrder" )
21942206 tableName := types.PaymentOrder {}.TableName ()
2195- err := c .DB .Exec (
2196- `ALTER TABLE "?" ADD COLUMN "workspace_subscription_id" uuid;` ,
2197- gorm .Expr (tableName ),
2198- ).Error
2207+ err := c .DB .Exec (alterTableAddColumnSQL (tableName , `"workspace_subscription_id" uuid` )).Error
21992208 if err != nil {
22002209 return fmt .Errorf (
22012210 "failed to add column workspace_subscription_id to PaymentOrder: %w" ,
@@ -2206,37 +2215,31 @@ func (c *Cockroach) InitTables() error {
22062215 if ! c .DB .Migrator ().HasColumn (& types.Payment {}, "stripe" ) {
22072216 fmt .Println ("add column stripe to Payment" )
22082217 tableName := types.Payment {}.TableName ()
2209- err := c .DB .Exec (`ALTER TABLE "?" ADD COLUMN " stripe" JSON;` , gorm . Expr ( tableName )).Error
2218+ err := c .DB .Exec (alterTableAddColumnSQL ( tableName , `" stripe" JSON` )).Error
22102219 if err != nil {
22112220 return fmt .Errorf ("failed to add column stripe to Payment: %w" , err )
22122221 }
22132222 }
22142223 if ! c .DB .Migrator ().HasColumn (& types.PaymentOrder {}, "stripe" ) {
22152224 fmt .Println ("add column stripe to Payment" )
22162225 tableName := types.PaymentOrder {}.TableName ()
2217- err := c .DB .Exec (`ALTER TABLE "?" ADD COLUMN " stripe" JSON;` , gorm . Expr ( tableName )).Error
2226+ err := c .DB .Exec (alterTableAddColumnSQL ( tableName , `" stripe" JSON` )).Error
22182227 if err != nil {
22192228 return fmt .Errorf ("failed to add column stripe to Payment: %w" , err )
22202229 }
22212230 }
22222231 if ! c .DB .Migrator ().HasColumn (& types.WorkspaceSubscriptionPlan {}, "ai_quota" ) {
22232232 fmt .Println ("add column ai_quota to WorkspaceSubscriptionPlan" )
22242233 tableName := types.WorkspaceSubscriptionPlan {}.TableName ()
2225- err := c .DB .Exec (
2226- `ALTER TABLE "?" ADD COLUMN "ai_quota" bigint NOT NULL DEFAULT 0;` ,
2227- gorm .Expr (tableName ),
2228- ).Error
2234+ err := c .DB .Exec (alterTableAddColumnSQL (tableName , `"ai_quota" bigint NOT NULL DEFAULT 0` )).Error
22292235 if err != nil {
22302236 return fmt .Errorf ("failed to add column ai_quota to WorkspaceSubscriptionPlan: %w" , err )
22312237 }
22322238 }
22332239 if ! c .Localdb .Migrator ().HasColumn (& types.WorkspaceSubscriptionPlan {}, "ai_quota" ) {
22342240 fmt .Println ("add column ai_quota to WorkspaceSubscriptionPlan" )
22352241 tableName := types.WorkspaceSubscriptionPlan {}.TableName ()
2236- err := c .Localdb .Exec (
2237- `ALTER TABLE "?" ADD COLUMN "ai_quota" bigint NOT NULL DEFAULT 0;` ,
2238- gorm .Expr (tableName ),
2239- ).Error
2242+ err := c .Localdb .Exec (alterTableAddColumnSQL (tableName , `"ai_quota" bigint NOT NULL DEFAULT 0` )).Error
22402243 if err != nil {
22412244 return fmt .Errorf ("failed to add column ai_quota to WorkspaceSubscriptionPlan: %w" , err )
22422245 }
@@ -2250,8 +2253,7 @@ func (c *Cockroach) InitTables() error {
22502253 fmt .Println ("add table `Credits` column updated_at" )
22512254 tableName := types.Credits {}.TableName ()
22522255 err := c .DB .Exec (
2253- `ALTER TABLE "?" ADD COLUMN "updated_at" TIMESTAMP(3) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;` ,
2254- gorm .Expr (tableName ),
2256+ alterTableAddColumnSQL (tableName , `"updated_at" TIMESTAMP(3) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP` ),
22552257 ).Error
22562258 if err != nil {
22572259 return fmt .Errorf ("failed to add column updated_at: %w" , err )
@@ -2271,7 +2273,7 @@ func (c *Cockroach) InitTables() error {
22712273 if ! c .DB .Migrator ().HasColumn (& types.Corporate {}, `type` ) {
22722274 fmt .Println ("add table `Corporate` column type" )
22732275 tableName := types.Corporate {}.TableName ()
2274- err := c .DB .Exec (`ALTER TABLE "?" ADD COLUMN " type" TEXT;` , gorm . Expr ( tableName )).Error
2276+ err := c .DB .Exec (alterTableAddColumnSQL ( tableName , `" type" TEXT` )).Error
22752277 if err != nil {
22762278 return fmt .Errorf ("failed to add column type: %w" , err )
22772279 }
0 commit comments