From 72bc5ec8aed894ab92e76ab99f412cbd09e610fd Mon Sep 17 00:00:00 2001 From: aslan Date: Thu, 23 May 2024 14:48:29 +0300 Subject: [PATCH] add migrations with factory and seeders --- app/Models/Department.php | 6 + app/Models/Label.php | 7 + database/factories/LabelFactory.php | 1 - database/factories/ProjectFactory.php | 16 ++ database/factories/TaskFactory.php | 1 + database/factories/TaskStatusesFactory.php | 1 + database/factories/UserFactory.php | 1 + ...01_00_000000_create_departments_table.php} | 0 .../0001_01_01_000000_create_users_table.php | 1 + ...024_04_27_115329_create_projects_table.php | 30 ++++ ...4_27_115330_create_task_statuses_table.php | 1 + .../2024_04_27_115604_create_labels_table.php | 1 - .../2024_04_27_121447_create_tasks_table.php | 2 +- ...4_27_121448_create_project_user_table.php} | 6 +- database/seeders/DatabaseSeeder.php | 9 +- database/seeders/DepartmentSeeder.php | 4 + database/seeders/LabelSeeder.php | 4 - database/seeders/ProjectSeeder.php | 35 ++++ database/seeders/ProjectUserSeeder.php | 47 ++++++ database/seeders/TaskSeeder.php | 32 ++-- database/seeders/TaskStatusSeeder.php | 4 + database/seeders/TaskUserSeeder.php | 155 ------------------ 22 files changed, 181 insertions(+), 183 deletions(-) create mode 100644 database/factories/ProjectFactory.php rename database/migrations/{2024_04_27_121400_create_departments_table.php => 0001_01_00_000000_create_departments_table.php} (100%) create mode 100644 database/migrations/2024_04_27_115329_create_projects_table.php rename database/migrations/{2024_04_27_121448_create_user_task_table.php => 2024_04_27_121448_create_project_user_table.php} (66%) create mode 100644 database/seeders/ProjectSeeder.php create mode 100644 database/seeders/ProjectUserSeeder.php delete mode 100644 database/seeders/TaskUserSeeder.php diff --git a/app/Models/Department.php b/app/Models/Department.php index c38e561..9160984 100644 --- a/app/Models/Department.php +++ b/app/Models/Department.php @@ -8,4 +8,10 @@ use Illuminate\Database\Eloquent\Model; class Department extends Model { use HasFactory; + + protected $fillable = [ + 'id', + 'name', + 'created_by_id', + ]; } diff --git a/app/Models/Label.php b/app/Models/Label.php index 6771431..14bd33c 100644 --- a/app/Models/Label.php +++ b/app/Models/Label.php @@ -8,4 +8,11 @@ use Illuminate\Database\Eloquent\Model; class Label extends Model { use HasFactory; + + protected $fillable = [ + 'id', + 'name', + 'department_id', + 'created_by_id', + ]; } diff --git a/database/factories/LabelFactory.php b/database/factories/LabelFactory.php index e4ddc35..533b30d 100644 --- a/database/factories/LabelFactory.php +++ b/database/factories/LabelFactory.php @@ -10,7 +10,6 @@ class LabelFactory extends Factory { return [ 'name' => fake()->name(), - 'description' => fake()->text(), ]; } } diff --git a/database/factories/ProjectFactory.php b/database/factories/ProjectFactory.php new file mode 100644 index 0000000..16c5e68 --- /dev/null +++ b/database/factories/ProjectFactory.php @@ -0,0 +1,16 @@ + fake()->name(), + 'description' => fake()->text(), + ]; + } +} diff --git a/database/factories/TaskFactory.php b/database/factories/TaskFactory.php index 1ff1943..de7d817 100644 --- a/database/factories/TaskFactory.php +++ b/database/factories/TaskFactory.php @@ -12,6 +12,7 @@ class TaskFactory extends Factory 'name' => fake()->name(), 'file' => fake()->name(), 'description' => fake()->text(), + 'project_id' => 1, 'parent_id' => 0, 'status_id' => 1, 'department_id' => 1, diff --git a/database/factories/TaskStatusesFactory.php b/database/factories/TaskStatusesFactory.php index 7dfcb8b..28ecbf1 100644 --- a/database/factories/TaskStatusesFactory.php +++ b/database/factories/TaskStatusesFactory.php @@ -10,6 +10,7 @@ class TaskStatusesFactory extends Factory { return [ 'name' => fake()->name(), + 'project_id' => 1, ]; } } diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 45a37d4..15860f5 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -16,6 +16,7 @@ class UserFactory extends Factory 'name' => fake()->name(), 'email' => fake()->unique()->safeEmail(), 'email_verified_at' => now(), + 'department_id' => 1, 'password' => static::$password ??= Hash::make('password'), 'remember_token' => Str::random(10), ]; diff --git a/database/migrations/2024_04_27_121400_create_departments_table.php b/database/migrations/0001_01_00_000000_create_departments_table.php similarity index 100% rename from database/migrations/2024_04_27_121400_create_departments_table.php rename to database/migrations/0001_01_00_000000_create_departments_table.php diff --git a/database/migrations/0001_01_01_000000_create_users_table.php b/database/migrations/0001_01_01_000000_create_users_table.php index c987f53..7931a6d 100644 --- a/database/migrations/0001_01_01_000000_create_users_table.php +++ b/database/migrations/0001_01_01_000000_create_users_table.php @@ -12,6 +12,7 @@ return new class extends Migration $table->id(); $table->string('name'); $table->string('email')->unique(); + $table->foreignId('department_id')->constrained('departments'); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); diff --git a/database/migrations/2024_04_27_115329_create_projects_table.php b/database/migrations/2024_04_27_115329_create_projects_table.php new file mode 100644 index 0000000..c122293 --- /dev/null +++ b/database/migrations/2024_04_27_115329_create_projects_table.php @@ -0,0 +1,30 @@ +id(); + $table->string('name', 255); + $table->text('description'); + $table->foreignId('department_id')->constrained('departments'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('projects'); + } +}; diff --git a/database/migrations/2024_04_27_115330_create_task_statuses_table.php b/database/migrations/2024_04_27_115330_create_task_statuses_table.php index c1277a7..11ae766 100644 --- a/database/migrations/2024_04_27_115330_create_task_statuses_table.php +++ b/database/migrations/2024_04_27_115330_create_task_statuses_table.php @@ -11,6 +11,7 @@ return new class extends Migration Schema::create('task_statuses', function (Blueprint $table) { $table->id(); $table->string('name', 255); + $table->foreignId('project_id')->constrained('projects'); $table->timestamps(); }); } diff --git a/database/migrations/2024_04_27_115604_create_labels_table.php b/database/migrations/2024_04_27_115604_create_labels_table.php index 0f1eb32..efa0908 100644 --- a/database/migrations/2024_04_27_115604_create_labels_table.php +++ b/database/migrations/2024_04_27_115604_create_labels_table.php @@ -11,7 +11,6 @@ return new class extends Migration Schema::create('labels', function (Blueprint $table) { $table->id(); $table->string('name', 255); - $table->string('description')->nullable(); $table->timestamps(); }); } diff --git a/database/migrations/2024_04_27_121447_create_tasks_table.php b/database/migrations/2024_04_27_121447_create_tasks_table.php index 52dd30a..a4c9ea9 100644 --- a/database/migrations/2024_04_27_121447_create_tasks_table.php +++ b/database/migrations/2024_04_27_121447_create_tasks_table.php @@ -13,8 +13,8 @@ return new class extends Migration $table->string('name', 255); $table->bigInteger('parent_id'); $table->text('description')->nullable(); + $table->foreignId('project_id')->constrained('projects'); $table->foreignId('status_id')->constrained('task_statuses'); - $table->foreignId('department_id')->constrained('departments'); $table->foreignId('created_by_id')->constrained('users'); $table->timestamps(); }); diff --git a/database/migrations/2024_04_27_121448_create_user_task_table.php b/database/migrations/2024_04_27_121448_create_project_user_table.php similarity index 66% rename from database/migrations/2024_04_27_121448_create_user_task_table.php rename to database/migrations/2024_04_27_121448_create_project_user_table.php index 4eb8409..b883773 100644 --- a/database/migrations/2024_04_27_121448_create_user_task_table.php +++ b/database/migrations/2024_04_27_121448_create_project_user_table.php @@ -8,8 +8,8 @@ return new class extends Migration { public function up(): void { - Schema::create('task_user', function (Blueprint $table) { - $table->foreignId('task_id')->constrained('tasks'); + Schema::create('project_user', function (Blueprint $table) { + $table->foreignId('project_id')->constrained('projects'); $table->foreignId('user_id')->constrained('users'); $table->timestamps(); }); @@ -17,6 +17,6 @@ return new class extends Migration public function down(): void { - Schema::dropIfExists('task_user'); + Schema::dropIfExists('project_user'); } }; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index cfd6e08..451d3e8 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -11,22 +11,27 @@ class DatabaseSeeder extends Seeder { public function run(): void { + $this->call([ + DepartmentSeeder::class, + ]); + User::factory(20)->create(); User::factory()->create([ 'name' => 'Test User', 'email' => 'test@example.com', + 'department_id' => 1, 'password' => Hash::make('password'), ]); $this->call([ - DepartmentSeeder::class, + ProjectSeeder::class, LabelSeeder::class, TaskStatusSeeder::class, TaskSeeder::class, NoteSeeder::class, LabelTaskSeeder::class, - TaskUserSeeder::class, + ProjectUserSeeder::class, ]); } } diff --git a/database/seeders/DepartmentSeeder.php b/database/seeders/DepartmentSeeder.php index 2e806f4..0dc3a4e 100644 --- a/database/seeders/DepartmentSeeder.php +++ b/database/seeders/DepartmentSeeder.php @@ -12,6 +12,10 @@ class DepartmentSeeder extends Seeder public function run(): void { DB::table('departments')->insert([ + [ + 'name' => 'без отдела', + 'created_at' => Carbon::now(), + ], [ 'name' => 'отдел Разработки', 'created_at' => Carbon::now(), diff --git a/database/seeders/LabelSeeder.php b/database/seeders/LabelSeeder.php index 60b70e5..3b061c3 100644 --- a/database/seeders/LabelSeeder.php +++ b/database/seeders/LabelSeeder.php @@ -14,22 +14,18 @@ class LabelSeeder extends Seeder DB::table('labels')->insert([ [ 'name' => 'ошибка', - 'description' => 'Какая-то ошибка в коде или проблема с функциональностью', 'created_at' => Carbon::now(), ], [ 'name' => 'документация', - 'description' => 'Задача которая касается документации', 'created_at' => Carbon::now(), ], [ 'name' => 'дубликат', - 'description' => 'Повтор другой задачи', 'created_at' => Carbon::now(), ], [ 'name' => 'доработка', - 'description' => 'Новая фича, которую нужно запилить', 'created_at' => Carbon::now(), ], ]); diff --git a/database/seeders/ProjectSeeder.php b/database/seeders/ProjectSeeder.php new file mode 100644 index 0000000..e0905ab --- /dev/null +++ b/database/seeders/ProjectSeeder.php @@ -0,0 +1,35 @@ +insert([ + [ + 'name' => 'Сайт для Абитуриентов', + 'description' => 'разработка Сайт для Абитуриентов', + 'department_id' => 1, + 'created_at' => Carbon::now(), + ], + [ + 'name' => 'Личный кабинет студента', + 'description' => 'разработка Личный кабинет студента', + 'department_id' => 1, + 'created_at' => Carbon::now(), + ], + [ + 'name' => 'Личный кабинет преподователя', + 'description' => 'разработка Личный кабинет преподователя', + 'department_id' => 1, + 'created_at' => Carbon::now(), + ], + ]); + } +} diff --git a/database/seeders/ProjectUserSeeder.php b/database/seeders/ProjectUserSeeder.php new file mode 100644 index 0000000..7ac9e5a --- /dev/null +++ b/database/seeders/ProjectUserSeeder.php @@ -0,0 +1,47 @@ +insert([ + [ + 'project_id' => 1, + 'user_id' => 2, + ], + [ + 'project_id' => 1, + 'user_id' => 4, + ], + [ + 'project_id' => 2, + 'user_id' => 1, + ], + [ + 'project_id' => 2, + 'user_id' => 3, + ], + [ + 'project_id' => 2, + 'user_id' => 4, + ], + [ + 'project_id' => 3, + 'user_id' => 1, + ], + [ + 'project_id' => 3, + 'user_id' => 2, + ], + [ + 'project_id' => 3, + 'user_id' => 3, + ], + ]); + } +} diff --git a/database/seeders/TaskSeeder.php b/database/seeders/TaskSeeder.php index 871092d..aae1222 100644 --- a/database/seeders/TaskSeeder.php +++ b/database/seeders/TaskSeeder.php @@ -18,8 +18,8 @@ class TaskSeeder extends Seeder 'description' => 'Я тут ошибку нашёл, надо бы её исправить и так далее и так далее', 'created_by_id' => 1, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 0, - 'department_id' => 1, ], [ 'status_id' => 2, @@ -27,8 +27,8 @@ class TaskSeeder extends Seeder 'description' => 'Вёрстка поехала в далёкие края. Нужно удалить бутстрап!', 'created_by_id' => 2, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 0, - 'department_id' => 1, ], [ 'status_id' => 3, @@ -36,8 +36,8 @@ class TaskSeeder extends Seeder 'description' => 'Выпилить всё легаси, которое найдёшь', 'created_by_id' => 2, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 0, - 'department_id' => 1, ], [ 'status_id' => 4, @@ -45,8 +45,8 @@ class TaskSeeder extends Seeder 'description' => 'За одно добавить тестовых данных', 'created_by_id' => 1, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 0, - 'department_id' => 1, ], [ 'status_id' => 1, @@ -54,8 +54,8 @@ class TaskSeeder extends Seeder 'description' => 'Кажется она не того цвета', 'created_by_id' => 3, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 2, @@ -63,8 +63,8 @@ class TaskSeeder extends Seeder 'description' => 'Не ищет то, что мне хочется', 'created_by_id' => 5, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 3, @@ -72,8 +72,8 @@ class TaskSeeder extends Seeder 'description' => 'Они такие мягкие и пушистые', 'created_by_id' => 7, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 4, @@ -81,8 +81,8 @@ class TaskSeeder extends Seeder 'description' => '', 'created_by_id' => 9, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 1, @@ -90,8 +90,8 @@ class TaskSeeder extends Seeder 'description' => 'Кому-то же они нужны?', 'created_by_id' => 11, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 2, @@ -99,8 +99,8 @@ class TaskSeeder extends Seeder 'description' => 'Этот сериал никому не нравится! :)', 'created_by_id' => 13, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 4, @@ -108,8 +108,8 @@ class TaskSeeder extends Seeder 'description' => 'Передать Олегу, чтобы больше не ронял прод', 'created_by_id' => 2, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 1, @@ -117,8 +117,8 @@ class TaskSeeder extends Seeder 'description' => 'Андрей, это задача для тебя', 'created_by_id' => 4, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 2, @@ -126,8 +126,8 @@ class TaskSeeder extends Seeder 'description' => 'Не забыть потестировать', 'created_by_id' => 6, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 3, @@ -135,8 +135,8 @@ class TaskSeeder extends Seeder 'description' => 'Кризис это не время, чтобы молчать!', 'created_by_id' => 8, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 4, @@ -144,8 +144,8 @@ class TaskSeeder extends Seeder 'description' => 'Только не по моему', 'created_by_id' => 10, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], [ 'status_id' => 1, @@ -153,8 +153,8 @@ class TaskSeeder extends Seeder 'description' => '', 'created_by_id' => 13, 'created_at' => Carbon::now(), + 'project_id' => 1, 'parent_id' => 1, - 'department_id' => 1, ], ]); } diff --git a/database/seeders/TaskStatusSeeder.php b/database/seeders/TaskStatusSeeder.php index fe65638..11d2f8a 100644 --- a/database/seeders/TaskStatusSeeder.php +++ b/database/seeders/TaskStatusSeeder.php @@ -14,18 +14,22 @@ class TaskStatusSeeder extends Seeder DB::table('task_statuses')->insert([ [ 'name' => 'Создан', + 'project_id' => 1, 'created_at' => Carbon::now(), ], [ 'name' => 'В работе', + 'project_id' => 1, 'created_at' => Carbon::now(), ], [ 'name' => 'На проверке', + 'project_id' => 1, 'created_at' => Carbon::now(), ], [ 'name' => 'Выполнен', + 'project_id' => 1, 'created_at' => Carbon::now(), ], ]); diff --git a/database/seeders/TaskUserSeeder.php b/database/seeders/TaskUserSeeder.php deleted file mode 100644 index ba6f777..0000000 --- a/database/seeders/TaskUserSeeder.php +++ /dev/null @@ -1,155 +0,0 @@ -insert([ - [ - 'task_id' => 1, - 'user_id' => 2, - ], - [ - 'task_id' => 1, - 'user_id' => 4, - ], - [ - 'task_id' => 2, - 'user_id' => 1, - ], - [ - 'task_id' => 2, - 'user_id' => 3, - ], - [ - 'task_id' => 2, - 'user_id' => 4, - ], - [ - 'task_id' => 3, - 'user_id' => 1, - ], - [ - 'task_id' => 3, - 'user_id' => 2, - ], - [ - 'task_id' => 3, - 'user_id' => 3, - ], - [ - 'task_id' => 4, - 'user_id' => 2, - ], - [ - 'task_id' => 4, - 'user_id' => 4, - ], - [ - 'task_id' => 5, - 'user_id' => 3, - ], - [ - 'task_id' => 6, - 'user_id' => 1, - ], - [ - 'task_id' => 6, - 'user_id' => 4, - ], - [ - 'task_id' => 7, - 'user_id' => 1, - ], - [ - 'task_id' => 8, - 'user_id' => 1, - ], - [ - 'task_id' => 8, - 'user_id' => 2, - ], - [ - 'task_id' => 8, - 'user_id' => 3, - ], - [ - 'task_id' => 8, - 'user_id' => 4, - ], - [ - 'task_id' => 9, - 'user_id' => 1, - ], - [ - 'task_id' => 9, - 'user_id' => 2, - ], - [ - 'task_id' => 9, - 'user_id' => 4, - ], - [ - 'task_id' => 10, - 'user_id' => 2, - ], - [ - 'task_id' => 10, - 'user_id' => 3, - ], - [ - 'task_id' => 10, - 'user_id' => 4, - ], - [ - 'task_id' => 11, - 'user_id' => 2, - ], - [ - 'task_id' => 11, - 'user_id' => 4, - ], - [ - 'task_id' => 12, - 'user_id' => 1, - ], - [ - 'task_id' => 13, - 'user_id' => 4, - ], - [ - 'task_id' => 14, - 'user_id' => 2, - ], - [ - 'task_id' => 15, - 'user_id' => 1, - ], - [ - 'task_id' => 15, - 'user_id' => 2, - ], - [ - 'task_id' => 15, - 'user_id' => 3, - ], - [ - 'task_id' => 16, - 'user_id' => 1, - ], - [ - 'task_id' => 16, - 'user_id' => 2, - ], - [ - 'task_id' => 16, - 'user_id' => 3, - ], - ]); - } -}