Laravel in production
Sign in

Topics

Backend

Tools & DevOps

All guides

Search

Topics

Practice

Architecture · Laravel in production

From Laravel to Laravel Rust. Day 6: a seeder that changes nothing

Author: Volodymyr Pichkurov Published: 8 min read
Laravel Rust Laravel Rust PostgreSQL

Sounds like bad news, but it's the best result of the day. We rewrote the skills catalog seeder in Rust and ran it in production. It went over 100 rows and changed not a single one. Not because it's broken, but because it works exactly like PHP.


In short

  • What we're moving: SkillSeeder — a catalog of 100 skills that developers pick in their profiles.
  • The big idea: we don't move the data, we pull it out of the code into a shared file, database/data/skills.json. Both seeders read it: PHP and Rust.
  • The tricky bit: by default, upsert in Rust touches the updated_at of every row, while updateOrCreate in PHP touches only the changed ones. We taught Rust to behave like PHP.
  • In production: "Deploy Rust" ran the seeder after the migrations. 100 rows, last change — still November 28.

Test subject number six

Here's what the seeder looked like in PHP:

php
private const CATALOG = [
    'languages' => ['PHP', 'JavaScript', 'TypeScript', 'Python', 'Go', 'Java', 'Kotlin', 'C#', 'Rust', 'Ruby', 'SQL', 'Bash', 'Dart', 'Swift', 'C++'],
    'backend'   => ['Laravel', 'Symfony', 'Yii', 'Slim', 'Node.js', 'NestJS', 'Express', 'Django', 'FastAPI', 'Spring', 'REST API', 'GraphQL', 'gRPC', 'WebSockets', 'Microservices', 'RabbitMQ', 'Kafka'],
    // … ещё шесть групп
];

public function run(): void
{
    foreach (self::CATALOG as $group => $names) {
        foreach ($names as $name) {
            Skill::updateOrCreate(
                ['slug' => self::slug($name)],
                ['name' => $name, 'group' => $group],
            );
        }
    }
}

public static function slug(string $name): string
{
    return Str::slug(str_replace(['#', '+', '.', '/'], ['sharp', 'plus', ' ', ' '], $name));
}

A good candidate for our first Rust seeder:

  • it's real reference data, not test users — the seeder runs in production on every deploy with changed content;
  • it's idempotent — updateOrCreate by slug, run it as often as you like;
  • it has a catch — a skill's address is built with Str::slug plus some manual replacements, so C# turns into csharp and Node.js into node-js.

Don't rewrite the data, move it out

The first temptation is to copy the array into Rust. Then the catalog would have two owners: add a skill in PHP, forget it in Rust — and the deploys start arguing over whose list is right.

So we moved the catalog into a data file. We computed the addresses once with the same PHP function and wrote them down next to the names:

json
[
    {
        "slug": "php",
        "name": "PHP",
        "group": "languages"
    },
    {
        "slug": "javascript",
        "name": "JavaScript",
        "group": "languages"
    },

Now there's no need to repeat Str::slug in Rust. But we don't take our own word for it: a PHP test walks through every entry and checks that the address in the file is exactly what PHP would have produced:

php
public function test_every_entry_has_the_slug_php_would_make_and_slugs_are_unique(): void
{
    $catalog = SkillSeeder::catalog();

    $this->assertNotEmpty($catalog);
    foreach ($catalog as $skill) {
        $this->assertSame(['slug', 'name', 'group'], array_keys($skill));
        $this->assertSame(SkillSeeder::slug($skill['name']), $skill['slug'], $skill['name']);
        $this->assertLessThanOrEqual(64, mb_strlen($skill['name']));
        $this->assertLessThanOrEqual(32, mb_strlen($skill['group']));
    }
    $slugs = array_column($catalog, 'slug');
    $this->assertSame(count($slugs), count(array_unique($slugs)));
}

The PHP seeder got shorter: it reads the same file. These days it's needed for local development and tests, while in production Rust fills the catalog.


The seeder in Rust

In Laravel Rust a seeder is just an async function with the #[seeder] attribute. The function's name becomes the seeder's name on the command line: devsense-rust db:seed skills.

rust
const CATALOG: &str = include_str!("../../../../database/data/skills.json");

#[derive(Debug, Deserialize, PartialEq)]
pub struct Skill {
    pub slug: String,
    pub name: String,
    pub group: String,
}

pub fn catalog() -> Result<Vec<Skill>, serde_json::Error> {
    serde_json::from_str(CATALOG)
}

#[seeder]
pub async fn skills() -> laravel::Result<()> {
    let catalog = catalog().map_err(|e| laravel::Error::msg(format!("skills.json: {e}")))?;
    let rows: Vec<_> = catalog
        .iter()
        .map(|skill| {
            values! {
                slug: skill.slug.as_str(),
                name: skill.name.as_str(),
                group: skill.group.as_str(),
                created_at: db::raw!("LOCALTIMESTAMP(0)"),
                updated_at: db::raw!("LOCALTIMESTAMP(0)"),
            }
        })
        .collect();
    db::table("skills")
        .upsert(
            rows,
            ["slug"],
            values! {
                name: db::excluded("name"),
                group: db::excluded("group"),
                updated_at: db::raw!(
                    "CASE WHEN skills.name IS DISTINCT FROM excluded.name \
                     OR skills.\"group\" IS DISTINCT FROM excluded.\"group\" \
                     THEN LOCALTIMESTAMP(0) ELSE skills.updated_at END"
                ),
            },
        )
        .await?;
    Ok(())
}

Three things we like here.

  • include_str!. The file is read at compile time and goes straight into the binary. There's no need to ship either the source or the JSON itself to the server: the new binary already knows the current catalog. That's why "Deploy Rust" now also runs when skills.json changes.
  • One query instead of a hundred. PHP ran updateOrCreate in a loop: a SELECT for every skill, then an INSERT or an UPDATE. Rust sends a single INSERT … ON CONFLICT (slug) DO UPDATE for all 100 rows.
  • A broken JSON won't slip through quietly. If the file is broken, the seeder returns an error and db:seed rolls back the transaction. A plain unit test checks this too.

The catch of the day: updated_at

The first version was simpler: the update had updated_at: LOCALTIMESTAMP(0). And it would have worked. But it would have differed from PHP in a way you don't spot right away.

updateOrCreate in PHP Laravel first finds the model, fills in the fields and calls save(). And save() on a model where nothing has changed does nothing — it doesn't even touch updated_at. So after a hundred runs of the PHP seeder, the "PHP" skill still carries the date it was created.

upsert is a single SQL query, and it knows nothing about any of that. The naive version would have stamped updated_at on all hundred skills on every deploy. Not much harm, but it's a lie: the history would say every skill had just been changed.

The fix is to tell PostgreSQL plainly: change the time only if the name or the group really is different. For that, upsert has excluded — the row we tried to insert. We compare it with the row in the table using IS DISTINCT FROM (unlike <>, it compares NULL correctly too) and pick the time with a CASE.

A small detail — the quotes around "group". It's an SQL keyword, and in a raw expression the query builder won't escape it for us.


Deploy: migrations first, then the seeder

sh
# Rust's seeders: reference data that can be applied again and again (upserts).
if ! ./devsense-rust.next db:seed skills --force; then
  echo "❌ the skills seeder failed (nothing was changed: db:seed runs in one transaction): the previous binary stays."
  rm -f devsense-rust.next
  exit 1
fi

The order is the same as for migrations in Day 5: the new binary first migrates the database, then fills the catalog, and only after that replaces the old one. If anything fails, the old binary keeps running and db:seed rolls back its transaction.

The seeder runs on every Rust deploy, even if the catalog hasn't changed. That's fine: it's one query, takes a few milliseconds and writes nothing when the list is unchanged. And we removed the SkillSeeder line from the PHP deploy: in production the catalog has one owner.


Verification

Locally, on the Sail database:

  1. The PHP seeder filled the table. We took a checksum of all rows: md5 over slug, name, group and updated_at.
  2. Ran the Rust seeder — same checksum. Not a single row changed, not even the time.
  3. Renamed "Rust" to "Rust (old)" and deleted the "MQTT" skill. Ran the Rust seeder: "Rust" came back with a fresh updated_at; "MQTT" reappeared; "PHP" kept its September date.
  4. Ran the Rust seeder once more, then the PHP seeder — the checksum didn't change once.

Plus a PHP test: a repeat run changes nothing, and a renamed skill gets fixed, with only its updated_at changing.

In production after the deploy:

text
100 | 2026-11-28 15:32:59

A hundred skills, and the most recent change is from November 28. The seeder went through and rewrote nothing, because the catalog in production already matched the file.


Honest differences and the price

  • The catalog is no longer code. To add a skill you edit JSON, not a PHP array. On the other hand, a test checks the address rule, so you can't make a mistake without noticing.
  • The production catalog now depends on the Rust deploy. If Rust is stopped, new skills won't show up until it's deployed. The old ones aren't going anywhere.
  • Skills removed from the list stay in the table — that's how it was in PHP too: people may have them in their profiles. Deleting them is a separate decision, not the seeder's job.
  • The seeder runs on every Rust deploy. A couple of milliseconds and not a single write when the catalog is unchanged — we consider that a fair price for production always matching the file.

The scoreboard

After six days, DevSense has in Rust:

  • three routes: the view counter, the Zones check and the people search in a team;
  • five models on top of PHP tables, plus reading the PHP session;
  • a queue and a worker with their own failed jobs table;
  • migrations in the shared migrations table;
  • a seeder for the skills catalog from a shared file.

Every Laravel mechanism in our plan now has at least one resident in Rust.


What comes next

The mechanism-by-mechanism migration plan is done; two big topics lie ahead.

Upstream. Everything we wrote to live side by side with PHP Laravel — reading the session, the base64 payload, the login_web_… key — will come in handy for more than just us. We'll send it as small PRs to laravel/rust-framework, each with documentation.

An honest breakdown. Speed on our data, memory (the Rust HTTP server — 16 MB, the worker — 17 MB versus 62 MB for a php-fpm process), what the move cost and which familiar packages Rust doesn't have yet.


Links

Was this guide helpful?

Read next

// Newsletter

New Architecture guides by e-mail

One e-mail a week with new guides on Architecture. No spam, unsubscribe in one click.

About the author

Volodymyr Pichkurov View portfolio

Spotted a mistake? Suggest an edit

Help us improve this article. Suggest edits, additions, or corrections.

No suggestions submitted yet. Be the first to share your feedback!

On this page