Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions database/seeds/PolicySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Database\Seeders;

use App\Policy;
use Carbon\CarbonImmutable;
use Illuminate\Database\Seeder;

class PolicySeeder extends Seeder {
public function run() {
Policy::create(
[
'policy_type' => 'terms-of-use',
'active_from' => CarbonImmutable::createFromDate(2026, 06, 01),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a CarbonImmutable instance here works, but makes it trickier to use firstOrCreate() or updateOrCreate() methods (see other comment as to why you would want to use those methods). This is because (emphasis mine):

Create a Carbon instance from just a date. The time portion is set to now.

And explained using tinker:

$ dc exec -it api php artisan tinker
> use Carbon\CarbonImmutable;

> $a = CarbonImmutable::createFromDate(2026, 06, 01);
= Carbon\CarbonImmutable @1780325575 {#8373
    date: 2026-06-01 14:52:55.291326 UTC (+00:00),
  }

> $b = CarbonImmutable::createFromDate(2026, 06, 01);
= Carbon\CarbonImmutable @1780325594 {#8399
    date: 2026-06-01 14:53:14.547747 UTC (+00:00),
  }

> $a === $b
= false

> $a == $b
= false

> $a->equalTo($b)
= false

To address this you could either:

  • use a date string (e.g. 'active_from' => '2026-06-01',)
  • use the CarbonImmutable::createMidnightDate() method
> $a = CarbonImmutable::createMidnightDate(2026, 06, 01);
= Carbon\CarbonImmutable @1780272000 {#8474
    date: 2026-06-01 00:00:00.0 UTC (+00:00),
  }

> $b = CarbonImmutable::createMidnightDate(2026, 06, 01);
= Carbon\CarbonImmutable @1780272000 {#8451
    date: 2026-06-01 00:00:00.0 UTC (+00:00),
  }

> $a === $b
= false

> $a == $b
= true

> $a->equalTo($b)
= true

'content_vue_file' => 'terms-of-use/example.vue',
]
);

@outdooracorn outdooracorn Jun 29, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will create a new policy, or fail if the policy already exists. If multiple seeders are run in the future, I think a failed Seeder will prevent subsequent seeders from running, which is likely not what we want.

From my quick searching, it's often recommended to create seeders in an idempotent way (i.e. running the seeder multiple times will result in the same database state).

https://oneuptime.com/blog/post/2026-02-03-laravel-database-seeding/view recommends using firstOrCreate() or updateOrCreate() methods on the model to make seeders idempotent.

Alternatively/additionally, you could use the Policy::truncate(); method to clear the table and reset the index (I haven't thought much about how best to handle existing constraints, though).

}
Comment on lines +10 to +18

@outdooracorn outdooracorn Jun 29, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For testing purposes, would it be useful to have more than one current policy? Maybe also a superseded policy? I'm also happy if we add these at a later stage, though.

}
Loading