Skip to content

Restore a db/schema.rb that loads into a fresh database - #57

Merged
kf8a merged 1 commit into
masterfrom
fix/loadable-schema
Nov 3, 2026
Merged

kf8a merged 1 commit into
masterfrom
fix/loadable-schema

Conversation

@kf8a

@kf8a kf8a commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Summary

The db/schema.rb committed in 7f502da can't be loaded into a fresh database, so test databases and new machines can't be set up from it. It was dumped from the research database the app shares a server with, so it carries things the app doesn't use and a fresh database can't create:

  • PostGIS and other extensions (postgis, postgis_raster, topology.postgis_topology, postgres_fdw, citext, fuzzystrmatch, pgcrypto, uuid-ossp, btree_gist), the topology schema and three enums
  • public.barcode, whose id defaults to a nanoid(13) function
  • 21 other tables, none used by the app (for example oban_jobs, areas, locations, farm_fields0, analysis_labs, users_tokens, spatial_ref_sys)

This restores the schema as it was before that commit, regenerated from a scratch database (load the old schema, confirm no migration is pending, dump), so it holds only the app's 85 tables and needs no extension beyond plpgsql. The regenerated file is byte-for-byte the pre-7f502dacd schema.

Why not trim the newer file instead

I tried it (removing the extras from the 7f502da file), and it loads after one more fix (protocols uses a legacy methocols_id_seq sequence that a fresh database lacks). But 53 specs then fail. The research database has foreign keys the schema the app and its specs were built on doesn't (46 against 17), for example datasets.website_id to websites, and the factories create records that break them. So the database has drifted from the migrations (see below), and this PR keeps to the migration-built schema the suite was written against.

Drift between your database and the migrations

Not changed here, but worth knowing. Compared with this schema, the database 7f502da was dumped from has:

  • extra columns, for example authors.orchid, citations.abstract_backup, affiliations.lno_record_id
  • extra or different constraints and defaults, for example NOT NULL on affiliations.person_id, and datasets.version with a default of 1 (the migration-built schema has no default)
  • about 30 more foreign keys
  • no plots, species, open_id_authentication_* tables, which the migrations still create

If those columns and constraints are meant to be part of the app, they need migrations.

Keeping it loadable

bin/rails db:migrate in development rewrites db/schema.rb from whatever database it runs against, which is how the extras got in. To add a migration without that, run it against a scratch database:

createdb schema_scratch
DATABASE_URL=postgres:///schema_scratch RAILS_ENV=development bin/rails db:schema:load db:migrate

Testing

  • Loaded with rails db:schema:load into a fresh PostgreSQL 16 database with no extensions except plpgsql (87 tables including schema_migrations and ar_internal_metadata): loads cleanly.
  • Full suite on that database: 317 examples, 0 failures, 3 pending.
  • Supersedes the closed Dump the database schema as db/structure.sql #53 (structure.sql), whose dump had the same problem.

🤖 Generated with Claude Code

https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7

The schema committed in 7f502da was dumped from the research database
the app shares a server with, so it carried things the app doesn't use and
a fresh database can't create: PostGIS and other extensions, the topology
schema, enums, public.barcode with a nanoid() default, and 21 other tables
(for example oban_jobs, areas, locations). Test databases and new machines
couldn't be set up from it.

This is the schema as it was before that commit, regenerated from a scratch
database (load the old schema, run the migrations, dump), so it holds only
the app's tables and needs no extensions beyond plpgsql. The full suite
passes on a database loaded from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7
@kf8a
kf8a merged commit 72b0d6a into master Nov 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants