Howdy

Seeds and factories

howdy db seed

A project with a database gets two kinds of test data: a factory for each record, which builds valid records for tests and seeds, and a seed script, which fills the development database with the records to start with. howdy db seed runs the seed script, and howdy db migrate applies the migrations on their own.

Factories and seeds live in dev/, beside the gleam dev entry point. Tests can import code in dev/ and gleam run -m can run it, but gleam run and gleam export erlang-shipment leave it out, so test data never ships. It also means src/ cannot import it.

Factories

Every record howdy create makes with controller, pages or repo gets a factory in dev/<project>/factories/<singular>_factory.gleam, and a project made by howdy new with a database has one for its todos. A factory has three functions:

  • input() is input that passes the record's validation. Each string field is its label and a number no earlier call in the run has had, such as "Name 7" or "Email 8", so values in unique columns stay apart. Other fields take a fixed sample value.
  • insert(db, input) validates the input and inserts it through the feature's create, as a request would, and returns the record. It crashes if either fails, which fails the test.
  • insert_many(db, count, with) inserts count records, each made from a fresh input() by the function with.

Change a field with Gleam's record update syntax:

import my_app/factories/user_factory
import my_app/features/users/user.{UserInput}

pub fn lists_users_test() {
  use db <- support.with_db()
  let ada =
    user_factory.insert(db, UserInput(..user_factory.input(), name: "Ada"))
  let others = user_factory.insert_many(db, 3, fn(input) { input })
  // ...
}

Use them in tests rather than writing insert helpers of your own: a record that only needs to exist is one line, and since everything goes through the record's validate, test data cannot drift out of step with the app's rules. The factory is yours to edit. When you add a validation rule that the sample values break, change input() to give a valid value.

Seeding

howdy db seed
howdy db seed --reset

howdy db seed migrates the database .env names, DATABASE_URL on PostgreSQL or DATABASE_PATH on SQLite, as the app does, then runs dev/seed.gleam with gleam run -m seed. Its seed function inserts the records to start with, by calling the factories; a new project's seeds a few todos:

pub fn seed(db: Repo) -> Nil {
  todo_factory.insert(db, TodoInput(title: "Try the todo list"))
  todo_factory.insert_many(db, 3, fn(input) { input })
  Nil
}

Add calls for your own records as you create them.

Seeding again inserts the records again. --reset starts over: it runs dev/seed/reset.gleam, which drops every table, then migrates and seeds the database as new. On PostgreSQL it drops and recreates the public schema. On SQLite it drops every table and view but keeps the file, so a running gleam dev carries on with the fresh database. Either way it deletes everything in that database, so never point .env at data you want to keep.

A project made before howdy new wrote the seed files gets dev/seed.gleam and dev/seed/reset.gleam the first time it runs howdy db seed, with a seed function that seeds nothing yet. Files the project already has are left as they are.

Migrating

howdy db migrate

howdy db migrate applies every migration in platform/database.gleam that has not run yet, without starting the server or seeding, by running gleam run -m tasks/migrate. The app also migrates when it starts. See Migrations.

Both commands run inside a project with a database, from any folder in it, and stream Gleam's output as it goes.

Flags

  • howdy db seed --reset: drop every table first, deleting all the data, then migrate and seed.