Support external file embedding in seed/script SQL (for binary/text asset inlining) (redo) #6
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
Seed/script SQL files executed via
relspec scripts execute(pkg/writers/sqlexec + pkg/readers/sqldir) are read and executed verbatim — there is no mechanism to pull the content of an external file (e.g. a.mdor.docxasset sitting alongside the SQL) into the script at execution time.Concrete use case (origin project): we want to store report template source files as real files on disk —
sql/vault/borg/data/templates/finance/*.mdand*.docx— and have a seed script (sql/vault/borg/seeds/300_2_template_seed_data.sql) insert their content into abytea/textcolumn (viaorg.save_filepointer) duringmake migrate-apply. Today the only way to do this is to hand-encode the file content as a SQL literal (dollar-quoted text, ordecode('<base64>', 'base64')for binary) and commit that generated SQL — which means the actual template file and the SQL literal can drift out of sync, and binary files bloat/mangle badly as hand-maintained SQL.Requested feature
Support a directive in seed/script
.sqlfiles that tells the reader to inline an external file's content before sending the script to Postgres, e.g. a leading comment pragma:mode=text— file read as UTF-8 text, safely escaped/dollar-quoted into the substituted value.mode=base64— file read as bytes, base64-encoded, suitable fordecode(:var, 'base64')::bytea..sqlfile's own directory (so seeds can reference a siblingdata/folder without hardcoding absolute paths).pkg/readers/sqldirand therelspec scripts executecodepath (pkg/writers/sqlexec).Alternative considered
pg_read_file/pg_read_binary_fileinside the SQL itself was considered, but requires the asset to exist on the Postgres server's filesystem (not the migration client's), needs elevated privileges, and doesn't work against managed/remote Postgres — not viable for this use case.Workaround in the meantime
We'll generate the seed
.sqlfile's literals from the source assets via a small script invoked beforemake migrate-apply, and commit the generated file. Native support in relspec would let us drop that generation step and keep the seed script and the source asset as a single source of truth.warkanum referenced this issue2026-07-16 19:45:01 +00:00
Rather go with: #7
Maybe we can add this later.
Changed my mind. We can combine this with the asset loaded
Support external file embedding in seed/script SQL (for binary/text asset inlining)to Support external file embedding in seed/script SQL (for binary/text asset inlining) (redo)Implementation complete for issue #6.
PR: #12
Branch: issue-6-external-file-embedding
Commit:
1bcdf29206Summary:
-- @embed:directives for seed/script SQL.mode=textas escaped SQL text literals andmode=base64as base64 SQL literals fordecode(..., 'base64').Verification:
git diff --check-> passed, no outputgo test ./pkg/assetloader ./pkg/readers/sqldir ./pkg/writers/sqlexec-> passedgo test ./...-> passed