These migrations... work?? 🐋

This commit is contained in:
Dane Everitt 2017-09-11 01:15:44 -05:00
parent 1873c1e9b9
commit 07965d0ce7
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
6 changed files with 173 additions and 80 deletions

View File

@ -1,49 +0,0 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddChainedTasksAbility extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('tasks', function (Blueprint $table) {
$table->unsignedInteger('parent_task_id')->after('id')->nullable();
$table->unsignedInteger('chain_order')->after('parent_task_id')->nullable();
$table->unsignedInteger('chain_delay')->after('minute')->nullable();
$table->string('name')->after('server_id')->nullable();
$table->foreign('parent_task_id')->references('id')->on('tasks')->onDelete('cascade');
$table->index(['parent_task_id', 'chain_order']);
$table->dropForeign(['user_id']);
$table->dropColumn('user_id');
$table->dropColumn('year');
$table->dropColumn('month');
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('tasks', function (Blueprint $table) {
$table->dropForeign(['parent_task_id']);
$table->dropIndex(['parent_task_id', 'chain_order']);
$table->dropColumn('parent_task_id');
$table->dropColumn('chain_order');
$table->dropColumn('chain_delay');
$table->dropColumn('name');
$table->unsignedInteger('user_id')->after('id')->nullable();
$table->string('year')->after('queued')->default('*');
$table->string('month')->after('year')->default('*');
$table->foreign('user_id')->references('id')->on('users');
});
}
}

View File

@ -1,31 +0,0 @@
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNullableNextRunColumn extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('tasks', function (Blueprint $table) {
$table = DB::getQueryGrammar()->wrapTable('tasks');
DB::statement('ALTER TABLE ' . $table . ' CHANGE `next_run` `next_run` TIMESTAMP NULL;');
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('tasks', function (Blueprint $table) {
$table = DB::getQueryGrammar()->wrapTable('tasks');
DB::statement('ALTER TABLE ' . $table . ' CHANGE `next_run` `next_run` TIMESTAMP NOT NULL;');
});
}
}

View File

@ -0,0 +1,23 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
class RenameTasksTableForStructureRefactor extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::rename('tasks', 'tasks_old');
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::rename('tasks_old', 'tasks');
}
}

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSchedulesTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('schedules', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('server_id');
$table->string('name')->nullable();
$table->string('cron_day_of_week');
$table->string('cron_day_of_month');
$table->string('cron_hour');
$table->string('cron_minute');
$table->boolean('is_active');
$table->boolean('is_processing');
$table->timestamp('last_run_at');
$table->timestamp('next_run_at');
$table->timestamps();
$table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('schedules');
}
}

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNewTasksTableForSchedules extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('schedule_id');
$table->unsignedInteger('sequence_id');
$table->string('action');
$table->text('payload');
$table->unsignedInteger('time_offset');
$table->boolean('is_queued');
$table->timestamps();
$table->index(['schedule_id', 'sequence_id']);
$table->foreign('schedule_id')->references('id')->on('schedules')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}

View File

@ -0,0 +1,75 @@
<?php
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class TransferOldTasksToNewScheduler extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
$tasks = DB::table('tasks_old')->get();
DB::beginTransaction();
$tasks->each(function ($task) {
$schedule = DB::table('schedules')->insertGetId([
'server_id' => $task->server_id,
'name' => null,
'cron_day_of_week' => $task->day_of_week,
'cron_day_of_month' => $task->day_of_month,
'cron_hour' => $task->hour,
'cron_minute' => $task->minute,
'is_active' => (bool) $task->active,
'is_processing' => false,
'last_run_at' => $task->last_run,
'next_run_at' => $task->next_run,
'created_at' => $task->created_at,
'updated_at' => Carbon::now()->toDateTimeString(),
]);
DB::table('tasks')->insert([
'schedule_id' => $schedule,
'sequence_id' => 1,
'action' => $task->action,
'payload' => $task->data,
'time_offset' => 0,
'is_queued' => false,
'updated_at' => Carbon::now()->toDateTimeString(),
'created_at' => Carbon::now()->toDateTimeString(),
]);
DB::table('tasks_old')->delete($task->id);
DB::commit();
});
Schema::dropIfExists('tasks_old');
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::create('tasks_old', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->nullable();
$table->unsignedInteger('server_id');
$table->tinyInteger('active')->default(1);
$table->string('action');
$table->text('data');
$table->unsignedTinyInteger('queued')->default(0);
$table->string('year')->default('*');
$table->string('month')->default('*');
$table->string('day_of_week')->default('*');
$table->string('day_of_month')->default('*');
$table->string('minute')->default('*');
$table->timestamp('last_run')->nullable();
$table->timestamp('next_run');
$table->timestamps();
});
}
}