From 9efabe0906524ed38584640df2d1bb55bfc3def9 Mon Sep 17 00:00:00 2001 From: Asara Date: Tue, 14 Jan 2020 22:40:32 -0500 Subject: [PATCH] Split out migrations --- db_reset.sh | 1 + main.go | 3 -- .../000001_create_initial_tables.down.sql | 4 +++ .../000001_create_initial_tables.up.sql | 34 +++++++++++++++++++ packages/auth/auth.go | 4 --- packages/blog/blog.go | 20 ----------- packages/users/users.go | 11 ------ 7 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 migrations/000001_create_initial_tables.down.sql create mode 100644 migrations/000001_create_initial_tables.up.sql diff --git a/db_reset.sh b/db_reset.sh index b5b5cc6..dc5563e 100755 --- a/db_reset.sh +++ b/db_reset.sh @@ -12,5 +12,6 @@ psql -d postgres -U postgres -h ${DB_HOST} << EOF CREATE DATABASE ${DB_NAME}; CREATE USER ${DB_USER} WITH ENCRYPTED PASSWORD '${DB_PW}'; GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER}; +ALTER DATABASE ${DB_NAME} OWNER TO ${DB_USER}; EOF echo Done diff --git a/main.go b/main.go index 50d071b..f990e93 100644 --- a/main.go +++ b/main.go @@ -23,11 +23,8 @@ func main() { db, _ := database.NewDB() defer db.Close() auth.DB = db - auth.Init() users.DB = db - users.Init() blog.DB = db - blog.Init() // initiate jwt token auth.TokenAuth = jwtauth.New("HS256", []byte(os.Getenv("JWT_SECRET")), nil) diff --git a/migrations/000001_create_initial_tables.down.sql b/migrations/000001_create_initial_tables.down.sql new file mode 100644 index 0000000..77cf3fd --- /dev/null +++ b/migrations/000001_create_initial_tables.down.sql @@ -0,0 +1,4 @@ +DROP TABLE IF EXISTS tags; +DROP TABLE IF EXISTS posts; +DROP TABLE IF EXISTS user_profiles; +DROP TABLE IF EXISTS users; diff --git a/migrations/000001_create_initial_tables.up.sql b/migrations/000001_create_initial_tables.up.sql new file mode 100644 index 0000000..23497ff --- /dev/null +++ b/migrations/000001_create_initial_tables.up.sql @@ -0,0 +1,34 @@ +BEGIN; +CREATE TABLE IF NOT EXISTS users ( + username text PRIMARY KEY, + email text, + password text, + admin boolean +); + +CREATE TABLE IF NOT EXISTS user_profiles ( + id SERIAL PRIMARY KEY, + username text REFERENCES users (username), + email text, + location text, + bio text +); + +CREATE TABLE IF NOT EXISTS posts ( + id SERIAL PRIMARY KEY, + title text, + slug text, + author text REFERENCES users (username), + content text, + time_published timestamp, + modified bool, + last_modified timestamp +); + +CREATE TABLE IF NOT EXISTS tags ( + id SERIAL PRIMARY KEY, + tag text, + article_id int REFERENCES posts (id) +); + +COMMIT; diff --git a/packages/auth/auth.go b/packages/auth/auth.go index 2f453cc..fe02414 100644 --- a/packages/auth/auth.go +++ b/packages/auth/auth.go @@ -46,10 +46,6 @@ type JWT struct { JWT string `json:"jwt"` } -func Init() { - DB.Exec("CREATE TABLE IF NOT EXISTS users (username text primary key, email text, password text, admin boolean);") -} - func Routes() *chi.Mux { r := chi.NewRouter() r.Post("/signin", signin) diff --git a/packages/blog/blog.go b/packages/blog/blog.go index ca995ec..2c1d630 100644 --- a/packages/blog/blog.go +++ b/packages/blog/blog.go @@ -57,26 +57,6 @@ type ReferenceID struct { LastID int `json:"last_id"` } -func Init() { - createPostsTable := ` - CREATE TABLE IF NOT EXISTS posts - (id SERIAL PRIMARY KEY, - title text, - slug text, - author text REFERENCES users (username), - content text, - time_published timestamp, - modified bool, - last_modified timestamp)` - DB.Exec(createPostsTable) - - createTagsTable := ` - CREATE TABLE IF NOT EXISTS tags - (id SERIAL PRIMARY KEY, - tag text, - article_id int REFERENCES posts (id))` - DB.Exec(createTagsTable) -} func Routes() *chi.Mux { r := chi.NewRouter() diff --git a/packages/users/users.go b/packages/users/users.go index a1aa6c6..447aa1b 100644 --- a/packages/users/users.go +++ b/packages/users/users.go @@ -23,17 +23,6 @@ type User struct { Bio string `json:"bio",db:"bio"` } -func Init() { - dbCreateStatement := ` - CREATE TABLE IF NOT EXISTS user_profiles - (id SERIAL PRIMARY KEY, - username text REFERENCES users (username), - email text, - location text, - bio text)` - DB.Exec(dbCreateStatement) -} - func Routes() *chi.Mux { r := chi.NewRouter() r.Group(func(r chi.Router) {