1
1
<?php
2
2
3
3
use Illuminate \Database \Schema \Blueprint ;
4
+ use Illuminate \Support \Facades \DB ;
4
5
use Illuminate \Support \Facades \Schema ;
5
6
use Studio \Totem \Database \TotemMigration ;
6
7
@@ -13,10 +14,7 @@ class UpdateTaskResultsDurationType extends TotemMigration
13
14
*/
14
15
public function up ()
15
16
{
16
- Schema::connection (TOTEM_DATABASE_CONNECTION )
17
- ->table (TOTEM_TABLE_PREFIX .'task_results ' , function (Blueprint $ table ) {
18
- $ table ->decimal ('duration ' , 24 , 14 )->charset ('' )->collation ('' )->change ();
19
- });
17
+ $ this ->migrateDurationValues ();
20
18
}
21
19
22
20
/**
@@ -26,9 +24,49 @@ public function up()
26
24
*/
27
25
public function down ()
28
26
{
27
+ $ this ->migrateDurationValues (false );
28
+ }
29
+
30
+ /**
31
+ * @param bool $toFloat
32
+ */
33
+ private function migrateDurationValues (bool $ toFloat = true )
34
+ {
35
+ Schema::connection (TOTEM_DATABASE_CONNECTION )
36
+ ->table (TOTEM_TABLE_PREFIX .'task_results ' , function (Blueprint $ table ) {
37
+ // Move string duration column temporarily
38
+ $ table ->renameColumn ('duration ' , 'duration_old ' );
39
+ });
40
+
41
+ Schema::connection (TOTEM_DATABASE_CONNECTION )
42
+ ->table (TOTEM_TABLE_PREFIX .'task_results ' , function (Blueprint $ table ) use ($ toFloat ) {
43
+ // Create new decimal column
44
+ if ($ toFloat ) {
45
+ $ table ->decimal ('duration ' , 24 , 14 )->default (0.0 )->charset ('' )->collation ('' );
46
+ } else {
47
+ $ table ->string ('duration ' )->default ('' );
48
+ }
49
+ });
50
+
51
+ // Copy old duration data into new column
52
+ DB ::connection (TOTEM_DATABASE_CONNECTION )
53
+ ->table (TOTEM_TABLE_PREFIX .'task_results ' )
54
+ ->select (['id ' , 'duration_old ' ])
55
+ ->chunkById (100 , function ($ rows ) use ($ toFloat ) {
56
+ foreach ($ rows as $ row ) {
57
+ DB ::connection (TOTEM_DATABASE_CONNECTION )
58
+ ->table (TOTEM_TABLE_PREFIX )
59
+ ->where ('id ' , $ row ->id )
60
+ ->update ([
61
+ 'duration ' => $ toFloat ? floatval ($ row ->duration_old ) : (string ) $ row ->duration_old ,
62
+ ]);
63
+ }
64
+ });
65
+
29
66
Schema::connection (TOTEM_DATABASE_CONNECTION )
30
67
->table (TOTEM_TABLE_PREFIX .'task_results ' , function (Blueprint $ table ) {
31
- $ table ->string ('duration ' , 255 )->change ();
68
+ // Remove temp column
69
+ $ table ->dropColumn ('duration_old ' );
32
70
});
33
71
}
34
72
}
0 commit comments