Skip to content

Commit

Permalink
Change ID type on form_submissions to be a float (#287)
Browse files Browse the repository at this point in the history
* Change ID type on form_submissions to be a float

* Support L10

* Fix up tests

* Need dbal 3.5+ for changing tables

* Does this fix tests?

* Hmm

* Update the `down` migration

---------

Co-authored-by: Duncan McClean <[email protected]>
  • Loading branch information
ryanmitchell and duncanmcclean authored May 30, 2024
1 parent d454e92 commit dc7851c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"statamic/cms": "^5.0.1"
},
"require-dev": {
"doctrine/dbal": "^3.3",
"doctrine/dbal": "^3.8",
"laravel/pint": "^1.0",
"orchestra/testbench": "^8.0 || ^9.0.2",
"phpunit/phpunit": "^9.4 || ^10.0 || ^11.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Statamic\Eloquent\Database\BaseMigration as Migration;

return new class extends Migration
{
public function up()
{
Schema::table($this->prefix('form_submissions'), function (Blueprint $table) {
$table->float('id', 10, 4)->index()->unique()->change();
});
}

public function down()
{
Schema::table($this->prefix('form_submissions'), function (Blueprint $table) {
$table->dropUnique('form_submissions_id_unique');
$table->string('id')->unique()->change();
});
}
};
1 change: 1 addition & 0 deletions src/Commands/ImportForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private function importForms(): void

app('statamic.eloquent.form_submissions.model')::firstOrNew(['created_at' => $timestamp])
->fill([
'id' => $submission->id(),
'form' => $form->handle(),
'data' => $submission->data(),
'updated_at' => $timestamp,
Expand Down
1 change: 1 addition & 0 deletions src/Forms/Submission.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function toModel()
return (! empty($model->id)) ? $model->fill([
'data' => $this->data,
]) : $model->fill([
'id' => $this->id(),
'data' => $this->data,
'form' => $this->form->handle(),
'created_at' => $timestamp,
Expand Down
2 changes: 2 additions & 0 deletions src/Forms/SubmissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class SubmissionModel extends BaseModel
{
protected $guarded = [];

public $incrementing = false;

protected $table = 'form_submissions';

protected $casts = [
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class ServiceProvider extends AddonServiceProvider
\Statamic\Eloquent\Updates\AddIdToAttributesInRevisionsTable::class,
\Statamic\Eloquent\Updates\RelateFormSubmissionsByHandle::class,
\Statamic\Eloquent\Updates\DropStatusOnEntries::class,
\Statamic\Eloquent\Updates\ChangeFormSubmissionsIdType::class,
];

protected $listen = [
Expand Down Expand Up @@ -146,6 +147,7 @@ private function publishMigrations(): void

$this->publishes($formSubmissionMigrations = [
__DIR__.'/../database/migrations/2024_03_07_100000_create_form_submissions_table.php' => database_path('migrations/2024_03_07_100000_create_form_submissions_table.php'),
__DIR__.'/../database/migrations/2024_05_15_100000_modify_form_submissions_id.php' => database_path('migrations/2024_05_15_100000_modify_form_submissions_id.php'),
], 'statamic-eloquent-form-submission-migrations');

$this->publishes($assetContainerMigrations = [
Expand Down
24 changes: 24 additions & 0 deletions src/Updates/ChangeFormSubmissionsIdType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Statamic\Eloquent\Updates;

use Statamic\UpdateScripts\UpdateScript;

class ChangeFormSubmissionsIdType extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('4.1.0');
}

public function update()
{
$source = __DIR__.'/../../database/migrations/2024_05_15_100000_modify_form_submissions_id.php';
$dest = database_path('migrations/2024_05_15_100000_modify_form_submissions_id.php');

$this->files->copy($source, $dest);

$this->console()->info('Migration created');
$this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.');
}
}
4 changes: 4 additions & 0 deletions tests/Forms/FormSubmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function it_should_have_timestamps()
]);

$submission = SubmissionModel::create([
'id' => 1111111111.1111,
'form' => $form->handle,
'data' => [
'name' => 'John Doe',
Expand All @@ -37,6 +38,7 @@ public function it_should_save_to_the_database()
]);

$submission = SubmissionModel::create([
'id' => 1111111111.1111,
'form' => $form->handle,
'data' => [
'name' => 'John Doe',
Expand All @@ -61,13 +63,15 @@ public function it_should_not_overwrite_submissions()
]);

$submission = SubmissionModel::create([
'id' => 1111111111.1111,
'form' => $form->handle,
'data' => [
'name' => 'John Doe',
],
]);

$submission = SubmissionModel::create([
'id' => 1111111111.2222,
'form' => $form->handle,
'data' => [
'name' => 'Billy Doe',
Expand Down

0 comments on commit dc7851c

Please sign in to comment.