initial commit

Signed-off-by: Pin <wf6DJd8a3xSSCZbn@protonmail.com>
This commit is contained in:
Pin
2021-01-31 16:45:18 -05:00
committed by Pin
commit 6121f39c2a
11 changed files with 214 additions and 0 deletions

46
db/dbinit/dbinit.go Normal file
View File

@@ -0,0 +1,46 @@
package dbinit
import (
"log"
"fmt"
_ "github.com/lib/pq"
"database/sql"
"os"
)
func checkerr(err error) {
if err != nil {
log.Println(err)
}
}
func InitDB() {
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("DBUSER"), os.Getenv("PASSWD"), os.Getenv("DBNAME"))
db, err := sql.Open("postgres", psqlInfo)
checkerr(err)
defer db.Close()
_, err = db.Exec(`create table if not exists breed_manifest (
breed_id bigserial primary key,
breed text NOT NULL UNIQUE);`)
checkerr(err)
_, err = db.Exec(`create table if not exists dog_pictures (
dog_id bigserial primary key,
breed_id bigserial NOT NULL,
author text,
title text,
picture_format text NOT NULL,
picture_hash text NOT NULL UNIQUE,
picture_url text NOT NULL UNIQUE,
CONSTRAINT fk_breed_id
FOREIGN KEY (breed_id)
REFERENCES breed_manifest(breed_id));`)
checkerr(err)
}