cmd

package
v0.0.0-...-ec3f0cb Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2016 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Migrations []*Migration = []*Migration{
	{
		ID:          1,
		Description: "ladon tables",
		MigrateUp: func(db *sqlx.DB) error {
			return ladon.NewSQLManager(db, nil).CreateSchemas()
		},
		MigrateDown: func(db *sqlx.DB) error {
			var tables []string
			if err := db.Select(&tables, util.HereDoc(`
				SELECT table_name
				FROM information_schema.tables
				WHERE table_name LIKE 'ladon_%';
			`)); err != nil {
				return err
			}
			for _, t := range tables {
				if _, err := db.Exec(`DROP IF EXISTS ` + t + ` CASCADE;`); err != nil {
					return err
				}
			}
			return nil
		},
	},
	{
		ID:          2,
		Description: "boilerplate tables",
		MigrateUp: func(db *sqlx.DB) error {
			tx, err := db.Begin()
			if err != nil {
				return err
			}
			if _, err = tx.Exec(util.HereDocf(`
				CREATE TABLE IF NOT EXISTS %[1]s_role (
					id SERIAL PRIMARY KEY,
					policy_id VARCHAR(255) NOT NULL,
					created_at TIMESTAMP WITH TIME ZONE DEFAULT (now() AT TIME ZONE 'utc'),
					updated_at TIMESTAMP WITH TIME ZONE DEFAULT (now() AT TIME ZONE 'utc')
				);
				ALTER TABLE %[1]s_role ADD CONSTRAINT fk_%[1]s_role_policy_id FOREIGN KEY (policy_id) REFERENCES ladon_policy(id);
				CREATE TABLE IF NOT EXISTS %[1]s_user (
					id SERIAL PRIMARY KEY,
					username TEXT NOT NULL,
					password TEXT NOT NULL,
					role_id INTEGER NOT NULL,
					created_at TIMESTAMP WITH TIME ZONE DEFAULT (now() AT TIME ZONE 'utc'),
					updated_at TIMESTAMP WITH TIME ZONE DEFAULT (now() AT TIME ZONE 'utc')
				);
				ALTER TABLE %[1]s_user ADD CONSTRAINT fk_%[1]s_user_role_id FOREIGN KEY (role_id) REFERENCES %[1]s_role(id);
			`, tablePrefix)); err != nil {
				return err
			}
			if err := tx.Commit(); err != nil {
				return err
			}
			return nil
		},
		MigrateDown: func(db *sqlx.DB) error {
			tx, err := db.Begin()
			if err != nil {
				return err
			}
			if _, err = tx.Exec(util.HereDocf(`
				DROP TABLE IF EXISTS %s_user CASCADE;
				DROP TABLE IF EXISTS %s_role CASCADE;
			`, tablePrefix)); err != nil {
				return err
			}
			if err = tx.Commit(); err != nil {
				return err
			}
			return nil
		},
	},
}

Functions

func CreateAuditTriggers

func CreateAuditTriggers(db *sqlx.DB) error

func CreateMigrationsTable

func CreateMigrationsTable(db *sqlx.DB) error

func Execute

func Execute()

func MigrationsTableExists

func MigrationsTableExists(db *sqlx.DB) bool

Types

type Migration

type Migration struct {
	ID          uint64               `db:"id" json:"id"`
	Description string               `db:"description" json:"description"`
	CreatedAt   time.Time            `db:"created_at" json:"created_at"`
	UpdatedAt   time.Time            `db:"updated_at" json:"updated_at"`
	MigrateUp   func(*sqlx.DB) error `db:"-" json:"-"`
	MigrateDown func(*sqlx.DB) error `db:"-" json:"-"`
}

func GetCurrentMigration

func GetCurrentMigration(db *sqlx.DB) *Migration

Jump to

Keyboard shortcuts

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