/* Simple API the serves pictures of dogs Copyright (C) 2021 Pin This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ 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) }