From 07965d0ce704b3a5cb1d9a274c6cb6d43b6760e8 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 11 Sep 2017 01:15:44 -0500 Subject: [PATCH] These migrations... work?? :whale2: --- ...17_09_09_125253_AddChainedTasksAbility.php | 49 ------------ ..._09_09_162204_AddNullableNextRunColumn.php | 31 -------- ...9_RenameTasksTableForStructureRefactor.php | 23 ++++++ ...2017_09_10_225941_CreateSchedulesTable.php | 39 ++++++++++ ...230309_CreateNewTasksTableForSchedules.php | 36 +++++++++ ..._002938_TransferOldTasksToNewScheduler.php | 75 +++++++++++++++++++ 6 files changed, 173 insertions(+), 80 deletions(-) delete mode 100644 database/migrations/2017_09_09_125253_AddChainedTasksAbility.php delete mode 100644 database/migrations/2017_09_09_162204_AddNullableNextRunColumn.php create mode 100644 database/migrations/2017_09_10_225749_RenameTasksTableForStructureRefactor.php create mode 100644 database/migrations/2017_09_10_225941_CreateSchedulesTable.php create mode 100644 database/migrations/2017_09_10_230309_CreateNewTasksTableForSchedules.php create mode 100644 database/migrations/2017_09_11_002938_TransferOldTasksToNewScheduler.php diff --git a/database/migrations/2017_09_09_125253_AddChainedTasksAbility.php b/database/migrations/2017_09_09_125253_AddChainedTasksAbility.php deleted file mode 100644 index 8748e4092..000000000 --- a/database/migrations/2017_09_09_125253_AddChainedTasksAbility.php +++ /dev/null @@ -1,49 +0,0 @@ -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'); - }); - } -} diff --git a/database/migrations/2017_09_09_162204_AddNullableNextRunColumn.php b/database/migrations/2017_09_09_162204_AddNullableNextRunColumn.php deleted file mode 100644 index 40adb4013..000000000 --- a/database/migrations/2017_09_09_162204_AddNullableNextRunColumn.php +++ /dev/null @@ -1,31 +0,0 @@ -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;'); - }); - } -} diff --git a/database/migrations/2017_09_10_225749_RenameTasksTableForStructureRefactor.php b/database/migrations/2017_09_10_225749_RenameTasksTableForStructureRefactor.php new file mode 100644 index 000000000..12eada73c --- /dev/null +++ b/database/migrations/2017_09_10_225749_RenameTasksTableForStructureRefactor.php @@ -0,0 +1,23 @@ +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'); + } +} diff --git a/database/migrations/2017_09_10_230309_CreateNewTasksTableForSchedules.php b/database/migrations/2017_09_10_230309_CreateNewTasksTableForSchedules.php new file mode 100644 index 000000000..9c225a834 --- /dev/null +++ b/database/migrations/2017_09_10_230309_CreateNewTasksTableForSchedules.php @@ -0,0 +1,36 @@ +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'); + } +} diff --git a/database/migrations/2017_09_11_002938_TransferOldTasksToNewScheduler.php b/database/migrations/2017_09_11_002938_TransferOldTasksToNewScheduler.php new file mode 100644 index 000000000..2a20ef10e --- /dev/null +++ b/database/migrations/2017_09_11_002938_TransferOldTasksToNewScheduler.php @@ -0,0 +1,75 @@ +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(); + }); + } +}