Skip to content

Dump the database schema as db/structure.sql - #53

Closed
kf8a wants to merge 2 commits into
masterfrom
chore/structure-sql
Closed

kf8a wants to merge 2 commits into
masterfrom
chore/structure-sql

Conversation

@kf8a

@kf8a kf8a commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

The db/schema.rb from 7f502da can't be loaded into a fresh database, so test databases and new machines can't be set up. It was dumped from a database with things schema.rb can't describe (PostGIS and other extensions, the topology schema, public.barcode with a nanoid(13) default). This switches to db/structure.sql, written by pg_dump:

  • config.active_record.schema_format = :sql
  • config.active_record.dump_schemas = 'public' (changed from :all in 27922e4)
  • --no-owner --no-privileges for the dump (config/initializers/structure_dump.rb), since owners and grants are roles on the machine it came from
  • db/schema.rb removed
  • db/structure.sql added (27922e4, 7,756 lines)

Status: the committed structure.sql doesn't load into a fresh database yet

I loaded the pushed file into a fresh PostgreSQL 16 database (rails db:schema:load). It stops at the first error, and a lenient load shows why:

  1. Needs PostgreSQL 17 or newer. The header has SET transaction_timeout = 0;, which PostgreSQL 16 rejects. (It was dumped by a 17+ pg_dump.)
  2. CREATE SCHEMA public; fails on any database that already has a public schema, which is every new one.
  3. No CREATE EXTENSION lines. With dump_schemas = 'public', pg_dump leaves extensions out, so public.geometry (PostGIS) doesn't exist when the tables and views that use it are created.
  4. It contains the research database, not only the app. Of 105 tables, 85 are the app's. The other 20 are not used by the app (for example areas, locations, farm_fields0, barcode, oban_jobs, analysis_labs, 2_ed_lux_arbor_microplots), and there are 18 views (for example kbs013_001_2, mle_polygons, n2o_project_weather). Several views read other schemas (weather, kbs013, kbs019, kbs020, test) that the dump doesn't include, so they can't be created. Nothing in app/, lib/, config/ or the specs uses PostGIS, nanoid() or barcode.

A lenient load on 16 (with items 1 and 2 stripped) reports 569 errors, mostly from 3 and 4, and the test database ends up without several app tables.

Likely fix (not done yet)

Dump from a scratch database that holds only the app: load the schema from before 7f502da, run the migrations, then bin/rails db:schema:dump. That contains only the app's objects, loads on PostgreSQL 16 and later, and drops the research tables and views from the Rails schema. If the app really does need PostGIS or nanoid(), they should be added by a migration (enable_extension, CREATE FUNCTION) instead of coming along in the dump.

Testing

  • On a scratch database built from the previous schema, plus PostGIS and a stand-in nanoid(): a dump with dump_schemas = :all loaded into a fresh test database, and the full suite passed (307 examples, 0 failures).
  • The committed db/structure.sql (27922e4) was not loadable, as described above; the suite was not run against it.

lib/parser_test.rb still loads db/schema.rb; it's an old SQLite script and is left as is.

🤖 Generated with Claude Code

https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7

The database now has things schema.rb can't describe: PostGIS and other
extensions, the topology schema, and functions such as nanoid() used in
column defaults (public.barcode). The schema.rb dumped from it couldn't be
loaded into a fresh database, so test databases and new machines couldn't be
set up. structure.sql, written by pg_dump, keeps all of it.

- schema_format :sql, dumping every schema so the extensions' own come along
- the dump leaves out owners and grants, which are roles on the machine it
  came from and would stop it loading elsewhere
- db/schema.rb is removed; db/structure.sql has to be dumped from a database
  that has those objects

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016AR3uoZS1uCC8hXmojk3K7
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