Switching to SQLite and creating the tables
This commit is contained in:
@@ -3,43 +3,34 @@ package db
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"go-sjles-pta-vote/config"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func Connect() error {
|
||||
var err error
|
||||
dbConfig := config.GetConfig()
|
||||
|
||||
db, err = sql.Open(
|
||||
"postgres",
|
||||
fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||||
dbConfig.Host,
|
||||
dbConfig.Port,
|
||||
dbConfig.User,
|
||||
dbCofnig.Password,
|
||||
dbConfig.Database
|
||||
)
|
||||
)
|
||||
db, err = sql.Open("sqlite", dbConfig.DBPath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = db.Ping(); err != nil {
|
||||
sql_create, err := ioutil.ReadFile("./db_format.sql")
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Connected to PostgreSQL")
|
||||
|
||||
return nil
|
||||
_, err = db.Exec(sql_create)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Is there a way to close automatically on app closure
|
||||
func Close() {
|
||||
if db != nil {
|
||||
_ = db.Close()
|
||||
|
||||
25
server/db/db_format.sql
Normal file
25
server/db/db_format.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
CREATE TABLE IF NOT EXISTS poll (
|
||||
id UNSIGNED INT NOT NULL AUTOINCREMENT,
|
||||
question TEXT NOT NULL,
|
||||
member_yes_votes UNSIGNED INT NOT NULL,
|
||||
member_no_votes UNSIGNED INT NOT NULL,
|
||||
non_member_yes_votes UNSIGNED INT NOT NULL,
|
||||
non_member_no_votes UNSIGNED INT NOT NULL,
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME,
|
||||
expires_at DATETIME,
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS voters (
|
||||
poll_id UNSIGNED INT NOT NULL,
|
||||
voter_email TEXT NOT NULL,
|
||||
FOREIGN KEY (poll_id) poll(id),
|
||||
PRIMARY KEY (poll_id, voter_email)
|
||||
)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS members (
|
||||
email TEXT NOT NULL,
|
||||
member_name TEXT,
|
||||
PRIMARY KEY (email)
|
||||
)
|
||||
Reference in New Issue
Block a user