Script Metadata Guide#
pgmi supports optional XML metadata blocks in SQL files that control deployment behavior: path-independent tracking, idempotency control, and explicit execution ordering.
Metadata is Optional#
You don’t need metadata to use pgmi.
A file without a <pgmi-meta> block executes in lexicographic path order, and pgmi generates a deterministic fallback identifier from its path. The basic template adds a block only to its run-once files (idempotent="false"), for the id its ledger tracks them by.
myproject/
├── deploy.sql # No metadata needed
├── migrations/
│ ├── 001_users.sql # No metadata needed
│ └── 002_orders.sql # No metadata needed
└── __test__/
└── test_users.sql # No metadata neededThis is often sufficient. Add metadata only when you need its specific capabilities.
When to Use Metadata#
Use metadata when you need:#
| Capability | Without Metadata | With Metadata |
|---|---|---|
| Script identity | Path-based (changes if file moves) | UUID-based (survives renames) |
| Execution order | Lexicographic by path | Explicit via sort keys |
| Re-execution control | All scripts re-run every deploy | Idempotent vs one-time |
| Multi-phase execution | Not possible | Same file at multiple stages |
Consider metadata for:#
- Production deployments where you need idempotency control (one-time migrations vs always-update functions)
- Large projects (20+ SQL files) where explicit ordering matters
- Team projects where file paths may change during refactoring
- Complex deployments with distinct phases (bootstrap → migrate → setup → seed)
Skip metadata for:#
- Simple projects with < 10 SQL files
- Linear migrations (001_, 002_, 003_ naming works fine)
- Prototype or throwaway projects
- Test files (
__test__/or__tests__/directories - metadata is ignored there anyway)
Metadata Syntax#
Metadata lives in a SQL block comment at the top of the file:
/*
<pgmi-meta
id="550e8400-e29b-41d4-a716-446655440000"
idempotent="true">
<description>What this script does</description>
<sortKeys>
<key>10-utils/0010</key>
</sortKeys>
</pgmi-meta>
*/
-- Your SQL code follows...
CREATE OR REPLACE FUNCTION ...Attributes#
| Attribute | Required | Type | Description |
|---|---|---|---|
id | Yes | UUID | Unique identifier (RFC 4122 format) |
idempotent | Yes | boolean | true = always re-run, false = run once |
Child Elements#
| Element | Required | Description |
|---|---|---|
<description> | No | Human-readable explanation |
<sortKeys> | No | Execution order keys (defaults to file path) |
Script Identity (UUID)#
The id attribute gives your script a stable identity independent of its file path.
Why This Matters#
# Before refactoring:
migrations/001_create_users.sql → tracked by path
# After refactoring:
database/schema/users.sql → NEW path = lost tracking history!With metadata UUID:
/*
<pgmi-meta id="a1b2c3d4-e5f6-7890-abcd-ef1234567890" idempotent="false">
<description>Create users table</description>
</pgmi-meta>
*/Now the file can move anywhere and pgmi still knows it’s the same script. One-time migrations won’t re-run just because you reorganized your project.
Generating UUIDs#
# Linux/Mac
uuidgen
# PowerShell
[guid]::NewGuid()
# PostgreSQL
SELECT gen_random_uuid();
# Or use pgmi's scaffold command (see CLI Commands below)Fallback Identity#
Files without metadata get a deterministic UUID based on their path:
generic_id = md5(raw_path)::uuidThis means:
./migrations/001.sqlalways gets the same fallback UUID- Case changes (
./SETUP/File.SQLvs./setup/file.sql) produce different UUIDs — use explicit<pgmi-meta>id if you need rename-stable identity - The fallback UUID is not RFC-4122 compliant (it matches PostgreSQL’s
md5(path::bytea)::uuid) - But path changes (
./old/file.sql→./new/file.sql) get different UUIDs
Use explicit metadata when path stability matters.
Idempotency Control#
The idempotent attribute controls whether a script runs every deployment or just once.
idempotent="true" - Always Re-run#
Use for scripts that are safe (or intended) to run multiple times:
/*
<pgmi-meta id="..." idempotent="true">
<description>User API handlers - always update to latest</description>
</pgmi-meta>
*/
-- Safe to run repeatedly
CREATE OR REPLACE FUNCTION api.get_users()
RETURNS SETOF users AS $$
SELECT * FROM users WHERE deleted_at IS NULL;
$$ LANGUAGE sql;Use for:
CREATE OR REPLACE FUNCTION/VIEWCREATE TABLE IF NOT EXISTS- Configuration updates
- Reference data upserts (
ON CONFLICT DO UPDATE)
idempotent="false" - Run Once#
Use for scripts that should only execute once:
/*
<pgmi-meta id="..." idempotent="false">
<description>Add email column (one-time migration)</description>
</pgmi-meta>
*/
-- Should only run once
ALTER TABLE users ADD COLUMN email TEXT;Use for:
ALTER TABLE ADD COLUMN- Data migrations
- Destructive operations (
DROP,TRUNCATE). Not because a second run fails (DROP ... IF EXISTSdoes not), but because it destroys data written since the first - Non-idempotent
INSERTstatements
How Tracking Works#
The advanced template maintains an execution log:
internal.deployment_script_execution_log
├── deployment_script_object_id (UUID)
├── deployment_script_content_checksum
├── pgmi_checksum
├── file_path
├── idempotent
├── sort_key
└── executed_atOn each deployment:
- Idempotent scripts: Always execute; each run adds a log row
- Non-idempotent scripts: Skipped once a run under
idempotent="false"is logged. No per-file notice; the closing[pgmi] Deployment complete: N executed, M skippedline counts them - Changed non-idempotent scripts: a run-once script that already ran here and was since edited, moved, re-sorted, flipped to
idempotent="true"or removed refuses the deploy before any file runs, naming each file. Comment, whitespace and keyword-case edits are not changes (pgmi’s normalized checksum)
Execution Order (Sort Keys)#
Sort keys give you explicit control over execution order, overriding the default lexicographic path ordering.
Basic Usage#
/*
<pgmi-meta id="..." idempotent="true">
<sortKeys>
<key>10-utils/0010</key>
</sortKeys>
</pgmi-meta>
*/Scripts execute in sort key order (ascending), with path as tiebreaker.
Sort Key Conventions#
Choose a convention and stick to it:
Layer-based (recommended for frameworks):
000 # Bootstrap (roles, schemas, extensions)
001/000 # Layer 1: Utilities
002/000 # Layer 2: Internal infrastructure
003/000 # Layer 3: Core domain
004/000 # Layer 4: API layer
005/000 # Layer 5: Application codePhase-based (recommended for migrations):
01-pre-deployment/001
02-migrations/001
03-setup/001
04-post-deployment/001Date-based (for chronological migrations):
migrations/2025-01-15/001
migrations/2025-01-15/002
migrations/2025-01-16/001No Sort Keys = Path Order#
If you omit <sortKeys>, the file path becomes the sort key:
/*
<pgmi-meta id="..." idempotent="true">
<description>Uses path as sort key</description>
</pgmi-meta>
*/This file sorts by its path (e.g., ./migrations/001_users.sql).
Multi-Phase Execution#
A single script can execute at multiple deployment stages by specifying multiple sort keys. Only a re-run script (idempotent="true") can: a run-once script runs once, at one place, and both templates refuse a run-once script with more than one key.
Example: Bootstrap + Seed#
/*
<pgmi-meta id="..." idempotent="true">
<description>Create roles schema, then populate later</description>
<sortKeys>
<key>000/020</key> <!-- Early in deployment -->
<key>005/010</key> <!-- Late in deployment -->
</sortKeys>
</pgmi-meta>
*/
-- Both statements run EACH time the file executes.
-- The file is idempotent, so re-execution is safe.
CREATE SCHEMA IF NOT EXISTS roles;
INSERT INTO roles.role (name) VALUES ('admin'), ('user')
ON CONFLICT (name) DO NOTHING;How It Works#
Each <key> generates a separate execution entry. The entire file runs at each stage:
Execution Plan:
#5: sort_key='000/020', path='./roles.sql' (full file runs — schema + data)
#47: sort_key='005/010', path='./roles.sql' (full file runs again — both are no-ops)This is useful when other scripts between the two stages depend on what this file creates. The file must be idempotent (idempotent="true") since it runs multiple times.
Use Cases#
- Deferred initialization: Create schema early, populate data later
- Circular dependencies: Define types early, create functions that use them later
- Configuration refresh: Apply initial config, then override after other setup
CLI Commands#
pgmi provides commands to work with metadata:
Scaffold Metadata#
Generate metadata blocks for files that don’t have them:
# Preview what would be generated for one layer
pgmi metadata scaffold ./myproject/core --sort-key 005/core/
# Write metadata to files
pgmi metadata scaffold ./myproject/core --sort-key 005/core/ --write
# Generate with idempotent=false default
pgmi metadata scaffold ./myproject/migrations --sort-key 004/ --idempotent=false --writeEach block gets a random id and the key <prefix>/<file name>. The prefix
comes from --sort-key, or from the prefix the files with metadata in the same
directory already share; with neither, the command refuses before writing. The
path cannot supply it: api/orders.sql sorts before core/orders.sql, but the
kernel has to run first.
Validate Metadata#
Check for syntax errors and conflicts:
# Human-readable output
pgmi metadata validate ./myproject
# JSON output for CI/CD
pgmi metadata validate ./myproject --jsonValidates:
- XML syntax
- Required attributes (
id,idempotent) - UUID format
- Duplicate IDs across files
- Empty sort keys
It also warns about a <pgmi-meta> outside a /* */ block comment, such as
-- <pgmi-meta ...>: the parser never reads it, so the file has no metadata.
Preview Execution Plan#
See the execution order before deploying:
# Human-readable table
pgmi metadata plan ./myproject
# JSON output
pgmi metadata plan ./myproject --jsonExamples#
Example 1: Simple Utility (Idempotent)#
/*
<pgmi-meta
id="a1b2c3d4-e5f6-4789-a012-3456789abcde"
idempotent="true">
<description>Safe UUID casting utility</description>
<sortKeys>
<key>001/010</key>
</sortKeys>
</pgmi-meta>
*/
CREATE OR REPLACE FUNCTION common.try_cast(input text, default_value uuid)
RETURNS uuid
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE AS $$
BEGIN
RETURN input::uuid;
EXCEPTION WHEN invalid_text_representation THEN
RETURN default_value;
END;
$$;Result: Executes every deployment, always updates function definition.
Example 2: One-Time Migration#
/*
<pgmi-meta
id="f9e8d7c6-b5a4-4321-9876-543210fedcba"
idempotent="false">
<description>Add soft delete support</description>
<sortKeys>
<key>02-migrations/2025-01-15/001</key>
</sortKeys>
</pgmi-meta>
*/
ALTER TABLE users ADD COLUMN deleted_at TIMESTAMPTZ;
CREATE INDEX idx_users_active ON users(id) WHERE deleted_at IS NULL;Result: Executes once, skipped on subsequent deployments.
Example 3: Minimal Metadata#
/*
<pgmi-meta id="12345678-1234-5678-1234-567812345678" idempotent="true">
</pgmi-meta>
*/
-- Uses path for sort order, tracks by UUID
CREATE TABLE IF NOT EXISTS logs (id SERIAL PRIMARY KEY);Result: Minimal metadata for UUID tracking, path-based ordering.
Error Messages#
pgmi metadata validate <path> exits 10 on the first problem it can name.
A malformed block stops the scan:
$ pgmi metadata validate ./myproject
pgmi: error: validation failed: invalid PGMI metadata in ./migrations/001_users.sql:
1. id attribute is required and cannot be the nil UUID (00000000-0000-0000-0000-000000000000).
Each script must have a unique identifier.
Generate with: uuidgen (Linux/Mac), [guid]::NewGuid() (PowerShell), or https://www.uuidgenerator.net/
See metadata format documentation:
Format reference: .agents/skills/pgmi-metadata/SKILL.md (scaffolded by pgmi init) or https://github.com/vvka-141/pgmi/blob/main/docs/METADATA.md
Generate template: pgmi metadata scaffold <path>Uniqueness is a property of the whole project, so it is checked after every file parses — the summary prints first, then the collision:
$ pgmi metadata validate ./myproject
Total files: 2
Files with metadata: 2
Files without metadata: 0
Duplicate id: 550e8400-e29b-41d4-a716-446655440000: ./migrations/001_users.sql, ./setup/users_setup.sql
pgmi: error: metadata validation failed: 1 duplicate id(s)Give one of the two files a new UUID. You do not have to run validate to be
protected: pgmi deploy rejects the same collision during file scanning, before
it connects, with exit 10, and so does pgmi metadata plan:
pgmi: error: duplicate <pgmi-meta id> across files; each script must have a unique id:
550e8400-e29b-41d4-a716-446655440000: ./migrations/001_users.sql, ./setup/users_setup.sqlAdding metadata to existing files#
The basic template’s physical/ files already carry an id and idempotent="false". For a project of your own without <pgmi-meta> blocks, or files you move into the advanced template:
Assess need: Do you actually need idempotency control or path-independent tracking?
Scaffold metadata, one folder at a time:
pgmi metadata scaffold ./myproject/physical --sort-key 005/ --idempotent=false --writeReview and customize:
- Adjust
idempotentflags (tables → false, views and functions → true) - Set meaningful sort keys if order matters
- Add descriptions for documentation
- Adjust
Validate:
pgmi metadata validate ./myprojectPreview execution:
pgmi metadata plan ./myproject
Summary#
| Feature | When to Use | How |
|---|---|---|
| UUID identity | Files may be renamed/moved | Add id="..." attribute |
| Idempotency | Mix of one-time and repeatable scripts | Set idempotent="true/false" |
| Sort keys | Need explicit ordering | Add <sortKeys><key>...</key></sortKeys> |
| Multi-phase | Same script at different stages | Multiple <key> elements |
Remember: Metadata is a power tool, not a requirement. Start simple, add metadata when you need its specific capabilities.