Switching to SQLite and creating the tables

This commit is contained in:
2025-11-04 13:59:11 -05:00
parent 47310997ec
commit 13e0efb67c
6 changed files with 79 additions and 35 deletions

View File

@@ -8,11 +8,7 @@ import (
)
type Config struct {
Host string `json:"host"`
Port string `json:"port"`
User string `json:"user"`
Password string `json:"password"`
Database string `json:"database"`
DBPath string `json:"db_path"`
RedisHost string `json:"redis_host"`
RedisPassword string `json:"redis_password"`
}
@@ -49,16 +45,8 @@ func GetConfig() *Config {
// TODO: Better error checking if values are missing
// TODO: Default values
for key, value := range envMap {
if strings.Contains(key, "host") {
conf.Host = value
} else if strings.Contains(key, "port") {
conf.Port = value
} else if strings.Contains(key, "user") {
conf.User = value
} else if strings.Contains(key, "password") {
conf.Password = value
} else if strings.Contains(key, "database") {
conf.Database = value
if strings.Contains(key, "db_path") {
conf.DBPath = value
} else if strings.Contains(key, "redis_host") {
conf.RedisHost = value
} else if strings.Contains(key, "redis_password") {

View File

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

View File

@@ -2,10 +2,10 @@ package models
type Poll struct {
ID int `json:"id"`
Question string `json:"Question"`
Options []string `json:"options"`
Question string `json:"question"`
TotalVotes int `json:"total_votes"`
WhoVoted []string `json:"who_voted"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ExpiresAt string `json:"expires_at"`
}