darwinx

package module
v0.0.0-...-4a53fc8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 10 Imported by: 0

README

darwinx

Postgres schema evolution library for Go with pgx
Based on https://github.com/GuiaBolso/darwin

Example

package main

import (
	"context"
	"github.com/jackc/pgx/v5"
	"github.com/pkg/errors"
	"net/url"
)

var (
	migrations = []darwinx.Migration{
		{
			Version:     1,
			Description: "Creating table posts",
			Script: `CREATE TABLE posts (
						id INT 		auto_increment, 
						title 		VARCHAR(255),
						PRIMARY KEY (id)
					 );`,
		},
		{
			Version:     2,
			Description: "Adding column body",
			Script:      "ALTER TABLE posts ADD body TEXT AFTER title;",
		},
	}
)

func main() {
	conn, _ := pgx.Connect(context.Background(), (&url.URL{
        Scheme:   "postgres",
        User:     url.UserPassword("postgres", "admin"),
        Host:     "localhost:5432/tcp",
        Path:     "db",
        RawQuery: "sslmode=disable&timezone=UTC",
    }).String())

	d, _ := darwinx.New(conn, darwinx.WithMigration(migrations))

	err := d.Migrate()
	if err != nil {
		log.Println(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Darwinx

type Darwinx struct {
	// contains filtered or unexported fields
}

Darwinx is a helper struct to access the Validate and migration functions

func New

func New(conn *pgx.Conn, options ...Option) (*Darwinx, error)

New returns a new Darwinx struct

func (*Darwinx) Info

func (d *Darwinx) Info(ctx context.Context) ([]MigrationInfo, error)

Info returns the status of all migrations

func (*Darwinx) Migrate

func (d *Darwinx) Migrate(ctx context.Context) error

Migrate executes the missing migrations in database Apply all Migration or rollback

func (*Darwinx) Validate

func (d *Darwinx) Validate(ctx context.Context) error

Validate if the database migrations are applied and consistent

type DuplicateMigrationVersionError

type DuplicateMigrationVersionError struct {
	Version float64
}

DuplicateMigrationVersionError is used to report when the migration list has duplicated entries

func (DuplicateMigrationVersionError) Error

type IllegalMigrationVersionError

type IllegalMigrationVersionError struct {
	Version float64
}

IllegalMigrationVersionError is used to report when the migration has an illegal Version number

func (IllegalMigrationVersionError) Error

type InvalidChecksumError

type InvalidChecksumError struct {
	Version float64
}

InvalidChecksumError is used to report when a migration was modified

func (InvalidChecksumError) Error

func (i InvalidChecksumError) Error() string

type Migration

type Migration struct {
	Version     float64
	Description string
	Script      string
}

Migration represents a database migrations.

func MigrationsFromString

func MigrationsFromString(content string) (migrations []Migration, err error)

func (Migration) Checksum

func (m Migration) Checksum() string

Checksum calculate the Script sha256

type MigrationInfo

type MigrationInfo struct {
	Status    Status
	Error     error
	Migration Migration
}

MigrationInfo is a struct used in the infoChan to inform clients about the migration being applied.

type MigrationRecord

type MigrationRecord struct {
	Version       float64
	Description   string
	Checksum      string
	AppliedAt     time.Time
	ExecutionTime time.Duration
}

MigrationRecord is the entry in schema table

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithMigration

func WithMigration(migrations []Migration) Option

func WithTableName

func WithTableName(tn string) Option

type RemovedMigrationsError

type RemovedMigrationsError struct {
	Versions []float64
}

RemovedMigrationError is used to report when a migration is removed from the list

func (RemovedMigrationsError) Error

func (r RemovedMigrationsError) Error() string

type Status

type Status uint8

Status is a migration status value

const (
	// Ignored means that the migrations was not applied to the database
	Ignored Status = iota
	// Applied means that the migrations was successfully applied to the database
	Applied
	// Pending means that the migrations is a new migration and it is waiting to be applied to the database
	Pending
	// Error means that the migration could not be applied to the database
	Error
)

func (Status) String

func (s Status) String() string

Jump to

Keyboard shortcuts

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