migration

package
v1.214.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SET_LOCK_TIMEOUT         = "SET LOCAL 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)"
	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",
		"pgsodium",
		"pgtle",
		`supabase\_migrations`,
		"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 added in v1.203.0

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

func DropUserSchemas added in v1.186.0

func DropUserSchemas(ctx context.Context, conn *pgx.Conn) 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 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 added in v1.186.0

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 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 added in v1.203.0

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

func GetPendingSeeds added in v1.203.0

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

func NewSeedFile added in v1.203.0

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

func ReadSeedTable added in v1.203.0

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

func (*SeedFile) ExecBatchWithCache added in v1.203.0

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