1
1
<?php
2
2
3
+ use Illuminate \Database \Connection ;
3
4
use Illuminate \Database \Migrations \Migration ;
4
5
use Illuminate \Database \Schema \Blueprint ;
5
6
use Illuminate \Support \Facades \DB ;
@@ -22,15 +23,29 @@ public function up()
22
23
$ table ->index (['key ' , 'metable_type ' , 'numeric_value ' ]);
23
24
24
25
$ stringIndexLength = (int )config ('metable.stringValueIndexLength ' , 255 );
25
- if ($ stringIndexLength > 0 && $ driver = $ this ->detectDriverName ()) {
26
+ $ connection = $ this ->detectConnectionInUse ();
27
+ if ($ stringIndexLength > 0 && $ driver = $ connection ?->getDriverName()) {
28
+ $ grammar = $ connection ->getQueryGrammar ();
26
29
if (in_array ($ driver , ['mysql ' , 'mariadb ' ])) {
27
30
$ table ->rawIndex (
28
- "`metable_type`, `key`, `value`( $ stringIndexLength) " ,
31
+ sprintf (
32
+ "%s, %s, %s(%d) " ,
33
+ $ grammar ->wrap ('metable_type ' ),
34
+ $ grammar ->wrap ('key ' ),
35
+ $ grammar ->wrap ('value ' ),
36
+ $ stringIndexLength
37
+ ),
29
38
'value_string_prefix_index '
30
39
);
31
40
} elseif (in_array ($ driver , ['pgsql ' , 'sqlite ' ])) {
32
41
$ table ->rawIndex (
33
- "`metable_type`, `key`, SUBSTR(`value`, 1, $ stringIndexLength) " ,
42
+ sprintf (
43
+ "%s, %s, SUBSTR(%s, 1, %d) " ,
44
+ $ grammar ->wrap ('metable_type ' ),
45
+ $ grammar ->wrap ('key ' ),
46
+ $ grammar ->wrap ('value ' ),
47
+ $ stringIndexLength
48
+ ),
34
49
'value_string_prefix_index '
35
50
);
36
51
}
@@ -48,7 +63,10 @@ public function down()
48
63
Schema::table ('meta ' , function (Blueprint $ table ) {
49
64
$ stringIndexLength = (int )config ('metable.stringValueIndexLength ' , 255 );
50
65
if ($ stringIndexLength > 0
51
- && in_array ($ this ->detectDriverName (), ['mysql ' , 'mariadb ' , 'pgsql ' , 'sqlite ' ])
66
+ && in_array (
67
+ $ this ->detectConnectionInUse ()?->getDriverName(),
68
+ ['mysql ' , 'mariadb ' , 'pgsql ' , 'sqlite ' ]
69
+ )
52
70
) {
53
71
$ table ->dropIndex ('value_string_prefix_index ' );
54
72
}
@@ -61,7 +79,7 @@ public function down()
61
79
});
62
80
}
63
81
64
- private function detectDriverName (): ?string
82
+ private function detectConnectionInUse (): ?Connection
65
83
{
66
84
/** @var \Illuminate\Database\Migrations\Migrator $migrator */
67
85
$ migrator = app ('migrator ' );
@@ -73,8 +91,10 @@ private function detectDriverName(): ?string
73
91
$ resolver = DB ::getFacadeRoot ();
74
92
}
75
93
76
- return $ resolver ->connection (
94
+ $ connection = $ resolver ->connection (
77
95
$ this ->getConnection () ?? $ migrator ->getConnection ()
78
- )->getDriverName ();
96
+ );
97
+
98
+ return $ connection instanceof Connection ? $ connection : null ;
79
99
}
80
100
};
0 commit comments