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 CreateMigrationsTable ¶
func MigrationsTableExists ¶
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 ¶
Click to show internal directories.
Click to hide internal directories.