custom-target
Implementing bunmigrator.Target yourself, for objects the built-ins do not
cover: a Postgres extension (extension.go) and a view (view.go),
registered alongside a normal bunmigrator.Table.
Extension renders CREATE EXTENSION IF NOT EXISTS "pgcrypto" /
DROP EXTENSION IF EXISTS "pgcrypto".
View renders CREATE OR REPLACE VIEW "active_users" AS SELECT ... FROM "users" WHERE "deleted_at" IS NULL / DROP VIEW IF EXISTS "active_users",
and depends on User -- the table its query reads from -- so it is never
created first. Its Slug is create_view_active_users specifically so
bunmigrator.Changes reports ADDED VIEW active_users rather than
falling back to the generic APPLIED create_view_active_users.
Both are ordinary values, not pointers, and both are checked against
bunmigrator.Target at compile time with a var _ bunmigrator.Target = ...
line -- the same pattern the Table and Enum types in this module would
use if they needed it.
This example does not add a custom table-like target using
AutoUpdatedAtTrigger: bunmigrator.Table with WithAutoUpdatedAt()
already covers every case a bun model can express, so a hand-rolled
alternative here would only duplicate it. AutoUpdatedAtTrigger is exported
for the case this example does not have -- a table with no bun model behind
it at all.
Run it
go run ./examples/custom-target --dry-run
What you should see
Three migrations: pgcrypto (the extension), users (the table), then
active_users (the view) -- in that order, because active_users depends
on users. The output contains both CREATE EXTENSION IF NOT EXISTS and
CREATE OR REPLACE VIEW.