Remove DB.Init in favor of migrations using golang-migrate/migrate #2

Merged
Asara merged 1 commit from migrations into master 2020-01-15 04:12:02 +00:00
7 changed files with 39 additions and 38 deletions

View file

@ -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

View file

@ -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)

View 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;

View 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;

View file

@ -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)

View file

@ -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()

View file

@ -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) {