33use Illuminate \Database \Migrations \Migration ;
44use Illuminate \Database \Schema \Blueprint ;
55use Illuminate \Support \Facades \Config ;
6+ use Illuminate \Support \Facades \DB ;
67use Illuminate \Support \Facades \Schema ;
78
89return new class extends Migration
@@ -20,27 +21,37 @@ public function getConnection(): ?string
2021 */
2122 public function up (): void
2223 {
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 ) {
2427 $ table ->engine = 'InnoDB ' ;
2528 $ table ->id ();
2629 $ table ->unsignedInteger ('timestamp ' );
2730 $ table ->string ('type ' );
2831 $ 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+ };
3037 $ table ->text ('value ' );
3138
3239 $ table ->index ('timestamp ' ); // For trimming...
3340 $ 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 ...
3542 });
3643
37- Schema::create ('pulse_entries ' , function (Blueprint $ table ) {
44+ Schema::create ('pulse_entries ' , function (Blueprint $ table ) use ( $ connection ) {
3845 $ table ->engine = 'InnoDB ' ;
3946 $ table ->id ();
4047 $ table ->unsignedInteger ('timestamp ' );
4148 $ table ->string ('type ' );
4249 $ 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+ };
4455 $ table ->bigInteger ('value ' )->nullable ();
4556
4657 $ table ->index ('timestamp ' ); // For trimming...
@@ -49,14 +60,18 @@ public function up(): void
4960 $ table ->index (['timestamp ' , 'type ' , 'key_hash ' , 'value ' ]); // For aggregate queries...
5061 });
5162
52- Schema::create ('pulse_aggregates ' , function (Blueprint $ table ) {
63+ Schema::create ('pulse_aggregates ' , function (Blueprint $ table ) use ( $ connection ) {
5364 $ table ->engine = 'InnoDB ' ;
5465 $ table ->id ();
5566 $ table ->unsignedInteger ('bucket ' );
5667 $ table ->unsignedMediumInteger ('period ' );
5768 $ table ->string ('type ' );
5869 $ 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+ };
6075 $ table ->string ('aggregate ' );
6176 $ table ->decimal ('value ' , 20 , 2 );
6277 $ table ->unsignedInteger ('count ' )->nullable ();
0 commit comments