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 Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\EventConfig;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Collection;
class CreateEventConfigTable extends Migration
{
protected $mapping = [
protected array $mapping = [
'buildup_start_date' => 'buildup_start',
'event_start_date' => 'event_start',
'event_end_date' => 'event_end',
@ -41,26 +41,24 @@ class CreateEventConfigTable extends Migration
}
if ($this->schema->hasTable('EventConfig')) {
$config = $this->schema->getConnection()
$connection = $this->schema->getConnection();
$config = $connection
->table('EventConfig')
->first();
if (!empty($config)) {
(new EventConfig([
'name' => 'name',
'value' => $config->event_name,
]))->save();
(new EventConfig([
'name' => 'welcome_msg',
'value' => $config->event_welcome_msg,
]))->save();
$connection->table('event_config')
->insert([
['name' => 'name', 'value' => $config->event_name],
['name' => 'welcome_msg', 'value' => $config->event_welcome_msg],
]);
foreach ($this->mapping as $old => $new) {
(new EventConfig([
'name' => $new,
'value' => (new Carbon())->setTimestamp($config->{$old}),
]))->save();
$connection->table('event_config')
->insert([
'name' => $new,
'value' => (new Carbon())->setTimestamp($config->{$old}),
]);
}
}
@ -73,6 +71,8 @@ class CreateEventConfigTable extends Migration
*/
public function down()
{
$connection = $this->schema->getConnection();
$this->schema->create('EventConfig', function (Blueprint $table) {
$table->string('event_name')->nullable();
$table->integer('buildup_start_date')->nullable();
@ -82,19 +82,19 @@ class CreateEventConfigTable extends Migration
$table->string('event_welcome_msg')->nullable();
});
$config = new EventConfig();
$config = $connection->table('event_config')->get();
$data = [
'event_name' => $config->findOrNew('name')->value,
'event_welcome_msg' => $config->findOrNew('welcome_msg')->value,
'event_name' => $this->getConfigValue($config, 'name'),
'event_welcome_msg' => $this->getConfigValue($config, 'welcome_msg'),
];
foreach ($this->mapping as $new => $old) {
/** @var Carbon $value */
$value = $config->findOrNew($old)->value;
$value = $this->getConfigValue($config, $old);
if (!$value) {
continue;
}
$value = Carbon::make($value);
$data[$new] = $value->getTimestamp();
}
@ -111,4 +111,16 @@ class CreateEventConfigTable extends Migration
$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 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 stdClass;
@ -83,33 +77,33 @@ class CreateUsersTables extends Migration
});
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'];
/** @var stdClass[] $users */
$users = $this->schema->getConnection()->table('User')->get();
$users = $connection->table('User')->get();
foreach ($users as $data) {
$user = new User([
$user = [
'id' => $data->UID,
'name' => $data->Nick,
'password' => $data->Passwort,
'email' => $data->email,
'api_key' => $data->api_key,
'last_login_at' => $data->lastLogIn ? Carbon::createFromTimestamp($data->lastLogIn) : null,
]);
$user->setAttribute('id', $data->UID);
];
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([
'dect' => $data->DECT ? $data->DECT : null,
'mobile' => $data->Handy ?: ($data->Telefon ?: null),
$connection->table('users_contact')->insert([
'user_id' => $data->UID,
'dect' => $data->DECT ?: 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,
'last_name' => $data->Name ?: null,
'shirt_size' => $data->Size ?: null,
@ -120,25 +114,17 @@ class CreateUsersTables extends Migration
? Carbon::createFromTimestamp($data->planned_departure_date)
: null,
]);
$personalData->user()
->associate($user)
->save();
$settings = new Settings([
$connection->table('users_settings')->insert([
'user_id' => $data->UID,
'language' => $data->Sprache,
'theme' => $data->color,
'email_human' => $data->email_by_human_allowed,
'email_shiftinfo' => $data->email_shiftinfo,
]);
unset($settings->email_news);
unset($settings->email_goody);
unset($settings->mobile_show);
$settings->user()
->associate($user)
->save();
$state = new State([
$connection->table('users_state')->insert([
'user_id' => $data->UID,
'arrived' => $data->Gekommen,
'arrival_date' => $data->arrival_date ? Carbon::createFromTimestamp($data->arrival_date) : null,
'active' => $data->Aktiv,
@ -146,17 +132,12 @@ class CreateUsersTables extends Migration
'got_shirt' => $data->Tshirt,
'got_voucher' => $data->got_voucher,
]);
$state->user()
->associate($user)
->save();
if ($data->password_recovery_token) {
$passwordReset = new PasswordReset([
'token' => $data->password_recovery_token,
$connection->table('password_resets')->insert([
'user_id' => $data->UID,
'token' => $data->password_recovery_token,
]);
$passwordReset->user()
->associate($user)
->save();
}
}
@ -176,8 +157,10 @@ class CreateUsersTables extends Migration
*/
public function down()
{
$connection = $this->schema->getConnection();
$this->schema->create('User', function (Blueprint $table) {
$table->integer('UID', true, false);
$table->integer('UID', true);
$table->string('Nick', 23)->unique()->default('');
$table->string('Name', 23)->nullable();
@ -218,12 +201,16 @@ class CreateUsersTables extends Migration
$table->index('planned_departure_date', 'planned_departure_date');
});
foreach (User::all() as $user) {
/** @var User $user */
$contact = $user->contact;
$personal = $user->personalData;
$settings = $user->settings;
$state = $user->state;
foreach ($connection->table('users')->get() as $user) {
/** @var stdClass $user */
/** @var stdClass $contact */
$contact = $connection->table('users_contact')->where('user_id', $user->id)->first();
/** @var stdClass $personal */
$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
->getConnection()
@ -245,23 +232,28 @@ class CreateUsersTables extends Migration
'Tshirt' => $state->got_shirt,
'color' => $settings->theme,
'Sprache' => $settings->language,
'lastLogIn' => $user->last_login_at ? $user->last_login_at->getTimestamp() : null,
'CreateDate' => $user->created_at ? $user->created_at->toDateTimeString() : null,
'lastLogIn' => $user->last_login_at
? 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,
'got_voucher' => $state->got_voucher,
'arrival_date' => $state->arrival_date ? $state->arrival_date->getTimestamp() : null,
'planned_arrival_date' => $personal->planned_arrival_date
? $personal->planned_arrival_date->getTimestamp()
'arrival_date' => $state->arrival_date
? Carbon::make($state->arrival_date)->getTimestamp()
: null,
'planned_arrival_date' => $personal->planned_arrival_date
? Carbon::make($personal->planned_arrival_date)->getTimestamp()
: 0,
'planned_departure_date' => $personal->planned_departure_date
? $personal->planned_departure_date->getTimestamp()
? Carbon::make($personal->planned_departure_date)->getTimestamp()
: null,
'email_by_human_allowed' => $settings->email_human,
]);
}
foreach (PasswordReset::all() as $passwordReset) {
/** @var PasswordReset $passwordReset */
foreach ($connection->table('password_resets')->get() as $passwordReset) {
$this->schema->getConnection()
->table('User')
->where('UID', '=', $passwordReset->user_id)

View File

@ -3,7 +3,7 @@
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\User\State;
use stdClass;
class FixMissingArrivalDates extends Migration
{
@ -12,10 +12,24 @@ class FixMissingArrivalDates extends Migration
*/
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) {
$state->arrival_date = $state->user->personalData->planned_arrival_date;
$state->save();
/** @var stdClass $personalData */
$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;
use Engelsystem\Database\Migration\Migration;
use Engelsystem\Models\Room;
use Illuminate\Database\Schema\Blueprint;
use stdClass;
@ -35,13 +34,13 @@ class CreateRoomsTable extends Migration
->get();
foreach ($previousRecords as $previousRecord) {
$room = new Room([
'name' => $previousRecord->Name,
'map_url' => $previousRecord->map_url ?: null,
'description' => $previousRecord->description ?: null,
]);
$room->setAttribute('id', $previousRecord->RID);
$room->save();
$connection->table('rooms')
->insert([
'id' => $previousRecord->RID,
'name' => $previousRecord->Name,
'map_url' => $previousRecord->map_url ?: null,
'description' => $previousRecord->description ?: null,
]);
}
$this->changeReferences(
@ -60,6 +59,8 @@ class CreateRoomsTable extends Migration
*/
public function down(): void
{
$connection = $this->schema->getConnection();
$this->schema->create('Room', function (Blueprint $table) {
$table->increments('RID');
$table->string('Name', 35)->unique();
@ -67,9 +68,9 @@ class CreateRoomsTable extends Migration
$table->mediumText('description')->nullable();
});
foreach (Room::all() as $room) {
$this->schema
->getConnection()
foreach ($connection->table('rooms')->get() as $room) {
/** @var stdClass $room */
$connection
->table('Room')
->insert([
'RID' => $room->id,

View File

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

View File

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

View File

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

View File

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

View File

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