Skip to content

Commit dc7851c

Browse files
Change ID type on form_submissions to be a float (#287)
* 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]>
1 parent d454e92 commit dc7851c

File tree

8 files changed

+58
-1
lines changed

8 files changed

+58
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"statamic/cms": "^5.0.1"
2929
},
3030
"require-dev": {
31-
"doctrine/dbal": "^3.3",
31+
"doctrine/dbal": "^3.8",
3232
"laravel/pint": "^1.0",
3333
"orchestra/testbench": "^8.0 || ^9.0.2",
3434
"phpunit/phpunit": "^9.4 || ^10.0 || ^11.0"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Statamic\Eloquent\Database\BaseMigration as Migration;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table($this->prefix('form_submissions'), function (Blueprint $table) {
12+
$table->float('id', 10, 4)->index()->unique()->change();
13+
});
14+
}
15+
16+
public function down()
17+
{
18+
Schema::table($this->prefix('form_submissions'), function (Blueprint $table) {
19+
$table->dropUnique('form_submissions_id_unique');
20+
$table->string('id')->unique()->change();
21+
});
22+
}
23+
};

src/Commands/ImportForms.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private function importForms(): void
7474

7575
app('statamic.eloquent.form_submissions.model')::firstOrNew(['created_at' => $timestamp])
7676
->fill([
77+
'id' => $submission->id(),
7778
'form' => $form->handle(),
7879
'data' => $submission->data(),
7980
'updated_at' => $timestamp,

src/Forms/Submission.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function toModel()
3737
return (! empty($model->id)) ? $model->fill([
3838
'data' => $this->data,
3939
]) : $model->fill([
40+
'id' => $this->id(),
4041
'data' => $this->data,
4142
'form' => $this->form->handle(),
4243
'created_at' => $timestamp,

src/Forms/SubmissionModel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class SubmissionModel extends BaseModel
88
{
99
protected $guarded = [];
1010

11+
public $incrementing = false;
12+
1113
protected $table = 'form_submissions';
1214

1315
protected $casts = [

src/ServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class ServiceProvider extends AddonServiceProvider
5858
\Statamic\Eloquent\Updates\AddIdToAttributesInRevisionsTable::class,
5959
\Statamic\Eloquent\Updates\RelateFormSubmissionsByHandle::class,
6060
\Statamic\Eloquent\Updates\DropStatusOnEntries::class,
61+
\Statamic\Eloquent\Updates\ChangeFormSubmissionsIdType::class,
6162
];
6263

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

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

151153
$this->publishes($assetContainerMigrations = [
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Statamic\Eloquent\Updates;
4+
5+
use Statamic\UpdateScripts\UpdateScript;
6+
7+
class ChangeFormSubmissionsIdType extends UpdateScript
8+
{
9+
public function shouldUpdate($newVersion, $oldVersion)
10+
{
11+
return $this->isUpdatingTo('4.1.0');
12+
}
13+
14+
public function update()
15+
{
16+
$source = __DIR__.'/../../database/migrations/2024_05_15_100000_modify_form_submissions_id.php';
17+
$dest = database_path('migrations/2024_05_15_100000_modify_form_submissions_id.php');
18+
19+
$this->files->copy($source, $dest);
20+
21+
$this->console()->info('Migration created');
22+
$this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.');
23+
}
24+
}

tests/Forms/FormSubmissionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function it_should_have_timestamps()
1818
]);
1919

2020
$submission = SubmissionModel::create([
21+
'id' => 1111111111.1111,
2122
'form' => $form->handle,
2223
'data' => [
2324
'name' => 'John Doe',
@@ -37,6 +38,7 @@ public function it_should_save_to_the_database()
3738
]);
3839

3940
$submission = SubmissionModel::create([
41+
'id' => 1111111111.1111,
4042
'form' => $form->handle,
4143
'data' => [
4244
'name' => 'John Doe',
@@ -61,13 +63,15 @@ public function it_should_not_overwrite_submissions()
6163
]);
6264

6365
$submission = SubmissionModel::create([
66+
'id' => 1111111111.1111,
6467
'form' => $form->handle,
6568
'data' => [
6669
'name' => 'John Doe',
6770
],
6871
]);
6972

7073
$submission = SubmissionModel::create([
74+
'id' => 1111111111.2222,
7175
'form' => $form->handle,
7276
'data' => [
7377
'name' => 'Billy Doe',

0 commit comments

Comments
 (0)