From 13e0efb67c2689251faeb5451b4ad739da8025ff Mon Sep 17 00:00:00 2001 From: paul Date: Tue, 4 Nov 2025 13:59:11 -0500 Subject: [PATCH 1/2] Switching to SQLite and creating the tables --- go.mod | 17 +++++++++++++++++ go.sum | 23 +++++++++++++++++++++++ server/config/config.go | 18 +++--------------- server/db/db.go | 27 +++++++++------------------ server/db/db_format.sql | 25 +++++++++++++++++++++++++ server/models/poll.go | 4 ++-- 6 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 server/db/db_format.sql diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c5eda1c --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module go-sjles-pta-vote/server + +go 1.24.4 + +require github.com/glebarez/go-sqlite v1.22.0 + +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/google/uuid v1.5.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/sys v0.15.0 // indirect + modernc.org/libc v1.37.6 // indirect + modernc.org/mathutil v1.6.0 // indirect + modernc.org/memory v1.7.2 // indirect + modernc.org/sqlite v1.28.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e635754 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ= +github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw= +modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= +modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= +modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ= +modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0= diff --git a/server/config/config.go b/server/config/config.go index e63d509..4621721 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -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") { diff --git a/server/db/db.go b/server/db/db.go index 14793cb..69321c4 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -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() diff --git a/server/db/db_format.sql b/server/db/db_format.sql new file mode 100644 index 0000000..a72260a --- /dev/null +++ b/server/db/db_format.sql @@ -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) +) diff --git a/server/models/poll.go b/server/models/poll.go index 4d98457..078c62e 100644 --- a/server/models/poll.go +++ b/server/models/poll.go @@ -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"` } \ No newline at end of file From 3e2efae4ced3e836c7f1f7c617705c8c5be2c006 Mon Sep 17 00:00:00 2001 From: paul Date: Tue, 4 Nov 2025 14:41:49 -0500 Subject: [PATCH 2/2] Adding tests --- server/db/db.go | 15 ++++++--------- server/db/db_format.sql | 15 +++++++-------- server/db/db_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 server/db/db_test.go diff --git a/server/db/db.go b/server/db/db.go index 69321c4..60c1ccc 100644 --- a/server/db/db.go +++ b/server/db/db.go @@ -2,31 +2,28 @@ package db import ( "database/sql" - "fmt" - "io/ioutil" - - "go-sjles-pta-vote/config" + "os" _ "github.com/glebarez/go-sqlite" ) var db *sql.DB -func Connect() error { +func Connect(db_path string) error { var err error - db, err = sql.Open("sqlite", dbConfig.DBPath) + db, err = sql.Open("sqlite", db_path) if err != nil { return err } - sql_create, err := ioutil.ReadFile("./db_format.sql") + sql_create, err := os.ReadFile("./db_format.sql") if err != nil { return err } - _, err = db.Exec(sql_create) + _, err = db.Exec(string(sql_create)) return err } @@ -35,4 +32,4 @@ func Close() { if db != nil { _ = db.Close() } -} \ No newline at end of file +} diff --git a/server/db/db_format.sql b/server/db/db_format.sql index a72260a..1721e07 100644 --- a/server/db/db_format.sql +++ b/server/db/db_format.sql @@ -1,5 +1,5 @@ -CREATE TABLE IF NOT EXISTS poll ( - id UNSIGNED INT NOT NULL AUTOINCREMENT, +CREATE TABLE IF NOT EXISTS polls ( + id INTEGER PRIMARY KEY, question TEXT NOT NULL, member_yes_votes UNSIGNED INT NOT NULL, member_no_votes UNSIGNED INT NOT NULL, @@ -7,19 +7,18 @@ CREATE TABLE IF NOT EXISTS poll ( non_member_no_votes UNSIGNED INT NOT NULL, created_at DATETIME, updated_at DATETIME, - expires_at DATETIME, - PRIMARY KEY (id) -) + expires_at DATETIME +); CREATE TABLE IF NOT EXISTS voters ( poll_id UNSIGNED INT NOT NULL, voter_email TEXT NOT NULL, - FOREIGN KEY (poll_id) poll(id), + FOREIGN KEY (poll_id) REFERENCES polls(id), PRIMARY KEY (poll_id, voter_email) -) +); CREATE TABLE IF NOT EXISTS members ( email TEXT NOT NULL, member_name TEXT, PRIMARY KEY (email) -) +); diff --git a/server/db/db_test.go b/server/db/db_test.go new file mode 100644 index 0000000..ef57785 --- /dev/null +++ b/server/db/db_test.go @@ -0,0 +1,24 @@ +package db + +import ( + "os" + "testing" +) + +func TestConnect(t *testing.T) { + tmp_db, err := os.CreateTemp("", "vote_test.*.db") + tmp_db_name := tmp_db.Name() + tmp_db.Close() + + defer os.Remove(tmp_db_name) + + if err != nil { + t.Errorf(`Failed to create temporary db: %v`, err) + } + + if err := Connect(tmp_db_name); err != nil { + t.Errorf(`Failed to create the database at %s: %v`, tmp_db_name, err) + } + + defer Close() +} \ No newline at end of file