From 16a72f52c20b69828f4cb8ebda6f88e1e17be2b4 Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Tue, 9 Nov 2021 10:37:08 +0200 Subject: [PATCH] Task 5 - column or table exists --- README.md | 10 +++++ .../2021_11_09_083051_create_users_table.php | 36 ++++++++++++++++++ .../2021_11_09_083121_update_users_table.php | 31 ++++++++++++++++ ...2021_11_09_083225_recreate_users_table.php | 37 +++++++++++++++++++ tests/Feature/MigrationsTest.php | 9 ++++- 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 database/migrations/task5/2021_11_09_083051_create_users_table.php create mode 100644 database/migrations/task5/2021_11_09_083121_update_users_table.php create mode 100644 database/migrations/task5/2021_11_09_083225_recreate_users_table.php diff --git a/README.md b/README.md index 93a5bb7..f4551d6 100644 --- a/README.md +++ b/README.md @@ -69,3 +69,13 @@ Test method `test_delete_parent_child_record()`. --- +## Task 5. Check if Table/Column Exists + +Folder `database/migrations/task5` contains migrations for users table. By mistake, some developer tries to add the column that already exists, and re-create the users table that already exists. + +You need to modify the migrations to ignore those operations if the column/table exists. So "php artisan migrate" should run successfully, without errors. + +Test method `test_repeating_column_table()`. + +--- + diff --git a/database/migrations/task5/2021_11_09_083051_create_users_table.php b/database/migrations/task5/2021_11_09_083051_create_users_table.php new file mode 100644 index 0000000..621a24e --- /dev/null +++ b/database/migrations/task5/2021_11_09_083051_create_users_table.php @@ -0,0 +1,36 @@ +id(); + $table->string('name'); + $table->string('email')->unique(); + $table->timestamp('email_verified_at')->nullable(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('users'); + } +} diff --git a/database/migrations/task5/2021_11_09_083121_update_users_table.php b/database/migrations/task5/2021_11_09_083121_update_users_table.php new file mode 100644 index 0000000..c10976a --- /dev/null +++ b/database/migrations/task5/2021_11_09_083121_update_users_table.php @@ -0,0 +1,31 @@ +string('name'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/database/migrations/task5/2021_11_09_083225_recreate_users_table.php b/database/migrations/task5/2021_11_09_083225_recreate_users_table.php new file mode 100644 index 0000000..6b15a7c --- /dev/null +++ b/database/migrations/task5/2021_11_09_083225_recreate_users_table.php @@ -0,0 +1,37 @@ +id(); + $table->string('name'); + $table->string('email')->unique(); + $table->timestamp('email_verified_at')->nullable(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/tests/Feature/MigrationsTest.php b/tests/Feature/MigrationsTest.php index e76f1c1..b6f0a56 100644 --- a/tests/Feature/MigrationsTest.php +++ b/tests/Feature/MigrationsTest.php @@ -40,7 +40,6 @@ class MigrationsTest extends TestCase } $this->assertEquals(3, $fieldNumber); - } public function test_soft_deletes() @@ -65,4 +64,12 @@ class MigrationsTest extends TestCase Product::factory()->create(); $category->delete(); } + + public function test_repeating_column_table() + { + // We just test if the migration succeeds or throws an exception + $this->expectNotToPerformAssertions(); + + Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task5']); + } }