diff --git a/README.md b/README.md index 5a51207..bc85106 100644 --- a/README.md +++ b/README.md @@ -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()`. + +--- + diff --git a/database/migrations/task2/2021_11_09_075914_create_users_table.php b/database/migrations/task2/2021_11_09_075914_create_users_table.php new file mode 100644 index 0000000..621a24e --- /dev/null +++ b/database/migrations/task2/2021_11_09_075914_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/task2/2021_11_09_075928_add_surname_to_users_table.php b/database/migrations/task2/2021_11_09_075928_add_surname_to_users_table.php new file mode 100644 index 0000000..5a3422a --- /dev/null +++ b/database/migrations/task2/2021_11_09_075928_add_surname_to_users_table.php @@ -0,0 +1,33 @@ + '/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); + + } }