3
3
use Illuminate \Database \Migrations \Migration ;
4
4
use Illuminate \Database \Schema \Blueprint ;
5
5
use Illuminate \Support \Facades \Config ;
6
+ use Illuminate \Support \Facades \DB ;
6
7
use Illuminate \Support \Facades \Schema ;
7
8
8
9
return new class extends Migration
@@ -20,27 +21,37 @@ public function getConnection(): ?string
20
21
*/
21
22
public function up (): void
22
23
{
23
- Schema::create ('pulse_values ' , function (Blueprint $ table ) {
24
+ $ connection = $ this ->getConnection () ?? DB ::connection ();
25
+
26
+ Schema::create ('pulse_values ' , function (Blueprint $ table ) use ($ connection ) {
24
27
$ table ->engine = 'InnoDB ' ;
25
28
$ table ->id ();
26
29
$ table ->unsignedInteger ('timestamp ' );
27
30
$ table ->string ('type ' );
28
31
$ table ->text ('key ' );
29
- $ table ->char ('key_hash ' , 16 )->charset ('binary ' )->virtualAs ('unhex(md5(`key`)) ' );
32
+ match ($ driver = $ connection ->getDriverName ()) {
33
+ 'mysql ' => $ table ->char ('key_hash ' , 16 )->charset ('binary ' )->virtualAs ('unhex(md5(`key`)) ' ),
34
+ 'pgsql ' => $ table ->uuid ('key_hash ' )->storedAs ('md5("key")::uuid ' ),
35
+ default => throw new RuntimeException ("Unsupported database driver [ {$ driver }]. " ),
36
+ };
30
37
$ table ->text ('value ' );
31
38
32
39
$ table ->index ('timestamp ' ); // For trimming...
33
40
$ table ->index ('type ' ); // For fast lookups and purging...
34
- $ table ->unique (['type ' , 'key_hash ' ]); // For data integrity...
41
+ $ table ->unique (['type ' , 'key_hash ' ]); // For data integrity and upserts ...
35
42
});
36
43
37
- Schema::create ('pulse_entries ' , function (Blueprint $ table ) {
44
+ Schema::create ('pulse_entries ' , function (Blueprint $ table ) use ( $ connection ) {
38
45
$ table ->engine = 'InnoDB ' ;
39
46
$ table ->id ();
40
47
$ table ->unsignedInteger ('timestamp ' );
41
48
$ table ->string ('type ' );
42
49
$ table ->text ('key ' );
43
- $ table ->char ('key_hash ' , 16 )->charset ('binary ' )->virtualAs ('unhex(md5(`key`)) ' );
50
+ match ($ driver = $ connection ->getDriverName ()) {
51
+ 'mysql ' => $ table ->char ('key_hash ' , 16 )->charset ('binary ' )->virtualAs ('unhex(md5(`key`)) ' ),
52
+ 'pgsql ' => $ table ->uuid ('key_hash ' )->storedAs ('md5("key")::uuid ' ),
53
+ default => throw new RuntimeException ("Unsupported database driver [ {$ driver }]. " ),
54
+ };
44
55
$ table ->bigInteger ('value ' )->nullable ();
45
56
46
57
$ table ->index ('timestamp ' ); // For trimming...
@@ -49,14 +60,18 @@ public function up(): void
49
60
$ table ->index (['timestamp ' , 'type ' , 'key_hash ' , 'value ' ]); // For aggregate queries...
50
61
});
51
62
52
- Schema::create ('pulse_aggregates ' , function (Blueprint $ table ) {
63
+ Schema::create ('pulse_aggregates ' , function (Blueprint $ table ) use ( $ connection ) {
53
64
$ table ->engine = 'InnoDB ' ;
54
65
$ table ->id ();
55
66
$ table ->unsignedInteger ('bucket ' );
56
67
$ table ->unsignedMediumInteger ('period ' );
57
68
$ table ->string ('type ' );
58
69
$ table ->text ('key ' );
59
- $ table ->char ('key_hash ' , 16 )->charset ('binary ' )->virtualAs ('unhex(md5(`key`)) ' );
70
+ match ($ driver = $ connection ->getDriverName ()) {
71
+ 'mysql ' => $ table ->char ('key_hash ' , 16 )->charset ('binary ' )->virtualAs ('unhex(md5(`key`)) ' ),
72
+ 'pgsql ' => $ table ->uuid ('key_hash ' )->storedAs ('md5("key")::uuid ' ),
73
+ default => throw new RuntimeException ("Unsupported database driver [ {$ driver }]. " ),
74
+ };
60
75
$ table ->string ('aggregate ' );
61
76
$ table ->decimal ('value ' , 20 , 2 );
62
77
$ table ->unsignedInteger ('count ' )->nullable ();
0 commit comments