migration

package
v0.0.0-...-a9967de Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SET_LOCK_TIMEOUT         = "SET lock_timeout = '4s'"
	CREATE_VERSION_SCHEMA    = "CREATE SCHEMA IF NOT EXISTS supabase_migrations"
	CREATE_VERSION_TABLE     = "CREATE TABLE IF NOT EXISTS supabase_migrations.schema_migrations (version text NOT NULL PRIMARY KEY)"
	ADD_STATEMENTS_COLUMN    = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS statements text[]"
	ADD_NAME_COLUMN          = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS name text"
	INSERT_MIGRATION_VERSION = "INSERT INTO supabase_migrations.schema_migrations(version, name, statements) VALUES($1, $2, $3)"
	UPSERT_MIGRATION_VERSION = INSERT_MIGRATION_VERSION + " ON CONFLICT (version) DO UPDATE SET name = EXCLUDED.name, statements = EXCLUDED.statements"
	DELETE_MIGRATION_VERSION = "DELETE FROM supabase_migrations.schema_migrations WHERE version = ANY($1)"
	DELETE_MIGRATION_BEFORE  = "DELETE FROM supabase_migrations.schema_migrations WHERE version <= $1"
	TRUNCATE_VERSION_TABLE   = "TRUNCATE supabase_migrations.schema_migrations"
	SELECT_VERSION_TABLE     = "SELECT version, coalesce(name, '') as name, statements FROM supabase_migrations.schema_migrations"
	LIST_MIGRATION_VERSION   = "SELECT version FROM supabase_migrations.schema_migrations ORDER BY version"
	CREATE_SEED_TABLE        = "CREATE TABLE IF NOT EXISTS supabase_migrations.seed_files (path text NOT NULL PRIMARY KEY, hash text NOT NULL)"
	UPSERT_SEED_FILE         = "INSERT INTO supabase_migrations.seed_files(path, hash) VALUES($1, $2) ON CONFLICT (path) DO UPDATE SET hash = EXCLUDED.hash"
	SELECT_SEED_TABLE        = "SELECT path, hash FROM supabase_migrations.seed_files"
)

Variables

View Source
var (
	ErrMissingRemote = errors.New("Found local migration files to be inserted before the last migration on remote database.")
	ErrMissingLocal  = errors.New("Remote migration versions not found in local migrations directory.")
)
View Source
var (
	//go:embed queries/drop.sql
	DropObjects string
	//go:embed queries/list.sql
	ListSchemas string

	// Initialised by postgres image and owned by postgres role
	ManagedSchemas = []string{
		`information\_schema`,
		`pg\_%`,
		`\_analytics`,
		`\_realtime`,
		`\_supavisor`,
		"pgbouncer",
		"pgmq",
		"pgsodium",
		"pgtle",
		`supabase\_migrations`,
		"vault",
	}
)
View Source
var (
	InternalSchemas = []string{
		"information_schema",
		"pg_*",

		"_analytics",
		"_realtime",
		"_supavisor",
		"auth",
		"etl",
		"extensions",
		"pgbouncer",
		"realtime",
		"storage",
		"supabase_functions",
		"supabase_migrations",

		"cron",
		"dbdev",
		"graphql",
		"graphql_public",
		"net",
		"pgmq",
		"pgsodium",
		"pgsodium_masks",
		"pgtle",
		"repack",
		"tiger",
		"tiger_data",
		"timescaledb_*",
		"_timescaledb_*",
		"topology",
		"vault",
	}
)

Functions

func ApplyMigrations

func ApplyMigrations(ctx context.Context, pending []string, conn *pgx.Conn, fsys fs.FS) error

func CreateMigrationTable

func CreateMigrationTable(ctx context.Context, conn *pgx.Conn) error

TODO: support overriding `supabase_migrations.schema_migrations` with user defined <schema>.<table>

func CreateSeedTable

func CreateSeedTable(ctx context.Context, conn *pgx.Conn) error

func DropUserSchemas

func DropUserSchemas(ctx context.Context, conn *pgx.Conn) error

func DumpData

func DumpData(ctx context.Context, config pgconn.Config, w io.Writer, exec ExecFunc, opts ...DumpOptionFunc) error

func DumpRole

func DumpRole(ctx context.Context, config pgconn.Config, w io.Writer, exec ExecFunc, opts ...DumpOptionFunc) error

func DumpSchema

func DumpSchema(ctx context.Context, config pgconn.Config, w io.Writer, exec ExecFunc, opts ...DumpOptionFunc) error

func FindPendingMigrations

func FindPendingMigrations(localMigrations, remoteMigrations []string) ([]string, error)

Find unapplied local migrations older than the latest migration on remote, and remote migrations that are missing from local.

func IsSchemaQualified

func IsSchemaQualified(typeName string) bool

IsSchemaQualified checks if a type name already contains a schema qualifier (e.g., "extensions.ltree")

func ListLocalMigrations

func ListLocalMigrations(migrationsDir string, fsys fs.FS, filter ...func(string) bool) ([]string, error)

func ListRemoteMigrations

func ListRemoteMigrations(ctx context.Context, conn *pgx.Conn) ([]string, error)

func ListUserSchemas

func ListUserSchemas(ctx context.Context, conn *pgx.Conn, exclude ...string) ([]string, error)

func SeedData

func SeedData(ctx context.Context, pending []SeedFile, conn *pgx.Conn, fsys fs.FS) error

func SeedGlobals

func SeedGlobals(ctx context.Context, pending []string, conn *pgx.Conn, fsys fs.FS) error

Types

type DumpOptionFunc

type DumpOptionFunc func(*pgDumpOption)

func WithColumnInsert

func WithColumnInsert(use bool) DumpOptionFunc

func WithComments

func WithComments(keep bool) DumpOptionFunc

func WithSchema

func WithSchema(schema ...string) DumpOptionFunc

func WithoutTable

func WithoutTable(table ...string) DumpOptionFunc

type ExecFunc

type ExecFunc func(context.Context, string, []string, io.Writer) error

type MigrationFile

type MigrationFile struct {
	Version    string
	Name       string
	Statements []string
}

func NewMigrationFromFile

func NewMigrationFromFile(path string, fsys fs.FS) (*MigrationFile, error)

func NewMigrationFromReader

func NewMigrationFromReader(sql io.Reader) (*MigrationFile, error)

func ReadMigrationTable

func ReadMigrationTable(ctx context.Context, conn *pgx.Conn) ([]MigrationFile, error)

func (*MigrationFile) ExecBatch

func (m *MigrationFile) ExecBatch(ctx context.Context, conn *pgx.Conn) error

type SeedFile

type SeedFile struct {
	Path  string
	Hash  string
	Dirty bool `db:"-"`
}

func GetPendingSeeds

func GetPendingSeeds(ctx context.Context, locals config.Glob, conn *pgx.Conn, fsys fs.FS) ([]SeedFile, error)

func NewSeedFile

func NewSeedFile(path string, fsys fs.FS) (*SeedFile, error)

func ReadSeedTable

func ReadSeedTable(ctx context.Context, conn *pgx.Conn) ([]SeedFile, error)

func (*SeedFile) ExecBatchWithCache

func (m *SeedFile) ExecBatchWithCache(ctx context.Context, conn *pgx.Conn, fsys fs.FS) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL