mirror of
https://github.com/10h30/Test-Laravel-Migrations.git
synced 2026-06-05 15:07:54 +09:00
Rename column
This commit is contained in:
@@ -103,3 +103,11 @@ Test method `test_renamed_table()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 9. Rename column
|
||||
|
||||
Folder `database/migrations/task9` contains a migration for companies table. Later it was decided to rename the column from "companies.title" to "companies.name". Write the code for that, in the second migration file.
|
||||
|
||||
Test method `test_renamed_column()`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateCompaniesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('companies', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('title');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('companies');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class RenameNameInCompaniesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
// TASK: write the migration to rename the column "title" into "name"
|
||||
Schema::table('companies', function (Blueprint $table) {
|
||||
// Write code here
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('companies', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -104,4 +104,12 @@ class MigrationsTest extends TestCase
|
||||
DB::table('companies')->insert(['name' => 'First']);
|
||||
$this->assertDatabaseHas(Company::class, ['name' => 'First']);
|
||||
}
|
||||
|
||||
public function test_renamed_column()
|
||||
{
|
||||
Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task9']);
|
||||
|
||||
Company::create(['name' => 'First']);
|
||||
$this->assertDatabaseHas(Company::class, ['name' => 'First']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user