Skip to content

Commit

Permalink
Add support for SQLite3 databases, migration support will follow
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Nov 27, 2024
1 parent a8c35e5 commit ffbc0d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
8 changes: 3 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ CS_BRANCH="main"
# Can be one of: "us", "eu", "azure-na" or "azure-eu".
CS_REGION=

DB_HOST="127.0.0.1"
DB_PORT="6033"
DB_USER="root"
DB_PASS="root"
DB_NAME="db"
# Format: USER:PASS@tcp(HOST:PORT)/DB
DB_CONN="root:root@tcp(127.0.0.1:6033)/db"
DB_TYPE="mysql"

# vim: set ft=sh:
26 changes: 15 additions & 11 deletions cmd/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@ package database

import (
"database/sql"
"fmt"
"errors"
"os"

_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
)

var DB *sql.DB

func Connect() error {
connString := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s",
os.Getenv("DB_USER"),
os.Getenv("DB_PASS"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_NAME"),
)

db, err := sql.Open("mysql", connString)
connString := os.Getenv("DB_CONN")

if connString == "" {
return errors.New("DB_CONN is not set")
}

dbType := os.Getenv("DB_TYPE")

if dbType == "" {
return errors.New("DB_TYPR is not set")
}

db, err := sql.Open(dbType, connString)

if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/sys v0.27.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
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/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
Expand Down

0 comments on commit ffbc0d1

Please sign in to comment.