Hold onto user accounts during migration drop
This commit is contained in:
parent
3c790dc9ff
commit
c4969084ff
3 changed files with 28 additions and 13 deletions
23
main.go
23
main.go
|
@ -126,13 +126,36 @@ func cmdMigrateDb(c *cli.Context) {
|
|||
log.Fatal("Error initializing migrations: ", err)
|
||||
}
|
||||
|
||||
users := make(Users, 0)
|
||||
|
||||
if c.Bool("drop") {
|
||||
// Back up users table
|
||||
if err := DBH.Select(&users, `SELECT * FROM users;`); err != nil {
|
||||
log.Fatal("Couldn't back up identity tables: ", err)
|
||||
}
|
||||
log.Printf("%+v Users", len(users))
|
||||
|
||||
// Drop tables
|
||||
if err = migrator.RollbackAll(); err != nil && err != gomigrate.NoActiveMigrations {
|
||||
log.Fatal("Error rolling back migrations: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Run migrations
|
||||
if err = migrator.Migrate(); err != nil {
|
||||
log.Fatal("Error applying migrations: ", err)
|
||||
}
|
||||
|
||||
// If we dropped, restore the user records
|
||||
if c.Bool("drop") {
|
||||
// Stick users back into DB
|
||||
if len(users) > 0 {
|
||||
// varargs don't seem to work here, loop instead
|
||||
for _, user := range users {
|
||||
if err := DBH.Insert(user); err != nil {
|
||||
log.Fatal("Couldn't restore user: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
-- bactdb
|
||||
-- Matthew R Dillon
|
||||
|
||||
-- Need to include something to keep gomigrate happy.
|
||||
-- SELECT 1;
|
||||
|
||||
DROP TABLE users;
|
||||
|
||||
DROP TYPE e_roles;
|
||||
|
|
|
@ -1,17 +1,12 @@
|
|||
-- bactdb
|
||||
-- Matthew R Dillon
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'e_roles') THEN
|
||||
CREATE TYPE e_roles AS ENUM('R', 'W', 'A');
|
||||
-- 'R': read-only, default
|
||||
-- 'W': read-write
|
||||
-- 'A': administrator
|
||||
END IF;
|
||||
END$$;
|
||||
CREATE TYPE e_roles AS ENUM('R', 'W', 'A');
|
||||
-- 'R': read-only, default
|
||||
-- 'W': read-write
|
||||
-- 'A': administrator
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
CREATE TABLE users (
|
||||
id BIGSERIAL NOT NULL,
|
||||
email CHARACTER VARYING(254) NOT NULL UNIQUE,
|
||||
password CHARACTER(60) NOT NULL,
|
||||
|
|
Reference in a new issue