Skip to content

Commit

Permalink
don't create db
Browse files Browse the repository at this point in the history
  • Loading branch information
jakehobbs committed Jan 19, 2024
1 parent f2c6b8d commit a9f70cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
79 changes: 39 additions & 40 deletions service/db.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
package main

import (
"fmt"
"github.com/dxe/helptheducks.com/service/config"
"github.com/jmoiron/sqlx"
"log"
"strings"
)

func mustGetOrCreateDb() *sqlx.DB {
dsn, dbName := splitDbNameFromDsn(config.Dsn)

db, err := sqlx.Open("mysql", dsn)
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
fmt.Println("Connected to database")

// Create the database if it doesn't exist.
query := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", dbName)
_, err = db.Exec(query)
if err != nil {
log.Fatalf("Error creating database: %v", err)
}
// TODO: make a separate script to init db b/c this is kinda sketch for prod.
func getDb() *sqlx.DB {
//dsn, dbName := splitDbNameFromDsn(config.Dsn)
//
//db, err := sqlx.Open("mysql", dsn)
//if err != nil {
// log.Fatalf("Error connecting to database: %v", err)
//}
//fmt.Println("Connected to database")
//
//// Create the database if it doesn't exist.
//query := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", dbName)
//_, err = db.Exec(query)
//if err != nil {
// log.Fatalf("Error creating database: %v", err)
//}

// Connect to the specific database.
db, err = sqlx.Open("mysql", config.Dsn)
db, err := sqlx.Open("mysql", config.Dsn)
if err != nil {
log.Fatalf("Error connecting to specified database: %v", err)
}

createTableQuery := `
CREATE TABLE IF NOT EXISTS messages (
id INT AUTO_INCREMENT PRIMARY KEY,
submitted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
ip_address VARCHAR(255),
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(255),
outside_us BOOLEAN NOT NULL DEFAULT FALSE,
zip VARCHAR(5),
city VARCHAR(255),
message TEXT,
status VARCHAR(20) NOT NULL DEFAULT 'PENDING'
)`
_, err = db.Exec(createTableQuery)
if err != nil {
log.Fatalf("Error creating messages table: %v", err)
}
//createTableQuery := `
// CREATE TABLE IF NOT EXISTS messages (
// id INT AUTO_INCREMENT PRIMARY KEY,
// submitted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
// ip_address VARCHAR(255),
// name VARCHAR(255) NOT NULL,
// email VARCHAR(255) NOT NULL,
// phone VARCHAR(255),
// outside_us BOOLEAN NOT NULL DEFAULT FALSE,
// zip VARCHAR(5),
// city VARCHAR(255),
// message TEXT,
// status VARCHAR(20) NOT NULL DEFAULT 'PENDING'
// )`
//_, err = db.Exec(createTableQuery)
//if err != nil {
// log.Fatalf("Error creating messages table: %v", err)
//}

return db
}

func splitDbNameFromDsn(dsn string) (string, string) {
parts := strings.Split(dsn, "/")
return strings.Join(parts[:len(parts)-1], "/") + "/", parts[len(parts)-1]
}
//func splitDbNameFromDsn(dsn string) (string, string) {
// parts := strings.Split(dsn, "/")
// return strings.Join(parts[:len(parts)-1], "/") + "/", parts[len(parts)-1]
//}
2 changes: 1 addition & 1 deletion service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
MaxAge: 300, // Maximum value not ignored by any of major browsers.
}))

db = mustGetOrCreateDb()
db = getDb()
defer db.Close()

r.Route("/message", func(r chi.Router) {
Expand Down

0 comments on commit a9f70cf

Please sign in to comment.