github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/database/migrate/1_initial.go (about) 1 package migrate 2 3 import ( 4 "fmt" 5 6 "github.com/go-pg/migrations" 7 ) 8 9 const accountTable = ` 10 CREATE TABLE accounts ( 11 id serial NOT NULL, 12 created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, 13 updated_at timestamp with time zone DEFAULT current_timestamp, 14 last_login timestamp with time zone NOT NULL DEFAULT current_timestamp, 15 email text NOT NULL UNIQUE, 16 name text NOT NULL, 17 active boolean NOT NULL DEFAULT TRUE, 18 roles text[] NOT NULL DEFAULT '{"user"}', 19 PRIMARY KEY (id) 20 )` 21 22 const tokenTable = ` 23 CREATE TABLE tokens ( 24 id serial NOT NULL, 25 created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, 26 updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, 27 account_id int NOT NULL REFERENCES accounts(id), 28 token text NOT NULL UNIQUE, 29 expiry timestamp with time zone NOT NULL, 30 mobile boolean NOT NULL DEFAULT FALSE, 31 identifier text, 32 PRIMARY KEY (id) 33 )` 34 35 func init() { 36 up := []string{ 37 accountTable, 38 tokenTable, 39 } 40 41 down := []string{ 42 `DROP TABLE tokens`, 43 `DROP TABLE accounts`, 44 } 45 46 migrations.Register(func(db migrations.DB) error { 47 fmt.Println("creating initial tables") 48 for _, q := range up { 49 _, err := db.Exec(q) 50 if err != nil { 51 return err 52 } 53 } 54 return nil 55 }, func(db migrations.DB) error { 56 fmt.Println("dropping initial tables") 57 for _, q := range down { 58 _, err := db.Exec(q) 59 if err != nil { 60 return err 61 } 62 } 63 return nil 64 }) 65 }