All Projects â†’ gsamokovarov â†’ gloat

gsamokovarov / gloat

Licence: MIT license
Next-gen database migrations framework for Go.

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to gloat

Datofu
there's a :db/fn for that
Stars: ✭ 104 (+593.33%)
Mutual labels:  migrations
Postgres Migrations
🐊 A Stack Overflow-inspired PostgreSQL migration library with strict ordering and immutable migrations
Stars: ✭ 161 (+973.33%)
Mutual labels:  migrations
Secondbase
Seamless second database integration for Rails.
Stars: ✭ 216 (+1340%)
Mutual labels:  migrations
Dbatools
🚀 SQL Server automation and instance migrations have never been safer, faster or freer
Stars: ✭ 1,742 (+11513.33%)
Mutual labels:  migrations
Flask Migrate
SQLAlchemy database migrations for Flask applications using Alembic
Stars: ✭ 1,971 (+13040%)
Mutual labels:  migrations
Typeorm Nestjs Migration Example
"Example of how to use migrations feature of TypeORM with NestJS.
Stars: ✭ 176 (+1073.33%)
Mutual labels:  migrations
Yii2 Migrik
Yii2 Gii-tools for create migration files
Stars: ✭ 99 (+560%)
Mutual labels:  migrations
elastic-migrations
Elasticsearch migrations for Laravel
Stars: ✭ 153 (+920%)
Mutual labels:  migrations
Express Typescript Boilerplate
A delightful way to building a RESTful API with NodeJs & TypeScript by @w3tecch
Stars: ✭ 2,293 (+15186.67%)
Mutual labels:  migrations
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 2,315 (+15333.33%)
Mutual labels:  migrations
Activerecord Pg enum
Integrate PostgreSQL's enumerated types with the Rails enum feature
Stars: ✭ 125 (+733.33%)
Mutual labels:  migrations
Request Migrations
HTTP Request Migrations for API Versioning like Stripe
Stars: ✭ 149 (+893.33%)
Mutual labels:  migrations
Laravel Migrate Fresh
An artisan command to build up a database from scratch
Stars: ✭ 179 (+1093.33%)
Mutual labels:  migrations
Cassandra Migrate
Simple Cassandra schema migration tool written in Python
Stars: ✭ 114 (+660%)
Mutual labels:  migrations
Django Migration Linter
🚀 Detect backward incompatible migrations for your django project
Stars: ✭ 231 (+1440%)
Mutual labels:  migrations
Lol dba
lol_dba is a small package of rake tasks that scan your application models and displays a list of columns that probably should be indexed. Also, it can generate .sql migration scripts.
Stars: ✭ 1,363 (+8986.67%)
Mutual labels:  migrations
Django Swappable Models
Swapper - The unofficial Django swappable models API.
Stars: ✭ 169 (+1026.67%)
Mutual labels:  migrations
MoalemYar
A personal project for class management, using various technologies like WPF, Entityframwork, CodeFirst, Sqlite, Migration and more
Stars: ✭ 53 (+253.33%)
Mutual labels:  migrations
Peewee migrate
Simple migration engine for Peewee
Stars: ✭ 250 (+1566.67%)
Mutual labels:  migrations
Cli
The Sequelize CLI
Stars: ✭ 2,248 (+14886.67%)
Mutual labels:  migrations

Build Status

Gloat /ɡlōt/

Contemplate or dwell on one's own success or another's misfortune with smugness or malignant pleasure.

Gloat is a modular SQL migration library for the Go programming language. Being a library, gloat can be easily integrated into your application or ORM.

Library

If you are using gloat as a library, the main components you'll be dealing with are migration, source, store and SQL executor. You'll be using those through the methods on the Gloat struct.

db, err := sql.Open("postgres", "connection string")
if err != nil {
	// Handle the *sql.DB creation error.
}

gl := gloat.Gloat{
	Store:    gloat.NewPostgreSQLStore(db),
	Source:   gloat.NewFileSystemSource("migrations"),
	Executor: gloat.NewSQLExecutor(db),
}

Migration

Migration holds all the relevant information for a migration. The content of the forward (up) side of a migration, the backward (down) side, a path and version. The version is used to determine the order of which the migrations would be executed. The path is the name in a store.

type Migration struct {
	UpSQL   []byte
	DownSQL []byte
	Path    string
	Version int64
}

Source

The Source interface represents a source of migration. The most common source is the file system.

type Source interface {
	Collect() (Migrations, error)
}

gloat.NewFileSystemSource is a constructor function that creates a source that collects migrations from a folder with the following structure:

migrations/
└── 20170329154959_introduce_domain_model
    ├── down.sql
    └── up.sql

In the example above migrations is a folder that stores all of the migrations. A migrations itself is a folder with a name in the form of :timestamp_:name containing up.sql and down.sql files.

The up.sql file contains the SQL that's executed when a migration is applied:

CREATE TABLE users (
    id    bigserial PRIMARY KEY NOT NULL,
    name  character varying NOT NULL,
    email character varying NOT NULL,

    created_at  timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updated_at  timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL
);

While down.sql is executed when a migration is reverted:

DROP TABLE users;

If the down.sql file is not present, we say that a migration is irreversible.

Store

The Store is an interface representing a place where the applied migrations are recorded. The common thing is to store the migrations in a database table. The gloat.DatabaseStore does just that. The gloat.NewPostgreSQLStore constructor function creates such store that records the migration in a table called schema_migrations. The table is automatically created if it does not exist.

type Store interface {
	Source

	Insert(*Migration, StoreExecer) error
	Remove(*Migration, StoreExecer) error
}

The Store.Insert records the migration version in to the schema_migrations table, while Store.Remove deletes the column with the version from the table. There are the following builtin store constructors:

// NewPostgreSQLStore creates a Store for PostgreSQL.
func NewPostgreSQLStore(db *sql.DB) Store {}

// NewMySQLStore creates a Store for MySQL.
func NewMySQLStore(db *sql.DB) Store {}

// NewSQLite3Store creates a Store for SQLite3.
func NewSQLite3Store(db *sql.DB) Store {}

Executor

The Executor interface, well, it executes the migrations. For SQL migrations, there is the gloat.SQLExecutor implementation. It's an interface, nevertheless, so you can fake it out during testing.

type Executor interface {
	Up(*Migration, Store) error
	Down(*Migration, Store) error
}

The executor executes the migration UpSQL or DownSQL sections.

Gloat

A Gloat binds a migration Source, Store and Executor into one thing, so it's easier to Apply, and Revert migrations.

gl := gloat.Gloat{
	Store:    gloat.NewPostgreSQLStore(db),
	Source:   gloat.NewFileSystemSource("migrations"),
	Executor: gloat.NewSQLExecutor(db),
}

// Applies all of the unapplied migrations.
if migrations, err := gl.Unapplied(); err == nil {
	for _, migration := range migrations {
		gl.Apply(migration)
	}
}

// Revert the last applied migration.
if migration, err := gl.Current(); err == nil {
	gl.Revert(migration)
}

Here is a description for the main Gloat methods.

// Unapplied returns the unapplied migrations in the current gloat.
func (c *Gloat) Unapplied() (Migrations, error) {}

// Current returns the latest applied migration. Even if no error is returned,
// the current migration can be nil.
//
// This is the case when the last applied migration is no longer available from
// the source or there are no migrations to begin with.
func (c *Gloat) Current() (*Migration, error) {}

// Apply applies a migration.
func (c *Gloat) Apply(migration *Migration) error {}

// Revert rollbacks a migration.
func (c *Gloat) Revert(migration *Migration) error {}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].