aboutsummaryrefslogtreecommitdiff
path: root/internal/dbmigrations.go
diff options
context:
space:
mode:
authorSamuel Johnson <[email protected]>2025-11-24 13:53:18 -0500
committerSamuel Johnson <[email protected]>2025-11-24 13:53:18 -0500
commit368a462bc744d8e9084eacfaddeb9afcaf7f7133 (patch)
treec6e8f665d6cb9713b9226b10c4a341e60b8e91c2 /internal/dbmigrations.go
parent4d4419f51557bef6b64dca8635ed61616d262a9b (diff)
Add session management
Diffstat (limited to 'internal/dbmigrations.go')
-rw-r--r--internal/dbmigrations.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/internal/dbmigrations.go b/internal/dbmigrations.go
new file mode 100644
index 0000000..e6c11ff
--- /dev/null
+++ b/internal/dbmigrations.go
@@ -0,0 +1,51 @@
+package internal
+
+import (
+ "database/sql"
+ "fmt"
+ "os"
+
+ "golang.org/x/crypto/bcrypt"
+)
+
+func Migrate (db *sql.DB, webmaster string, passOne string, passTwo string) {
+ _, table_check := db.Query("SELECT * FROM logins;")
+ if table_check != nil {
+ _, err := db.Exec("CREATE TABLE logins (id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, pass_one TEXT NOT NULL, pass_two TEXT NOT NULL);")
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Unable to create logins table: %v\n", err)
+ os.Exit(1)
+ }
+
+ hashOne, errHashOne := bcrypt.GenerateFromPassword([]byte(passOne), 12)
+ hashTwo, errHashTwo := bcrypt.GenerateFromPassword([]byte(passTwo), 12)
+ if errHashOne != nil || errHashTwo != nil {
+ fmt.Fprintf(os.Stderr, "Failed to hash password")
+ os.Exit(1)
+ }
+
+ _, err = db.Exec(fmt.Sprintf("INSERT INTO logins (name, pass_one, pass_two) VALUES ('%s', '%s', '%s');", webmaster, hashOne, hashTwo))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Unable to add webmaster to logins table: %v\n", err)
+ os.Exit(1)
+ }
+ }
+
+ _, table_check = db.Query("SELECT * FROM posts;")
+ if table_check != nil {
+ _, err := db.Exec("CREATE TABLE posts (id SERIAL PRIMARY KEY, user_id INTEGER REFERENCES logins(id), time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, content TEXT NOT NULL);")
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Unable to create posts table: %v\n", err)
+ os.Exit(1)
+ }
+ }
+
+
+ _, table_check = db.Query("SELECT * FROM cookies;")
+ if table_check != nil {
+ _, err := db.Exec("CREATE TABLE cookies (id SERIAL PRIMARY KEY, content VARCHAR(255) NOT NULL, user_id INTEGER REFERENCES logins(id), expiration TIMESTAMP);")
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Unable to create cookies table: %v\n", err)
+ }
+ }
+}