Skip to content

Commit 07965d0

Browse files
committed
These migrations... work?? 🐋
1 parent 1873c1e commit 07965d0

6 files changed

+173
-80
lines changed

database/migrations/2017_09_09_125253_AddChainedTasksAbility.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

database/migrations/2017_09_09_162204_AddNullableNextRunColumn.php

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class RenameTasksTableForStructureRefactor extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*/
11+
public function up()
12+
{
13+
Schema::rename('tasks', 'tasks_old');
14+
}
15+
16+
/**
17+
* Reverse the migrations.
18+
*/
19+
public function down()
20+
{
21+
Schema::rename('tasks_old', 'tasks');
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateSchedulesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up()
13+
{
14+
Schema::create('schedules', function (Blueprint $table) {
15+
$table->increments('id');
16+
$table->unsignedInteger('server_id');
17+
$table->string('name')->nullable();
18+
$table->string('cron_day_of_week');
19+
$table->string('cron_day_of_month');
20+
$table->string('cron_hour');
21+
$table->string('cron_minute');
22+
$table->boolean('is_active');
23+
$table->boolean('is_processing');
24+
$table->timestamp('last_run_at');
25+
$table->timestamp('next_run_at');
26+
$table->timestamps();
27+
28+
$table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade');
29+
});
30+
}
31+
32+
/**
33+
* Reverse the migrations.
34+
*/
35+
public function down()
36+
{
37+
Schema::dropIfExists('schedules');
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateNewTasksTableForSchedules extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up()
13+
{
14+
Schema::create('tasks', function (Blueprint $table) {
15+
$table->increments('id');
16+
$table->unsignedInteger('schedule_id');
17+
$table->unsignedInteger('sequence_id');
18+
$table->string('action');
19+
$table->text('payload');
20+
$table->unsignedInteger('time_offset');
21+
$table->boolean('is_queued');
22+
$table->timestamps();
23+
24+
$table->index(['schedule_id', 'sequence_id']);
25+
$table->foreign('schedule_id')->references('id')->on('schedules')->onDelete('cascade');
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('tasks');
35+
}
36+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
use Carbon\Carbon;
4+
use Illuminate\Support\Facades\DB;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Database\Migrations\Migration;
7+
8+
class TransferOldTasksToNewScheduler extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*/
13+
public function up()
14+
{
15+
$tasks = DB::table('tasks_old')->get();
16+
17+
DB::beginTransaction();
18+
$tasks->each(function ($task) {
19+
$schedule = DB::table('schedules')->insertGetId([
20+
'server_id' => $task->server_id,
21+
'name' => null,
22+
'cron_day_of_week' => $task->day_of_week,
23+
'cron_day_of_month' => $task->day_of_month,
24+
'cron_hour' => $task->hour,
25+
'cron_minute' => $task->minute,
26+
'is_active' => (bool) $task->active,
27+
'is_processing' => false,
28+
'last_run_at' => $task->last_run,
29+
'next_run_at' => $task->next_run,
30+
'created_at' => $task->created_at,
31+
'updated_at' => Carbon::now()->toDateTimeString(),
32+
]);
33+
34+
DB::table('tasks')->insert([
35+
'schedule_id' => $schedule,
36+
'sequence_id' => 1,
37+
'action' => $task->action,
38+
'payload' => $task->data,
39+
'time_offset' => 0,
40+
'is_queued' => false,
41+
'updated_at' => Carbon::now()->toDateTimeString(),
42+
'created_at' => Carbon::now()->toDateTimeString(),
43+
]);
44+
45+
DB::table('tasks_old')->delete($task->id);
46+
DB::commit();
47+
});
48+
49+
Schema::dropIfExists('tasks_old');
50+
}
51+
52+
/**
53+
* Reverse the migrations.
54+
*/
55+
public function down()
56+
{
57+
Schema::create('tasks_old', function (Blueprint $table) {
58+
$table->increments('id');
59+
$table->unsignedInteger('user_id')->nullable();
60+
$table->unsignedInteger('server_id');
61+
$table->tinyInteger('active')->default(1);
62+
$table->string('action');
63+
$table->text('data');
64+
$table->unsignedTinyInteger('queued')->default(0);
65+
$table->string('year')->default('*');
66+
$table->string('month')->default('*');
67+
$table->string('day_of_week')->default('*');
68+
$table->string('day_of_month')->default('*');
69+
$table->string('minute')->default('*');
70+
$table->timestamp('last_run')->nullable();
71+
$table->timestamp('next_run');
72+
$table->timestamps();
73+
});
74+
}
75+
}

0 commit comments

Comments
 (0)