internal/pgmapbroker/internal/sql/migrations/postgres_schema.md
EnsureSchema() manages the PostgreSQL schema for MapBroker automatically. It creates both JSONB and BYTEA schema variants in a single call (regardless of BinaryData config), uses integer-based versioning, and supports forward migrations.
The PostgreSQL MapBroker lives in Centrifugo at internal/pgmapbroker/.
| Change type | Mechanism | Migration needed? |
|---|---|---|
| New table | CREATE TABLE IF NOT EXISTS | No |
| New index | CREATE INDEX IF NOT EXISTS | No |
| Function body change | CREATE OR REPLACE FUNCTION | No |
| New function param with DEFAULT | CREATE OR REPLACE FUNCTION | No |
| New column on existing table | -- | Yes (ALTER TABLE ADD COLUMN IF NOT EXISTS) |
| Column type change | -- | Yes (ALTER TABLE ALTER COLUMN TYPE) |
| Function signature change (param/return types) | -- | Yes (DROP FUNCTION + CREATE) |
| Drop column | -- | Yes (two-phase: stop using, then drop) |
cf_map_schema_version (and cf_binary_map_schema_version).dbVersion+1 to schemaVersion.internal/pgmapbroker/internal/sql/schema_all.sql.SELECT schema_version FROM cf_map_schema_version WHERE id = 1.internal/pgmapbroker/internal/sql/migrations/.schemaVersion in internal/pgmapbroker/pgmapbroker.go.internal/pgmapbroker/internal/sql/migrations/NNN.sql -- explicit SQL for both prefixes, idempotent.schemaMigrations map (embed the file).schema.sql template to reflect the final state (fresh installs get everything).make pg-schemas to regenerate.Migration files are plain SQL targeting both prefixes explicitly:
-- Migration 002: Add foo column
ALTER TABLE cf_map_state ADD COLUMN IF NOT EXISTS foo TEXT;
ALTER TABLE cf_binary_map_state ADD COLUMN IF NOT EXISTS foo TEXT;
No template placeholders in migration files -- explicit is safer for production migrations.
Requirements on migration authors:
ADD COLUMN IF NOT EXISTS, etc.)cf_map_* and cf_binary_map_* prefixes