Remove DB.Init in favor of migrations using golang-migrate/migrate #2
7 changed files with 39 additions and 38 deletions
|
@ -12,5 +12,6 @@ psql -d postgres -U postgres -h ${DB_HOST} << EOF
|
||||||
CREATE DATABASE ${DB_NAME};
|
CREATE DATABASE ${DB_NAME};
|
||||||
CREATE USER ${DB_USER} WITH ENCRYPTED PASSWORD '${DB_PW}';
|
CREATE USER ${DB_USER} WITH ENCRYPTED PASSWORD '${DB_PW}';
|
||||||
GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};
|
GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};
|
||||||
|
ALTER DATABASE ${DB_NAME} OWNER TO ${DB_USER};
|
||||||
EOF
|
EOF
|
||||||
echo Done
|
echo Done
|
||||||
|
|
3
main.go
3
main.go
|
@ -23,11 +23,8 @@ func main() {
|
||||||
db, _ := database.NewDB()
|
db, _ := database.NewDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
auth.DB = db
|
auth.DB = db
|
||||||
auth.Init()
|
|
||||||
users.DB = db
|
users.DB = db
|
||||||
users.Init()
|
|
||||||
blog.DB = db
|
blog.DB = db
|
||||||
blog.Init()
|
|
||||||
|
|
||||||
// initiate jwt token
|
// initiate jwt token
|
||||||
auth.TokenAuth = jwtauth.New("HS256", []byte(os.Getenv("JWT_SECRET")), nil)
|
auth.TokenAuth = jwtauth.New("HS256", []byte(os.Getenv("JWT_SECRET")), nil)
|
||||||
|
|
4
migrations/000001_create_initial_tables.down.sql
Normal file
4
migrations/000001_create_initial_tables.down.sql
Normal file
|
@ -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;
|
34
migrations/000001_create_initial_tables.up.sql
Normal file
34
migrations/000001_create_initial_tables.up.sql
Normal file
|
@ -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;
|
|
@ -46,10 +46,6 @@ type JWT struct {
|
||||||
JWT string `json:"jwt"`
|
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 {
|
func Routes() *chi.Mux {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Post("/signin", signin)
|
r.Post("/signin", signin)
|
||||||
|
|
|
@ -57,26 +57,6 @@ type ReferenceID struct {
|
||||||
LastID int `json:"last_id"`
|
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 {
|
func Routes() *chi.Mux {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
|
@ -23,17 +23,6 @@ type User struct {
|
||||||
Bio string `json:"bio",db:"bio"`
|
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 {
|
func Routes() *chi.Mux {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
|
|
Reference in a new issue