diff --git a/composer.json b/composer.json index a460b924..441b9697 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/database/migrations/2024_05_15_100000_modify_form_submissions_id.php b/database/migrations/2024_05_15_100000_modify_form_submissions_id.php new file mode 100644 index 00000000..655b69c5 --- /dev/null +++ b/database/migrations/2024_05_15_100000_modify_form_submissions_id.php @@ -0,0 +1,23 @@ +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(); + }); + } +}; diff --git a/src/Commands/ImportForms.php b/src/Commands/ImportForms.php index daa92066..1acbf7b9 100644 --- a/src/Commands/ImportForms.php +++ b/src/Commands/ImportForms.php @@ -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, diff --git a/src/Forms/Submission.php b/src/Forms/Submission.php index 5da519d7..eeacc945 100644 --- a/src/Forms/Submission.php +++ b/src/Forms/Submission.php @@ -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, diff --git a/src/Forms/SubmissionModel.php b/src/Forms/SubmissionModel.php index 23d2ac28..89151e12 100644 --- a/src/Forms/SubmissionModel.php +++ b/src/Forms/SubmissionModel.php @@ -8,6 +8,8 @@ class SubmissionModel extends BaseModel { protected $guarded = []; + public $incrementing = false; + protected $table = 'form_submissions'; protected $casts = [ diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 5a4bd980..359021cd 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -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 = [ @@ -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 = [ diff --git a/src/Updates/ChangeFormSubmissionsIdType.php b/src/Updates/ChangeFormSubmissionsIdType.php new file mode 100644 index 00000000..0988544d --- /dev/null +++ b/src/Updates/ChangeFormSubmissionsIdType.php @@ -0,0 +1,24 @@ +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.'); + } +} diff --git a/tests/Forms/FormSubmissionTest.php b/tests/Forms/FormSubmissionTest.php index b62e433f..cadf1e29 100644 --- a/tests/Forms/FormSubmissionTest.php +++ b/tests/Forms/FormSubmissionTest.php @@ -18,6 +18,7 @@ public function it_should_have_timestamps() ]); $submission = SubmissionModel::create([ + 'id' => 1111111111.1111, 'form' => $form->handle, 'data' => [ 'name' => 'John Doe', @@ -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', @@ -61,6 +63,7 @@ public function it_should_not_overwrite_submissions() ]); $submission = SubmissionModel::create([ + 'id' => 1111111111.1111, 'form' => $form->handle, 'data' => [ 'name' => 'John Doe', @@ -68,6 +71,7 @@ public function it_should_not_overwrite_submissions() ]); $submission = SubmissionModel::create([ + 'id' => 1111111111.2222, 'form' => $form->handle, 'data' => [ 'name' => 'Billy Doe',