Migrations: Removed references to models to prevent further migration issues

This commit is contained in:
Igor Scheller 2022-10-21 15:40:14 +02:00
parent 3964982db0
commit c0a39cb2a1
9 changed files with 146 additions and 123 deletions

View File

@ -4,13 +4,13 @@ namespace Engelsystem\Migrations;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\EventConfig;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
class CreateEventConfigTable extends Migration class CreateEventConfigTable extends Migration
{ {
protected $mapping = [ protected array $mapping = [
'buildup_start_date' => 'buildup_start', 'buildup_start_date' => 'buildup_start',
'event_start_date' => 'event_start', 'event_start_date' => 'event_start',
'event_end_date' => 'event_end', 'event_end_date' => 'event_end',
@ -41,26 +41,24 @@ class CreateEventConfigTable extends Migration
} }
if ($this->schema->hasTable('EventConfig')) { if ($this->schema->hasTable('EventConfig')) {
$config = $this->schema->getConnection() $connection = $this->schema->getConnection();
$config = $connection
->table('EventConfig') ->table('EventConfig')
->first(); ->first();
if (!empty($config)) { if (!empty($config)) {
(new EventConfig([ $connection->table('event_config')
'name' => 'name', ->insert([
'value' => $config->event_name, ['name' => 'name', 'value' => $config->event_name],
]))->save(); ['name' => 'welcome_msg', 'value' => $config->event_welcome_msg],
]);
(new EventConfig([
'name' => 'welcome_msg',
'value' => $config->event_welcome_msg,
]))->save();
foreach ($this->mapping as $old => $new) { foreach ($this->mapping as $old => $new) {
(new EventConfig([ $connection->table('event_config')
->insert([
'name' => $new, 'name' => $new,
'value' => (new Carbon())->setTimestamp($config->{$old}), 'value' => (new Carbon())->setTimestamp($config->{$old}),
]))->save(); ]);
} }
} }
@ -73,6 +71,8 @@ class CreateEventConfigTable extends Migration
*/ */
public function down() public function down()
{ {
$connection = $this->schema->getConnection();
$this->schema->create('EventConfig', function (Blueprint $table) { $this->schema->create('EventConfig', function (Blueprint $table) {
$table->string('event_name')->nullable(); $table->string('event_name')->nullable();
$table->integer('buildup_start_date')->nullable(); $table->integer('buildup_start_date')->nullable();
@ -82,19 +82,19 @@ class CreateEventConfigTable extends Migration
$table->string('event_welcome_msg')->nullable(); $table->string('event_welcome_msg')->nullable();
}); });
$config = new EventConfig(); $config = $connection->table('event_config')->get();
$data = [ $data = [
'event_name' => $config->findOrNew('name')->value, 'event_name' => $this->getConfigValue($config, 'name'),
'event_welcome_msg' => $config->findOrNew('welcome_msg')->value, 'event_welcome_msg' => $this->getConfigValue($config, 'welcome_msg'),
]; ];
foreach ($this->mapping as $new => $old) { foreach ($this->mapping as $new => $old) {
/** @var Carbon $value */ $value = $this->getConfigValue($config, $old);
$value = $config->findOrNew($old)->value;
if (!$value) { if (!$value) {
continue; continue;
} }
$value = Carbon::make($value);
$data[$new] = $value->getTimestamp(); $data[$new] = $value->getTimestamp();
} }
@ -111,4 +111,16 @@ class CreateEventConfigTable extends Migration
$this->schema->dropIfExists('event_config'); $this->schema->dropIfExists('event_config');
} }
/**
* @param Collection $config
* @param string $name
* @return mixed|null
*/
protected function getConfigValue(Collection $config, string $name)
{
$value = $config->where('name', $name)->first('value', (object)['value' => null])->value;
return $value ? json_decode($value, true) : null;
}
} }

View File

@ -4,12 +4,6 @@ namespace Engelsystem\Migrations;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\User\Contact;
use Engelsystem\Models\User\PasswordReset;
use Engelsystem\Models\User\PersonalData;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\State;
use Engelsystem\Models\User\User;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use stdClass; use stdClass;
@ -83,33 +77,33 @@ class CreateUsersTables extends Migration
}); });
if ($this->schema->hasTable('User')) { if ($this->schema->hasTable('User')) {
$connection = $this->schema->getConnection();
$emptyDates = ['0000-00-00 00:00:00', '0001-01-01 00:00:00', '1000-01-01 00:00:00']; $emptyDates = ['0000-00-00 00:00:00', '0001-01-01 00:00:00', '1000-01-01 00:00:00'];
/** @var stdClass[] $users */ /** @var stdClass[] $users */
$users = $this->schema->getConnection()->table('User')->get(); $users = $connection->table('User')->get();
foreach ($users as $data) { foreach ($users as $data) {
$user = new User([ $user = [
'id' => $data->UID,
'name' => $data->Nick, 'name' => $data->Nick,
'password' => $data->Passwort, 'password' => $data->Passwort,
'email' => $data->email, 'email' => $data->email,
'api_key' => $data->api_key, 'api_key' => $data->api_key,
'last_login_at' => $data->lastLogIn ? Carbon::createFromTimestamp($data->lastLogIn) : null, 'last_login_at' => $data->lastLogIn ? Carbon::createFromTimestamp($data->lastLogIn) : null,
]); ];
$user->setAttribute('id', $data->UID);
if (!in_array($data->CreateDate, $emptyDates)) { if (!in_array($data->CreateDate, $emptyDates)) {
$user->setAttribute('created_at', new Carbon($data->CreateDate)); $user['created_at'] = new Carbon($data->CreateDate);
} }
$user->save(); $connection->table('users')->insert($user);
$contact = new Contact([ $connection->table('users_contact')->insert([
'dect' => $data->DECT ? $data->DECT : null, 'user_id' => $data->UID,
'dect' => $data->DECT ?: null,
'mobile' => $data->Handy ?: ($data->Telefon ?: null), 'mobile' => $data->Handy ?: ($data->Telefon ?: null),
]); ]);
$contact->user()
->associate($user)
->save();
$personalData = new PersonalData([ $connection->table('users_personal_data')->insert([
'user_id' => $data->UID,
'first_name' => $data->Vorname ?: null, 'first_name' => $data->Vorname ?: null,
'last_name' => $data->Name ?: null, 'last_name' => $data->Name ?: null,
'shirt_size' => $data->Size ?: null, 'shirt_size' => $data->Size ?: null,
@ -120,25 +114,17 @@ class CreateUsersTables extends Migration
? Carbon::createFromTimestamp($data->planned_departure_date) ? Carbon::createFromTimestamp($data->planned_departure_date)
: null, : null,
]); ]);
$personalData->user()
->associate($user)
->save();
$settings = new Settings([ $connection->table('users_settings')->insert([
'user_id' => $data->UID,
'language' => $data->Sprache, 'language' => $data->Sprache,
'theme' => $data->color, 'theme' => $data->color,
'email_human' => $data->email_by_human_allowed, 'email_human' => $data->email_by_human_allowed,
'email_shiftinfo' => $data->email_shiftinfo, 'email_shiftinfo' => $data->email_shiftinfo,
]); ]);
unset($settings->email_news);
unset($settings->email_goody);
unset($settings->mobile_show);
$settings->user() $connection->table('users_state')->insert([
->associate($user) 'user_id' => $data->UID,
->save();
$state = new State([
'arrived' => $data->Gekommen, 'arrived' => $data->Gekommen,
'arrival_date' => $data->arrival_date ? Carbon::createFromTimestamp($data->arrival_date) : null, 'arrival_date' => $data->arrival_date ? Carbon::createFromTimestamp($data->arrival_date) : null,
'active' => $data->Aktiv, 'active' => $data->Aktiv,
@ -146,17 +132,12 @@ class CreateUsersTables extends Migration
'got_shirt' => $data->Tshirt, 'got_shirt' => $data->Tshirt,
'got_voucher' => $data->got_voucher, 'got_voucher' => $data->got_voucher,
]); ]);
$state->user()
->associate($user)
->save();
if ($data->password_recovery_token) { if ($data->password_recovery_token) {
$passwordReset = new PasswordReset([ $connection->table('password_resets')->insert([
'user_id' => $data->UID,
'token' => $data->password_recovery_token, 'token' => $data->password_recovery_token,
]); ]);
$passwordReset->user()
->associate($user)
->save();
} }
} }
@ -176,8 +157,10 @@ class CreateUsersTables extends Migration
*/ */
public function down() public function down()
{ {
$connection = $this->schema->getConnection();
$this->schema->create('User', function (Blueprint $table) { $this->schema->create('User', function (Blueprint $table) {
$table->integer('UID', true, false); $table->integer('UID', true);
$table->string('Nick', 23)->unique()->default(''); $table->string('Nick', 23)->unique()->default('');
$table->string('Name', 23)->nullable(); $table->string('Name', 23)->nullable();
@ -218,12 +201,16 @@ class CreateUsersTables extends Migration
$table->index('planned_departure_date', 'planned_departure_date'); $table->index('planned_departure_date', 'planned_departure_date');
}); });
foreach (User::all() as $user) { foreach ($connection->table('users')->get() as $user) {
/** @var User $user */ /** @var stdClass $user */
$contact = $user->contact; /** @var stdClass $contact */
$personal = $user->personalData; $contact = $connection->table('users_contact')->where('user_id', $user->id)->first();
$settings = $user->settings; /** @var stdClass $personal */
$state = $user->state; $personal = $connection->table('users_personal_data')->where('user_id', $user->id)->first();
/** @var stdClass $settings */
$settings = $connection->table('users_settings')->where('user_id', $user->id)->first();
/** @var stdClass $state */
$state = $connection->table('users_state')->where('user_id', $user->id)->first();
$this->schema $this->schema
->getConnection() ->getConnection()
@ -245,23 +232,28 @@ class CreateUsersTables extends Migration
'Tshirt' => $state->got_shirt, 'Tshirt' => $state->got_shirt,
'color' => $settings->theme, 'color' => $settings->theme,
'Sprache' => $settings->language, 'Sprache' => $settings->language,
'lastLogIn' => $user->last_login_at ? $user->last_login_at->getTimestamp() : null, 'lastLogIn' => $user->last_login_at
'CreateDate' => $user->created_at ? $user->created_at->toDateTimeString() : null, ? Carbon::make($user->last_login_at)->getTimestamp()
: null,
'CreateDate' => $user->created_at
? Carbon::make($user->created_at)->toDateTimeString()
: '0001-01-01 00:00:00',
'api_key' => $user->api_key, 'api_key' => $user->api_key,
'got_voucher' => $state->got_voucher, 'got_voucher' => $state->got_voucher,
'arrival_date' => $state->arrival_date ? $state->arrival_date->getTimestamp() : null, 'arrival_date' => $state->arrival_date
'planned_arrival_date' => $personal->planned_arrival_date ? Carbon::make($state->arrival_date)->getTimestamp()
? $personal->planned_arrival_date->getTimestamp()
: null, : null,
'planned_arrival_date' => $personal->planned_arrival_date
? Carbon::make($personal->planned_arrival_date)->getTimestamp()
: 0,
'planned_departure_date' => $personal->planned_departure_date 'planned_departure_date' => $personal->planned_departure_date
? $personal->planned_departure_date->getTimestamp() ? Carbon::make($personal->planned_departure_date)->getTimestamp()
: null, : null,
'email_by_human_allowed' => $settings->email_human, 'email_by_human_allowed' => $settings->email_human,
]); ]);
} }
foreach (PasswordReset::all() as $passwordReset) { foreach ($connection->table('password_resets')->get() as $passwordReset) {
/** @var PasswordReset $passwordReset */
$this->schema->getConnection() $this->schema->getConnection()
->table('User') ->table('User')
->where('UID', '=', $passwordReset->user_id) ->where('UID', '=', $passwordReset->user_id)

View File

@ -3,7 +3,7 @@
namespace Engelsystem\Migrations; namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\User\State; use stdClass;
class FixMissingArrivalDates extends Migration class FixMissingArrivalDates extends Migration
{ {
@ -12,10 +12,24 @@ class FixMissingArrivalDates extends Migration
*/ */
public function up() public function up()
{ {
$states = State::whereArrived(true)->whereArrivalDate(null)->get(); $connection = $this->schema->getConnection();
/** @var stdClass[] $states */
$states = $connection
->table('users_state')
->where('arrived', true)
->where('arrival_date', null)
->get();
foreach ($states as $state) { foreach ($states as $state) {
$state->arrival_date = $state->user->personalData->planned_arrival_date; /** @var stdClass $personalData */
$state->save(); $personalData = $connection
->table('users_personal_data')
->where('user_id', $state->user_id)
->first();
$state->arrival_date = $personalData->planned_arrival_date;
$connection->table('users_state')
->update((array)$state);
} }
} }

View File

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Engelsystem\Migrations; namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\Room;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use stdClass; use stdClass;
@ -35,13 +34,13 @@ class CreateRoomsTable extends Migration
->get(); ->get();
foreach ($previousRecords as $previousRecord) { foreach ($previousRecords as $previousRecord) {
$room = new Room([ $connection->table('rooms')
->insert([
'id' => $previousRecord->RID,
'name' => $previousRecord->Name, 'name' => $previousRecord->Name,
'map_url' => $previousRecord->map_url ?: null, 'map_url' => $previousRecord->map_url ?: null,
'description' => $previousRecord->description ?: null, 'description' => $previousRecord->description ?: null,
]); ]);
$room->setAttribute('id', $previousRecord->RID);
$room->save();
} }
$this->changeReferences( $this->changeReferences(
@ -60,6 +59,8 @@ class CreateRoomsTable extends Migration
*/ */
public function down(): void public function down(): void
{ {
$connection = $this->schema->getConnection();
$this->schema->create('Room', function (Blueprint $table) { $this->schema->create('Room', function (Blueprint $table) {
$table->increments('RID'); $table->increments('RID');
$table->string('Name', 35)->unique(); $table->string('Name', 35)->unique();
@ -67,9 +68,9 @@ class CreateRoomsTable extends Migration
$table->mediumText('description')->nullable(); $table->mediumText('description')->nullable();
}); });
foreach (Room::all() as $room) { foreach ($connection->table('rooms')->get() as $room) {
$this->schema /** @var stdClass $room */
->getConnection() $connection
->table('Room') ->table('Room')
->insert([ ->insert([
'RID' => $room->id, 'RID' => $room->id,

View File

@ -6,7 +6,6 @@ namespace Engelsystem\Migrations;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\Worklog;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use stdClass; use stdClass;
@ -38,18 +37,19 @@ class CreateWorklogsTable extends Migration
->get(); ->get();
foreach ($previousRecords as $previousRecord) { foreach ($previousRecords as $previousRecord) {
$room = new Worklog([ $created_at = Carbon::createFromTimestamp($previousRecord->created_timestamp);
$this->schema->getConnection()
->table('worklogs')
->insert([
'id' => $previousRecord->id,
'user_id' => $previousRecord->user_id, 'user_id' => $previousRecord->user_id,
'creator_id' => $previousRecord->created_user_id, 'creator_id' => $previousRecord->created_user_id,
'worked_at' => $previousRecord->work_timestamp, 'worked_at' => $previousRecord->work_timestamp,
'hours' => $previousRecord->work_hours, 'hours' => $previousRecord->work_hours,
'comment' => $previousRecord->comment, 'comment' => $previousRecord->comment,
'created_at' => $created_at,
'updated_at' => $created_at,
]); ]);
$created_at = Carbon::createFromTimestamp($previousRecord->created_timestamp);
$room->setAttribute('id', $previousRecord->id);
$room->setAttribute('created_at', $created_at);
$room->setAttribute('updated_at', $created_at);
$room->save();
} }
$this->changeReferences( $this->changeReferences(
@ -68,6 +68,8 @@ class CreateWorklogsTable extends Migration
*/ */
public function down(): void public function down(): void
{ {
$connection = $this->schema->getConnection();
$this->schema->create('UserWorkLog', function (Blueprint $table) { $this->schema->create('UserWorkLog', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$this->referencesUser($table); $this->referencesUser($table);
@ -78,10 +80,9 @@ class CreateWorklogsTable extends Migration
$table->integer('created_timestamp')->index(); $table->integer('created_timestamp')->index();
}); });
foreach (Worklog::all() as $record) { foreach ($connection->table('worklogs')->get() as $record) {
/** @var Worklog $record */ /** @var stdClass $record */
$this->schema $connection
->getConnection()
->table('UserWorkLog') ->table('UserWorkLog')
->insert([ ->insert([
'id' => $record->id, 'id' => $record->id,

View File

@ -4,7 +4,6 @@ namespace Engelsystem\Migrations;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\Question;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
class AddTimestampsToQuestions extends Migration class AddTimestampsToQuestions extends Migration
@ -16,17 +15,20 @@ class AddTimestampsToQuestions extends Migration
*/ */
public function up(): void public function up(): void
{ {
$connection = $this->schema->getConnection();
$now = Carbon::now();
$this->schema->table('questions', function (Blueprint $table) { $this->schema->table('questions', function (Blueprint $table) {
$table->timestamp('answered_at')->after('answerer_id')->nullable(); $table->timestamp('answered_at')->after('answerer_id')->nullable();
$table->timestamps(); $table->timestamps();
}); });
$now = Carbon::now(); $connection->table('questions')
Question::query()->update([ ->update([
'created_at' => $now, 'created_at' => $now,
'updated_at' => $now, 'updated_at' => $now,
]); ]);
Question::query() $connection->table('questions')
->whereNotNull('answerer_id') ->whereNotNull('answerer_id')
->update([ ->update([
'answered_at' => $now, 'answered_at' => $now,

View File

@ -4,7 +4,6 @@ namespace Engelsystem\Migrations;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\Shifts\Schedule;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
class AddNameMinutesAndTimestampsToSchedules extends Migration class AddNameMinutesAndTimestampsToSchedules extends Migration
@ -16,6 +15,8 @@ class AddNameMinutesAndTimestampsToSchedules extends Migration
*/ */
public function up() public function up()
{ {
$connection = $this->schema->getConnection();
$this->schema->table( $this->schema->table(
'schedules', 'schedules',
function (Blueprint $table) { function (Blueprint $table) {
@ -27,7 +28,7 @@ class AddNameMinutesAndTimestampsToSchedules extends Migration
} }
); );
Schedule::query() $connection->table('schedules')
->update([ ->update([
'created_at' => Carbon::now(), 'created_at' => Carbon::now(),
'minutes_before' => 15, 'minutes_before' => 15,
@ -46,7 +47,6 @@ class AddNameMinutesAndTimestampsToSchedules extends Migration
// Add legacy reference // Add legacy reference
if ($this->schema->hasTable('ShiftTypes')) { if ($this->schema->hasTable('ShiftTypes')) {
$connection = $this->schema->getConnection();
$query = $connection $query = $connection
->table('Shifts') ->table('Shifts')
->select('Shifts.shifttype_id') ->select('Shifts.shifttype_id')
@ -54,8 +54,10 @@ class AddNameMinutesAndTimestampsToSchedules extends Migration
->where('schedule_shift.schedule_id', $connection->raw('schedules.id')) ->where('schedule_shift.schedule_id', $connection->raw('schedules.id'))
->limit(1); ->limit(1);
Schedule::query() $connection->table('schedules')
->update(['shift_type' => $connection->raw('(' . $query->toSql() . ')')]); ->update([
'shift_type' => $connection->raw('(' . $query->toSql() . ')')
]);
$this->schema->table( $this->schema->table(
'schedules', 'schedules',

View File

@ -5,7 +5,6 @@ namespace Engelsystem\Migrations;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Models\User\User;
use Illuminate\Database\Schema\Builder as SchemaBuilder; use Illuminate\Database\Schema\Builder as SchemaBuilder;
class SetAdminPassword extends Migration class SetAdminPassword extends Migration
@ -13,10 +12,10 @@ class SetAdminPassword extends Migration
use Reference; use Reference;
/** @var Authenticator */ /** @var Authenticator */
protected $auth; protected Authenticator $auth;
/** @var Config */ /** @var Config */
protected $config; protected Config $config;
/** /**
* @param SchemaBuilder $schemaBuilder * @param SchemaBuilder $schemaBuilder
@ -36,7 +35,6 @@ class SetAdminPassword extends Migration
*/ */
public function up() public function up()
{ {
/** @var User $admin */
$admin = $this->auth->authenticate('admin', 'asdfasdf'); $admin = $this->auth->authenticate('admin', 'asdfasdf');
$setupPassword = $this->config->get('setup_admin_password'); $setupPassword = $this->config->get('setup_admin_password');
if (!$admin || !$setupPassword) { if (!$admin || !$setupPassword) {

View File

@ -14,6 +14,8 @@ class UsersSettingsAddEmailGoody extends Migration
*/ */
public function up() public function up()
{ {
$connection = $this->schema->getConnection();
$this->schema->table( $this->schema->table(
'users_settings', 'users_settings',
function (Blueprint $table) { function (Blueprint $table) {
@ -21,7 +23,6 @@ class UsersSettingsAddEmailGoody extends Migration
} }
); );
$connection = $this->schema->getConnection();
$connection $connection
->table('users_settings') ->table('users_settings')
->update(['email_goody' => $connection->raw('email_human')]); ->update(['email_goody' => $connection->raw('email_human')]);