Allow multiple template versions: replace unique(slug) with unique(slug,version) + versioning test
This commit is contained in:
parent
84d15ac715
commit
4ca2d07e7f
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('document_templates', function (Blueprint $table) {
|
||||||
|
// Drop original unique index on slug if it exists, then add composite unique (slug, version)
|
||||||
|
try {
|
||||||
|
$table->dropUnique('document_templates_slug_unique');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
// Ignore if already dropped / not present (SQLite memory or prior manual change)
|
||||||
|
}
|
||||||
|
// Add composite unique to allow multiple versions per slug while preventing duplicate version numbers
|
||||||
|
$table->unique(['slug', 'version'], 'document_templates_slug_version_unique');
|
||||||
|
// Optional plain index on slug alone (not unique) to speed lookups by slug
|
||||||
|
$table->index('slug', 'document_templates_slug_index');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('document_templates', function (Blueprint $table) {
|
||||||
|
// Drop composite + plain indices
|
||||||
|
try {
|
||||||
|
$table->dropIndex('document_templates_slug_index');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$table->dropUnique('document_templates_slug_version_unique');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
}
|
||||||
|
// Restore original simple unique on slug
|
||||||
|
try {
|
||||||
|
$table->unique('slug', 'document_templates_slug_unique');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
59
tests/Feature/DocumentTemplateVersioningTest.php
Normal file
59
tests/Feature/DocumentTemplateVersioningTest.php
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\UploadedFile;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class DocumentTemplateVersioningTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_second_upload_creates_new_version_row(): void
|
||||||
|
{
|
||||||
|
Storage::fake('public');
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$role = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
|
||||||
|
$user->roles()->sync([$role->id]);
|
||||||
|
$this->actingAs($user);
|
||||||
|
|
||||||
|
$docxV1 = $this->fakeDocxWithBody('{{contract.reference}}');
|
||||||
|
$this->post(route('admin.document-templates.store'), [
|
||||||
|
'name' => 'Povzetek pogodbe',
|
||||||
|
'slug' => 'contract-summary',
|
||||||
|
'file' => $docxV1,
|
||||||
|
])->assertRedirect();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('document_templates', [
|
||||||
|
'slug' => 'contract-summary',
|
||||||
|
'version' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$docxV2 = $this->fakeDocxWithBody('{{contract.reference}} {{generation.date}}');
|
||||||
|
$this->post(route('admin.document-templates.store'), [
|
||||||
|
'name' => 'Povzetek pogodbe',
|
||||||
|
'slug' => 'contract-summary',
|
||||||
|
'file' => $docxV2,
|
||||||
|
])->assertRedirect();
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('document_templates', [
|
||||||
|
'slug' => 'contract-summary',
|
||||||
|
'version' => 2,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertEquals(2, \App\Models\DocumentTemplate::where('slug', 'contract-summary')->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function fakeDocxWithBody(string $body): UploadedFile
|
||||||
|
{
|
||||||
|
$tmp = tempnam(sys_get_temp_dir(), 'docx');
|
||||||
|
$zip = new \ZipArchive;
|
||||||
|
$zip->open($tmp, \ZipArchive::OVERWRITE);
|
||||||
|
$zip->addFromString('[Content_Types].xml', '<Types></Types>');
|
||||||
|
$zip->addFromString('word/document.xml', '<w:document><w:body>'.$body.'</w:body></w:document>');
|
||||||
|
$zip->close();
|
||||||
|
|
||||||
|
return UploadedFile::fake()->createWithContent('template.docx', file_get_contents($tmp));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user