Task 5 - column or table exists

This commit is contained in:
PovilasKorop
2021-11-09 10:37:08 +02:00
parent 32a21d2c05
commit 16a72f52c2
5 changed files with 122 additions and 1 deletions
+10
View File
@@ -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()`.
---
@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->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');
}
}
@@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// TASK: add an if-statement in this file to NOT add column if it already exists
Schema::table('users', function (Blueprint $table) {
$table->string('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RecreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// TASK: add an if-statement in this file to NOT create table if it already exists
Schema::create('users', function (Blueprint $table) {
$table->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()
{
//
}
}
+8 -1
View File
@@ -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']);
}
}