Task 2 - column after another column

This commit is contained in:
PovilasKorop
2021-11-09 10:08:56 +02:00
parent da6dd14446
commit 40044cbfa4
4 changed files with 97 additions and 0 deletions
+9
View File
@@ -35,3 +35,12 @@ Test method `test_successful_foreign_key_tasks_comments()`.
---
## Task 2. Add Column after Another Column.
Folder `database/migrations/task2` contains migrations for users table: one for creating the table, and another one for adding a NEW field.
That new field "surname" should be added in a particular order - after the "name" field.
Test method `test_column_added_to_the_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,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddSurnameToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
// TASK: Add a string field "surname" which would go after the field "name"
// Write code here
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
});
}
}
+19
View File
@@ -2,6 +2,7 @@
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;
@@ -20,4 +21,22 @@ class MigrationsTest extends TestCase
Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task1']);
}
public function test_column_added_to_the_table()
{
Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task2']);
User::factory()->create(['surname' => 'Testing']);
$this->assertDatabaseHas(User::class, ['surname' => 'Testing']);
$user = User::first();
$fieldNumber = 0;
foreach ($user->getAttributes() as $key => $value) {
$fieldNumber++;
if ($key == "surname") break;
}
$this->assertEquals(3, $fieldNumber);
}
}