diff --git a/database/migrations/2023_03_14_150900_create_app_users_table.php b/database/migrations/2023_03_14_150900_create_app_users_table.php index 9d410f9..abdcc68 100644 --- a/database/migrations/2023_03_14_150900_create_app_users_table.php +++ b/database/migrations/2023_03_14_150900_create_app_users_table.php @@ -2,6 +2,8 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +15,12 @@ return new class extends Migration */ public function up() { + DB::table('migrations')->truncate(); + Log::debug('truncate migrations'); + if (Schema::hasTable('app_users')) { + Log::debug('already has app_users'); + return; + } Schema::create('app_users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('email')->unique(); diff --git a/database/migrations/2023_03_14_150900_create_contacts_table.php b/database/migrations/2023_03_14_150900_create_contacts_table.php index 22a1405..cc2e6c0 100644 --- a/database/migrations/2023_03_14_150900_create_contacts_table.php +++ b/database/migrations/2023_03_14_150900_create_contacts_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if (Schema::hasTable('contacts')) { + Log::debug('already has contacts'); + return; + } Schema::create('contacts', function (Blueprint $table) { $table->string('id', 50)->primary(); $table->string('email'); diff --git a/database/migrations/2023_03_14_150900_create_directories_table.php b/database/migrations/2023_03_14_150900_create_directories_table.php index ab273b4..9915227 100644 --- a/database/migrations/2023_03_14_150900_create_directories_table.php +++ b/database/migrations/2023_03_14_150900_create_directories_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if (Schema::hasTable('directories')) { + Log::debug('already has directories'); + return; + } Schema::create('directories', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); diff --git a/database/migrations/2023_03_14_150900_create_documents_table.php b/database/migrations/2023_03_14_150900_create_documents_table.php index e651bd7..62161eb 100644 --- a/database/migrations/2023_03_14_150900_create_documents_table.php +++ b/database/migrations/2023_03_14_150900_create_documents_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if (Schema::hasTable('documents')) { + Log::debug('already has documents'); + return; + } Schema::create('documents', function (Blueprint $table) { $table->bigIncrements('id'); $table->char('uuid', 36)->nullable(); diff --git a/database/migrations/2023_03_14_150900_create_settings_table.php b/database/migrations/2023_03_14_150900_create_settings_table.php index 0a6408e..8fe1a7a 100644 --- a/database/migrations/2023_03_14_150900_create_settings_table.php +++ b/database/migrations/2023_03_14_150900_create_settings_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if (Schema::hasTable('settings')) { + Log::debug('already has settings'); + return; + } Schema::create('settings', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('key')->unique(); diff --git a/database/migrations/2023_03_14_150900_create_users_table.php b/database/migrations/2023_03_14_150900_create_users_table.php index 10e8072..f66e951 100644 --- a/database/migrations/2023_03_14_150900_create_users_table.php +++ b/database/migrations/2023_03_14_150900_create_users_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if (Schema::hasTable('users')) { + Log::debug('already has users'); + return; + } Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name', 100)->default('0'); diff --git a/database/migrations/2023_03_14_150901_add_foreign_keys_to_contacts_table.php b/database/migrations/2023_03_14_150901_add_foreign_keys_to_contacts_table.php index 9146a00..30d8012 100644 --- a/database/migrations/2023_03_14_150901_add_foreign_keys_to_contacts_table.php +++ b/database/migrations/2023_03_14_150901_add_foreign_keys_to_contacts_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if ($this->hasForeign('contacts', 'user_id', 'app_users', 'id')) { + Log::debug('already has foreign user_id'); + return; + } Schema::table('contacts', function (Blueprint $table) { $table->foreign(['user_id'])->references(['id'])->on('app_users'); }); @@ -29,4 +34,39 @@ return new class extends Migration $table->dropForeign('contacts_user_id_foreign'); }); } + + /** + * Check if a foreign key exists + * + * @param string $table + * @param string $column + * @param string $foreignTable + * @param string $foreignColumn + * @return boolean + */ + private function hasForeign( + string $table, + string $column, + string $foreignTable, + string $foreignColumn + ) { + + // * Get the foreign keys for the table + $fkColumns = Schema::getConnection() + ->getDoctrineSchemaManager() + ->listTableForeignKeys($table); + + // * Check if the foreign key exists + foreach ($fkColumns as $fkColumn) { + if ( + $fkColumn->getLocalColumns()[0] == $column && + $fkColumn->getForeignColumns()[0] == $foreignColumn && + $fkColumn->getForeignTableName() == $foreignTable + ) { + return true; + } + } + + return false; + } }; diff --git a/database/migrations/2023_03_14_150901_add_foreign_keys_to_directories_table.php b/database/migrations/2023_03_14_150901_add_foreign_keys_to_directories_table.php index 81af78f..a9c399d 100644 --- a/database/migrations/2023_03_14_150901_add_foreign_keys_to_directories_table.php +++ b/database/migrations/2023_03_14_150901_add_foreign_keys_to_directories_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if ($this->hasForeign('directories', 'parent', 'directories', 'id')) { + Log::debug('already has foreign parent'); + return; + } Schema::table('directories', function (Blueprint $table) { $table->foreign(['parent'])->references(['id'])->on('directories'); }); @@ -29,4 +34,39 @@ return new class extends Migration $table->dropForeign('directories_parent_foreign'); }); } + + /** + * Check if a foreign key exists + * + * @param string $table + * @param string $column + * @param string $foreignTable + * @param string $foreignColumn + * @return boolean + */ + private function hasForeign( + string $table, + string $column, + string $foreignTable, + string $foreignColumn + ) { + + // * Get the foreign keys for the table + $fkColumns = Schema::getConnection() + ->getDoctrineSchemaManager() + ->listTableForeignKeys($table); + + // * Check if the foreign key exists + foreach ($fkColumns as $fkColumn) { + if ( + $fkColumn->getLocalColumns()[0] == $column && + $fkColumn->getForeignColumns()[0] == $foreignColumn && + $fkColumn->getForeignTableName() == $foreignTable + ) { + return true; + } + } + + return false; + } }; diff --git a/database/migrations/2023_03_14_150901_add_foreign_keys_to_documents_table.php b/database/migrations/2023_03_14_150901_add_foreign_keys_to_documents_table.php index b09edf5..dfaa2a9 100644 --- a/database/migrations/2023_03_14_150901_add_foreign_keys_to_documents_table.php +++ b/database/migrations/2023_03_14_150901_add_foreign_keys_to_documents_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; return new class extends Migration @@ -13,6 +14,10 @@ return new class extends Migration */ public function up() { + if ($this->hasForeign('documents', 'directory', 'directories', 'id')) { + Log::debug('already has foreign directory'); + return; + } Schema::table('documents', function (Blueprint $table) { $table->foreign(['directory'])->references(['id'])->on('directories'); }); @@ -29,4 +34,39 @@ return new class extends Migration $table->dropForeign('documents_directory_foreign'); }); } + + /** + * Check if a foreign key exists + * + * @param string $table + * @param string $column + * @param string $foreignTable + * @param string $foreignColumn + * @return boolean + */ + private function hasForeign( + string $table, + string $column, + string $foreignTable, + string $foreignColumn + ) { + + // * Get the foreign keys for the table + $fkColumns = Schema::getConnection() + ->getDoctrineSchemaManager() + ->listTableForeignKeys($table); + + // * Check if the foreign key exists + foreach ($fkColumns as $fkColumn) { + if ( + $fkColumn->getLocalColumns()[0] == $column && + $fkColumn->getForeignColumns()[0] == $foreignColumn && + $fkColumn->getForeignTableName() == $foreignTable + ) { + return true; + } + } + + return false; + } }; diff --git a/database/migrations/2023_03_14_152343_create_jobs_table.php b/database/migrations/2023_03_14_152343_create_jobs_table.php index 1be9e8a..de4b6a8 100644 --- a/database/migrations/2023_03_14_152343_create_jobs_table.php +++ b/database/migrations/2023_03_14_152343_create_jobs_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; class CreateJobsTable extends Migration @@ -13,6 +14,10 @@ class CreateJobsTable extends Migration */ public function up() { + if (Schema::hasTable('jobs')) { + Log::debug('already has jobs'); + return; + } Schema::create('jobs', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('queue')->index(); diff --git a/database/migrations/2023_03_14_152416_create_failed_jobs_table.php b/database/migrations/2023_03_14_152416_create_failed_jobs_table.php index e39723c..013f280 100644 --- a/database/migrations/2023_03_14_152416_create_failed_jobs_table.php +++ b/database/migrations/2023_03_14_152416_create_failed_jobs_table.php @@ -2,6 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; class CreateFailedJobsTable extends Migration @@ -13,6 +14,10 @@ class CreateFailedJobsTable extends Migration */ public function up() { + if (Schema::hasTable('failed_jobs')) { + Log::debug('already has failed_jobs'); + return; + } Schema::create('failed_jobs', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('uuid')->unique(); diff --git a/database/migrations/2023_03_24_171035_update_app_users_table.php b/database/migrations/2023_03_24_171035_update_app_users_table.php index 213f0f7..d39cbbd 100644 --- a/database/migrations/2023_03_24_171035_update_app_users_table.php +++ b/database/migrations/2023_03_24_171035_update_app_users_table.php @@ -6,6 +6,7 @@ use App\Models\Directory; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; class UpdateAppUsersTable extends Migration @@ -32,8 +33,17 @@ class UpdateAppUsersTable extends Migration $appUsersOld = AppUser::get(); // * OTHERS + try { + Schema::table('app_users', function (Blueprint $table) { + $table->dropUnique('email'); + }); + } catch (\Exception $e) { + Log::debug('Failed to drop email unique'); + Schema::table('app_users', function (Blueprint $table) { + $table->dropUnique(['email']); + }); + } Schema::table('app_users', function (Blueprint $table) { - $table->dropUnique('email'); $table->unique(['email']); $table->renameColumn('surname', 'lastname'); $table->renameColumn('activated', 'status'); diff --git a/database/migrations/2023_04_05_093932_update_document_table.php b/database/migrations/2023_04_05_093932_update_document_table.php index e658f80..6330d5c 100644 --- a/database/migrations/2023_04_05_093932_update_document_table.php +++ b/database/migrations/2023_04_05_093932_update_document_table.php @@ -36,10 +36,10 @@ class UpdateDocumentTable extends Migration DB::statement('ALTER TABLE `documents` CHANGE `updated_at` `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'); self::updateData(); + Schema::disableForeignKeyConstraints(); Schema::table('documents', function (Blueprint $table) { $table->bigInteger('parent')->unsigned()->change(); $table->renameColumn('directory', 'directory_id'); - $table->dropColumn('parent'); $table->unsignedBigInteger('document_id')->index()->nullable()->after('directory'); $table->foreign(['document_id'])->references(['id'])->on('documents')->constrained(); $table->integer('order')->after('published')->comment('Ordonnance des documents.'); @@ -48,6 +48,7 @@ class UpdateDocumentTable extends Migration $table->renameColumn('published', 'status'); $table->dateTime('created_at')->default('CURRENT_TIMESTAMP')->nullable()->change(); }); + Schema::enableForeignKeyConstraints(); foreach ($this->documentsNew as $key => $documentNew) { $documentNew->document_id = ($this->documentsOld[$key]->parent !== null) ? intval($this->documentsOld[$key]->parent) : null; diff --git a/database/migrations/2023_04_18_144609_update_settings_table.php b/database/migrations/2023_04_18_144609_update_settings_table.php index 788f782..7dfed7a 100644 --- a/database/migrations/2023_04_18_144609_update_settings_table.php +++ b/database/migrations/2023_04_18_144609_update_settings_table.php @@ -4,6 +4,7 @@ use App\Models\Setting; use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Schema; class UpdateSettingsTable extends Migration @@ -19,8 +20,17 @@ class UpdateSettingsTable extends Migration //phpcs:ignore Generic.Files.LineLength.TooLong DB::statement('ALTER TABLE `settings` CHANGE `updated_at` `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'); + try { + Schema::table('settings', function (Blueprint $table) { + $table->dropUnique('data_key_unique'); + }); + } catch (\Exception $e) { + Log::debug('Failed to drop data_key_unique'); + Schema::table('settings', function (Blueprint $table) { + $table->dropUnique('settings_key_unique'); + }); + } Schema::table('settings', function (Blueprint $table) { - $table->dropUnique('data_key_unique'); $table->unique(['key']); $table->text('value')->nullable()->default(null)->change(); $table->dateTime('created_at')->default('CURRENT_TIMESTAMP')->nullable()->change(); diff --git a/database/migrations/2023_08_22_171154_add_missing_remember_token_to_users.php b/database/migrations/2023_08_22_171154_add_missing_remember_token_to_users.php index 9b4229a..c9832a0 100644 --- a/database/migrations/2023_08_22_171154_add_missing_remember_token_to_users.php +++ b/database/migrations/2023_08_22_171154_add_missing_remember_token_to_users.php @@ -1,7 +1,9 @@ getDoctrineSchemaManager() + ->listTableColumns('users') + )->mapWithKeys(function (Column $column) { + return [$column->getName() => $column]; + }); + if ($columns->has('remember_token')) { + Log::debug('already has remember_token column'); + return; + } Schema::table('users', function (Blueprint $table) { $table->rememberToken()->after('password'); }); diff --git a/database/migrations/2023_08_25_093125_upgrade_contacts_table.php b/database/migrations/2023_08_25_093125_upgrade_contacts_table.php index d992fc1..9322f9d 100644 --- a/database/migrations/2023_08_25_093125_upgrade_contacts_table.php +++ b/database/migrations/2023_08_25_093125_upgrade_contacts_table.php @@ -31,7 +31,7 @@ class UpgradeContactsTable extends Migration $table->string('firstname')->nullable()->comment('Prénom du contact, Form Status: Locked')->change(); $table->string('phone')->nullable()->comment('Téléphone du contact, Form Status: Locked')->change(); $table->text('notes')->nullable()->comment('Notes sur un contact')->change(); - $table->text('visits')->default('[]') + $table->json('visits')->default('[]') ->comment('Visite d\'un contact, Array<{title:string,note:string}>')->change(); $table->dropColumn('type');