lazymigrate

package module
v0.0.0-...-223d9b4 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: ISC Imports: 5 Imported by: 1

README

lazymigrate

Package lazymigrate allows for simple SQLite migrations by using a magic comment to delimit different versions of the schema.

This package is meant to work with sqlc, which requires you to keep a separate schema.sql file for your database schema. This package allows you to keep multiple versions of the schema in the same file, and then use a magic comment to delimit the different versions.

Usage

Example SQL file:

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL
);

--------------------------------- NEW VERSION ---------------------------------

ALTER TABLE users ADD COLUMN email TEXT;
//go:embed schema.sql
var schema string

func main() {
    db, _ := sql.Open("sqlite3", "file::memory:")
    defer db.Close()

    migration := lazymigrate.NewSchema(schema)
    if err := migration.Migrate(context.TODO(), db); err != nil {
        log.Fatal(err)
    }
}

Documentation

Overview

package lazymigrate allows for simple SQLite migrations by using a magic comment to delimit different versions of the schema.

Index

Constants

View Source
const Delimiter = "--------------------------------- NEW VERSION ---------------------------------"

Delimiter is the default delimiter for the schema string. It is intentionally long and ugly to avoid collisions.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, db *sql.DB, schema string) error

Migrate migrates the database at the given source to the latest migrations. It is a convenience function around NewSchema and Schema.Migrate.

Types

type Schema

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

Schema wraps a SQLite schema string. A schema string is a series of SQL statements that create and modify tables. The schema string is delimited by a configurable magic comment. The magic comment must be on its own line before and after the schema string. It must not appear at the start or end of the schema string.

func NewSchema

func NewSchema(schema string) *Schema

NewSchema returns a new Schema with the given schema string. The schema string is delimited by the default magic comment Delimiter.

func NewSchemaWithMagic

func NewSchemaWithMagic(schema, magic string) *Schema

NewSchemaWithMagic returns a new Schema with the given schema string and magic comment.

func (*Schema) Migrate

func (s *Schema) Migrate(ctx context.Context, db *sql.DB) error

Migrate migrates the database at the given source to the latest migrations. It uses the user_version pragma. Note that the function does not set any pragma values except for user_version. If you need to set other pragmas, you must do so yourself.

The migrations are all done in a single transaction. If any migration fails, the transaction is rolled back and the error is returned.

func (*Schema) Versions

func (s *Schema) Versions() []string

Versions returns the versions of the schema.

Jump to

Keyboard shortcuts

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