mirror of
https://github.com/10h30/Test-Laravel-Migrations.git
synced 2026-07-11 19:05:54 +09:00
Task 5 - column or table exists
This commit is contained in:
@@ -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()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,7 +40,6 @@ class MigrationsTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->assertEquals(3, $fieldNumber);
|
$this->assertEquals(3, $fieldNumber);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_soft_deletes()
|
public function test_soft_deletes()
|
||||||
@@ -65,4 +64,12 @@ class MigrationsTest extends TestCase
|
|||||||
Product::factory()->create();
|
Product::factory()->create();
|
||||||
$category->delete();
|
$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']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user