@@ -22,41 +22,36 @@ protected function table(): string
2222 */
2323 public function up (): void
2424 {
25- Schema::table ($ this ->table (), function (Blueprint $ table ) {
26- $ table ->renameColumn ('status ' , 'tmpStatus ' );
27- });
28-
29- Schema::table ($ this ->table (), function (Blueprint $ table ) {
30- $ table ->renameColumn ('status_last ' , 'tmpStatusLast ' );
31- });
32-
33- Schema::table ($ this ->table (), function (Blueprint $ table ) {
34- $ enums = [
35- Transfer::STATUS_TRANSFER ,
36- Transfer::STATUS_PAID ,
37- Transfer::STATUS_REFUND ,
38- Transfer::STATUS_GIFT ,
39- ];
40-
41- $ table ->enum ('status ' , $ enums )
42- ->default (Transfer::STATUS_TRANSFER );
43-
44- $ table ->enum ('status_last ' , $ enums )
45- ->nullable ();
46- });
47-
48- DB ::table ($ this ->table ())
49- ->update ([
50- 'status ' => DB ::raw ('tmpStatus ' ),
51- 'status_last ' => DB ::raw ('tmpStatusLast ' ),
52- ]);
53-
54- Schema::table ($ this ->table (), function (Blueprint $ table ) {
55- $ table ->dropColumn ('tmpStatus ' );
56- });
57-
58- Schema::table ($ this ->table (), function (Blueprint $ table ) {
59- $ table ->dropColumn ('tmpStatusLast ' );
25+ $ enums = [
26+ Transfer::STATUS_TRANSFER ,
27+ Transfer::STATUS_PAID ,
28+ Transfer::STATUS_REFUND ,
29+ Transfer::STATUS_GIFT ,
30+ ];
31+
32+ if (DB ::connection () instanceof \Illuminate \Database \MySqlConnection) {
33+ $ table = $ this ->table ();
34+ $ enumString = \implode ('\', \'' , $ enums );
35+ $ default = Transfer::STATUS_TRANSFER ;
36+ DB ::statement ("ALTER TABLE $ table CHANGE COLUMN status status ENUM(' $ enumString') NOT NULL DEFAULT ' $ default' " );
37+ DB ::statement ("ALTER TABLE $ table CHANGE COLUMN status_last status_last ENUM(' $ enumString') NULL " );
38+ return ;
39+ }
40+
41+ if (DB ::connection () instanceof \Illuminate \Database \PostgresConnection) {
42+ $ this ->alterEnum ($ this ->table (), 'status ' , $ enums );
43+ $ this ->alterEnum ($ this ->table (), 'status_last ' , $ enums );
44+ return ;
45+ }
46+
47+ Schema::table ($ this ->table (), function (Blueprint $ table ) use ($ enums ) {
48+ $ table ->string ('status ' )
49+ ->default (Transfer::STATUS_TRANSFER )
50+ ->change ();
51+
52+ $ table ->string ('status_last ' )
53+ ->nullable ()
54+ ->change ();
6055 });
6156 }
6257
@@ -65,48 +60,67 @@ public function up(): void
6560 */
6661 public function down (): void
6762 {
68- Schema::table ($ this ->table (), function (Blueprint $ table ) {
69- $ table ->renameColumn ('status ' , 'tmpStatus ' );
70- });
71-
72- Schema::table ($ this ->table (), function (Blueprint $ table ) {
73- $ table ->renameColumn ('status_last ' , 'tmpStatusLast ' );
74- });
75-
76- Schema::table ($ this ->table (), function (Blueprint $ table ) {
77- $ enums = [
78- Transfer::STATUS_PAID ,
79- Transfer::STATUS_REFUND ,
80- Transfer::STATUS_GIFT ,
81- ];
82-
83- $ table ->enum ('status ' , $ enums )
84- ->default (Transfer::STATUS_PAID );
63+ DB ::table ($ this ->table ())
64+ ->where ('status ' , Transfer::STATUS_TRANSFER )
65+ ->update (['status ' => Transfer::STATUS_PAID ]);
8566
86- $ table ->enum ('status_last ' , $ enums )
87- ->nullable ();
67+ DB ::table ($ this ->table ())
68+ ->where ('status_last ' , Transfer::STATUS_TRANSFER )
69+ ->update (['status_last ' => Transfer::STATUS_PAID ]);
70+
71+ $ enums = [
72+ Transfer::STATUS_PAID ,
73+ Transfer::STATUS_REFUND ,
74+ Transfer::STATUS_GIFT ,
75+ ];
76+
77+ if (DB ::connection () instanceof \Illuminate \Database \MySqlConnection) {
78+ $ table = $ this ->table ();
79+ $ enumString = \implode ('\', \'' , $ enums );
80+ $ default = Transfer::STATUS_PAID ;
81+ DB ::statement ("ALTER TABLE $ table CHANGE COLUMN status status ENUM(' $ enumString') NOT NULL DEFAULT ' $ default' " );
82+ DB ::statement ("ALTER TABLE $ table CHANGE COLUMN status_last status_last ENUM(' $ enumString') NULL " );
83+ return ;
84+ }
85+
86+ if (DB ::connection () instanceof \Illuminate \Database \PostgresConnection) {
87+ $ this ->alterEnum ($ this ->table (), 'status ' , $ enums );
88+ $ this ->alterEnum ($ this ->table (), 'status_last ' , $ enums );
89+ return ;
90+ }
91+
92+ Schema::table ($ this ->table (), function (Blueprint $ table ) use ($ enums ) {
93+ $ table ->string ('status ' )
94+ ->default (Transfer::STATUS_PAID )
95+ ->change ();
96+
97+ $ table ->string ('status_last ' )
98+ ->nullable ()
99+ ->change ();
88100 });
101+ }
89102
90- DB ::table ($ this ->table ())
91- ->where ('tmpStatus ' , Transfer::STATUS_TRANSFER )
92- ->update (['tmpStatus ' => Transfer::STATUS_PAID ]);
103+ /**
104+ * Alter an enum field constraints
105+ * @param $table
106+ * @param $field
107+ * @param array $options
108+ */
109+ protected function alterEnum ($ table , $ field , array $ options ): void
110+ {
111+ $ check = "$ {table}_ $ {field}_check " ;
93112
94- DB ::table ($ this ->table ())
95- ->where ('tmpStatusLast ' , Transfer::STATUS_TRANSFER )
96- ->update (['tmpStatusLast ' => Transfer::STATUS_PAID ]);
113+ $ enumList = [];
97114
98- DB ::table ($ this ->table ())
99- ->update ([
100- 'status ' => DB ::raw ('tmpStatus ' ),
101- 'status_last ' => DB ::raw ('tmpStatusLast ' ),
102- ]);
115+ foreach ($ options as $ option ) {
116+ $ enumList [] = sprintf ("'%s'::CHARACTER VARYING " , $ option );
117+ }
103118
104- Schema::table ($ this ->table (), function (Blueprint $ table ) {
105- $ table ->dropColumn ('tmpStatus ' );
106- });
119+ $ enumString = implode (', ' , $ enumList );
107120
108- Schema::table ($ this ->table (), function (Blueprint $ table ) {
109- $ table ->dropColumn ('tmpStatusLast ' );
121+ DB ::transaction (function () use ($ table , $ field , $ check , $ options , $ enumString ) {
122+ DB ::statement (sprintf ('ALTER TABLE %s DROP CONSTRAINT %s; ' , $ table , $ check ));
123+ DB ::statement (sprintf ('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s::TEXT = ANY (ARRAY[%s]::TEXT[])) ' , $ table , $ check , $ field , $ enumString ));
110124 });
111125 }
112126
0 commit comments