Seed a project with test data
Fill a test project, a branch or a local database with the same data every time, from a file in your repository, without duplicates and without half-loaded data.
A seed is the data a fresh project needs to be usable: reference rows, demo content, test users. Keep it in aurabase/seed.sql next to your migrations and load it after them. This page is about your own data; the example schema used across this documentation has its own command, aura db seed --example docs (Example schema).
Prerequisites#
-
A linked project:
aura link <project-id>. -
The example schema, installed with the service role key of the project:
export AURABASE_SERVICE_ROLE_KEY=aura_sk_… aura db seed --example docsIt creates
profiles,orgs,org_members,posts,todos, the viewopen_todosand the functiontodo_count(p_user uuid). See Example schema.
Seed a test project or a branch, never production. The examples create the table wf_seed_projects, the file aurabase/seed.sql and two users of the project; the Clean up block removes them.
aura db raw "create table wf_seed_projects (
id bigint generated always as identity primary key,
slug text not null unique,
name text not null,
owner uuid)" ℹ No results (0 rows).Which command loads the file#
| Command | Runs aurabase/seed.sql on | Works on a linked project |
|---|---|---|
aura db raw -f aurabase/seed.sql | The linked project, through the SQL API, in one transaction | Yes, with the service_role key |
aura db seed | The database of DATABASE_URL only | No: it fails with No database configured, even when a project is linked |
aura db seed --example docs | The linked project (or DATABASE_URL without a link) | Yes; it installs the example schema, not your file |
aura db seed ignores the linked project and looks for DATABASE_URL:
mkdir -p aurabase
echo "select 1;" > aurabase/seed.sql
aura db seed 2>&1 || echo "exit code $?"» Seeding database...
Error: No database configured. Link a project with 'aura link' (commands will use the API), or set DATABASE_URL (environment variable or .env) for a local database.
exit code 1On a linked project, use aura db raw -f. aura db seed remains the command for a local database (Local development).
Write a seed file you can run twice#
A seed runs again on every new branch, on every reset of a test project, and by mistake. Make each statement a no-op when its rows are already there: give the rows a natural key with a unique constraint and use on conflict do nothing, or insert with where not exists. Do not insert values into an identity column: let it number the rows.
cat > aurabase/seed.sql <<'SQL'
-- Reference data: safe to run again
insert into wf_seed_projects (slug, name) values
('website', 'Website'),
('mobile-app', 'Mobile app'),
('internal', 'Internal tools')
on conflict (slug) do nothing;
SQLLoad it#
aura db raw -f aurabase/seed.sql
aura db raw -f aurabase/seed.sql
aura db raw "select slug, name from wf_seed_projects order by id" ℹ No results (0 rows).
ℹ No results (0 rows).
name │ slug
───────────────┼───────────
Website │ website
Mobile app │ mobile-app
Internal tools │ internal
ℹ 3 row(s).Twice the file, three rows. (aura db raw prints columns in alphabetical order.) aura db raw -f sends the whole file as one request: it counts once against the SQL quota of the project (60 requests per minute on the Free plan by default) and follows the rules of Run SQL from the CLI or a server: no copy, no do blocks, no set_config.
A failing seed leaves nothing#
The file runs in one transaction. If a statement fails, the statements before it are rolled back too:
cat > aurabase/seed-broken.sql <<'SQL'
insert into wf_seed_projects (slug, name) values ('billing', 'Billing');
insert into wf_seed_projects (slug, name) values ('no-name', null);
SQL
aura db raw -f aurabase/seed-broken.sql 2>&1 || echo "exit code $?"
aura db raw "select count(*) as projects from wf_seed_projects"Error: [400] BAD_REQUEST: Invalid request: null value in column "name" of relation "wf_seed_projects" violates not-null constraint
exit code 1
projects
────────
3
ℹ 1 row(s).billing was not kept. Fix the file and run it again: there is no partial state to clean.
Rows that belong to users#
Rows owned by a user need a real user id: the users of a project live in its Auth schema, which SQL cannot write. Create the users with the CLI first, then use their ids in the seed. --json prints the user, id included; -p passes it to aura db raw as a text parameter, hence the cast $1::uuid:
for name in alice bob; do
aura auth users create --email "wf-seed-$name@example.com" --password 'correct-horse-battery-9' --json \
| node -pe 'JSON.parse(require("fs").readFileSync(0, "utf8")).id' > "aurabase/.user-$name"
done
aura db raw "update wf_seed_projects set owner = \$1::uuid where slug in ('website', 'internal')" -p "$(cat aurabase/.user-alice)"
aura db raw "update wf_seed_projects set owner = \$1::uuid where slug = 'mobile-app'" -p "$(cat aurabase/.user-bob)"
aura db raw "select slug, owner is not null as owned from wf_seed_projects order by id" ℹ No results (0 rows).
ℹ No results (0 rows).
owned │ slug
──────┼───────────
true │ website
true │ mobile-app
true │ internal
ℹ 3 row(s).- Creating a user twice fails with
[409] CONFLICT: Conflict: This email is already in use.: look the user up withaura auth users listinstead. - Users created this way are not confirmed (
email_verified: false). With email confirmation required, the default of a project, they cannot sign in:signInWithPasswordanswersEMAIL_NOT_CONFIRMED. On a test project, turn confirmation off withaura auth settings set email_confirmation_required=false. - A branch copies the rows of its source but not its users: the owner ids of the copied rows point to no user of the branch. Create the users on the branch and seed again.
Order and size#
- After the migrations. The seed needs the tables: run
aura db push, thenaura db raw -f aurabase/seed.sql. Keep the seed out ofaurabase/migrations: a migration runs once per project, a seed whenever you need fresh data. - Several files. Split a large seed into files that you load in a fixed order, one
aura db raw -feach; each file is its own transaction. - Large volumes. Every request through the gateway is limited by a statement timeout (Statement timeouts). For more than a few thousand rows, split the inserts into several files, or load the data with
psqlover the direct connection (Connect to your database). - Generated data.
insert … select … from generate_series(1, 1000)builds many rows in one statement.
Result#
aura db raw "select count(*) as projects, count(distinct owner) as owners from wf_seed_projects" owners │ projects
───────┼─────────
2 │ 3
ℹ 1 row(s).Common errors#
| Message | Cause | Fix |
|---|---|---|
No database configured. Link a project with 'aura link' (commands will use the API), or set DATABASE_URL … | aura db seed on a linked project: it only uses DATABASE_URL. | aura db raw -f aurabase/seed.sql. |
Seed file 'aurabase/seed.sql' not found. Create 'aurabase/seed.sql' first. | aura db seed without the file. | Create the file, or pass --example docs for the example schema. |
duplicate key value violates unique constraint … | The seed ran twice without on conflict do nothing. | Make each insert a no-op when the row exists. |
cannot insert a non-DEFAULT value into column "id" | The seed sets a generated always identity column. | Leave the column out. |
AURABASE_SERVICE_ROLE_KEY is not set. | aura db raw or aura db seed --example docs without the key. | Export the service_role key of the project. |
[409] CONFLICT: Conflict: This email is already in use. | The test user exists. | Reuse it: aura auth users list. |
Clean up#
for name in alice bob; do
if [ -f "aurabase/.user-$name" ]; then aura auth users delete "$(cat "aurabase/.user-$name")" --force > /dev/null; fi
done
aura db raw "drop table wf_seed_projects"
rm -rf aurabase ℹ No results (0 rows).