Move remaining project metadata ownership to templates
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
use App\Actions\Projects\DuplicateProjectConfiguration;
|
||||
use App\Models\Project;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private array $tables = [
|
||||
'board_columns',
|
||||
'ticket_field_definitions',
|
||||
'ticket_link_types',
|
||||
'ticket_priorities',
|
||||
'ticket_statuses',
|
||||
'ticket_types',
|
||||
'repository_connections',
|
||||
'notification_actions',
|
||||
];
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
foreach ($this->tables as $table) {
|
||||
Schema::table($table, function (Blueprint $table): void {
|
||||
$table->foreignId('project_template_id')
|
||||
->nullable()
|
||||
->after('project_id')
|
||||
->constrained('project_templates')
|
||||
->nullOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
$projects = DB::table('projects')
|
||||
->leftJoin('project_templates', 'project_templates.id', '=', 'projects.project_template_id')
|
||||
->select([
|
||||
'projects.id',
|
||||
'projects.key',
|
||||
'projects.name',
|
||||
'projects.project_template_id',
|
||||
'project_templates.project_id as template_project_id',
|
||||
])
|
||||
->get();
|
||||
|
||||
foreach ($projects as $project) {
|
||||
$projectTemplateId = $project->project_template_id;
|
||||
|
||||
if ($projectTemplateId === null || (int) ($project->template_project_id ?? 0) !== (int) $project->id) {
|
||||
$projectTemplateId = DB::table('project_templates')->insertGetId([
|
||||
'project_id' => $project->id,
|
||||
'source_project_id' => $project->id,
|
||||
'name' => $project->name.' base template',
|
||||
'slug' => Str::lower($project->key).'-base-template',
|
||||
'is_published' => false,
|
||||
'copy_areas' => json_encode([
|
||||
'project_settings',
|
||||
'board_workflow',
|
||||
'ticket_types',
|
||||
'ticket_priorities',
|
||||
'ticket_fields',
|
||||
'ticket_link_types',
|
||||
'repository_connections',
|
||||
'notification_actions',
|
||||
], JSON_THROW_ON_ERROR),
|
||||
'configuration_snapshot' => json_encode([], JSON_THROW_ON_ERROR),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('projects')
|
||||
->where('id', $project->id)
|
||||
->update([
|
||||
'project_template_id' => $projectTemplateId,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($this->tables as $table) {
|
||||
DB::table($table)
|
||||
->where('project_id', $project->id)
|
||||
->update(['project_template_id' => $projectTemplateId]);
|
||||
}
|
||||
|
||||
$projectModel = Project::query()->find($project->id);
|
||||
|
||||
if ($projectModel !== null) {
|
||||
DB::table('project_templates')
|
||||
->where('id', $projectTemplateId)
|
||||
->update([
|
||||
'configuration_snapshot' => json_encode(
|
||||
app(DuplicateProjectConfiguration::class)->export(
|
||||
$projectModel,
|
||||
DuplicateProjectConfiguration::copyableAreas(),
|
||||
),
|
||||
JSON_THROW_ON_ERROR,
|
||||
),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
foreach (array_reverse($this->tables) as $table) {
|
||||
Schema::table($table, function (Blueprint $table): void {
|
||||
$table->dropConstrainedForeignId('project_template_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user