Skip to content

Commit

Permalink
fix: removed db create / reset/ drop and upraded libs
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Jul 11, 2024
1 parent ef33473 commit 3cdc218
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 89 deletions.
81 changes: 0 additions & 81 deletions cmd/cmd_db.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package main

import (
"database/sql"
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -13,20 +10,6 @@ func dbCmd() *cobra.Command {
Short: "Create, setup, migrate, seed the database",
}

c1 := &cobra.Command{
Use: "create",
Short: "Create database",
Run: cmdDBCreate,
}
c.AddCommand(c1)

c2 := &cobra.Command{
Use: "drop",
Short: "Drop database",
Run: cmdDBDrop,
}
c.AddCommand(c2)

c.AddCommand(migrateCmd())

c3 := &cobra.Command{
Expand All @@ -44,14 +27,6 @@ func dbCmd() *cobra.Command {
}
c.AddCommand(c4)

c5 := &cobra.Command{
Use: "reset",
Short: "Reset database",
Long: "This command will drop, create, migrate and seed the database (won't run in production)",
Run: cmdDBReset,
}
c.AddCommand(c5)

return c
}

Expand All @@ -62,62 +37,6 @@ func cmdDBSetup(cmd *cobra.Command, args []string) {
log.Fatal("Database setup not support with MySQL")
}

cmdDBCreate(cmd, []string{})
cmdDBMigrate(cmd, []string{"up"})
cmdDBSeed(cmd, []string{})
}

func cmdDBReset(cmd *cobra.Command, args []string) {
setup(cpath)

if conf.Serv.Production {
log.Fatal("Command db:reset does not work in production")
}

cmdDBDrop(cmd, []string{})
cmdDBSetup(cmd, []string{})
}

func cmdDBCreate(cmd *cobra.Command, args []string) {
setup(cpath)
initDB(false)

if conf.DB.Type == "mysql" {
log.Fatalf("Database creation not support with MySQL")
}

dbName := conf.DB.DBName
dbExists := false

err := db.
QueryRow(`SELECT true as exists FROM pg_database WHERE datname = $1;`, dbName).
Scan(&dbExists)

if err != nil && err != sql.ErrNoRows {
log.Fatalf("Error checking if database exists: %s", err)
}

if dbExists {
log.Infof("Database exists: %s", dbName)
return
}

if _, err = db.Exec(`CREATE DATABASE "` + dbName + `"`); err != nil {
log.Fatalf("Failed to create database: %s", err)
}

log.Infof("Created database: %s", dbName)
}

func cmdDBDrop(cmd *cobra.Command, args []string) {
setup(cpath)
initDB(false)

sql := fmt.Sprintf(`DROP DATABASE IF EXISTS "%s"`, conf.DB.DBName)

if _, err := db.Exec(sql); err != nil {
log.Fatalf("Failed to drop database: %s", err)
}

log.Infof("Database dropped: %s", conf.DB.DBName)
}
2 changes: 0 additions & 2 deletions cmd/cmd_seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func compileAndRunJS(seed string, db *sql.DB) error {
"plugins": []string{
"proposal-async-generator-functions",
"proposal-class-properties",
"proposal-dynamic-import",
"proposal-json-strings",
"proposal-nullish-coalescing-operator",
"proposal-numeric-separator",
Expand Down Expand Up @@ -327,7 +326,6 @@ func importCSV(table, filename string, sep string, db *sql.DB) int64 {
pgx.Identifier{table},
cols,
s)

if err != nil {
err = fmt.Errorf("%w (line no %d)", err, s.i)
log.Fatalf("Error with copy-from: %s", err)
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwX
github.com/jackc/pgproto3/v2 v2.3.2/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag=
github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg=
github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc=
github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw=
Expand Down
6 changes: 3 additions & 3 deletions serv/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ func initPostgres(conf *Config, openDB, useTelemetry bool, fs core.FS) (*dbConf,
config.RuntimeParams["application_name"] = c.AppName
}

if openDB {
config.Database = c.DB.DBName
}
// if openDB {
config.Database = c.DB.DBName
// }

if c.DB.EnableTLS {
if len(c.DB.ServerName) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions serv/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-pkgz/expirable-cache v1.0.0
github.com/go-resty/resty/v2 v2.13.0
github.com/go-resty/resty/v2 v2.13.1
github.com/go-sql-driver/mysql v1.8.1
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gorilla/websocket v1.5.1
github.com/invopop/jsonschema v0.12.0
github.com/jackc/pgx/v5 v5.5.5
github.com/jackc/pgx/v5 v5.6.0
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/klauspost/compress v1.17.8
github.com/klauspost/compress v1.17.9
github.com/pkg/errors v0.9.1
github.com/rs/cors v1.11.0
github.com/spf13/afero v1.11.0
Expand Down
6 changes: 6 additions & 0 deletions serv/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ github.com/go-pkgz/expirable-cache v1.0.0 h1:ns5+1hjY8hntGv8bPaQd9Gr7Jyo+Uw5SLyI
github.com/go-pkgz/expirable-cache v1.0.0/go.mod h1:GTrEl0X+q0mPNqN6dtcQXksACnzCBQ5k/k1SwXJsZKs=
github.com/go-resty/resty/v2 v2.13.0 h1:joaL6wxSgm1OZal4FAAyddkL1T4uo5NxHYFkGmUusqE=
github.com/go-resty/resty/v2 v2.13.0/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0=
github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g=
github.com/go-resty/resty/v2 v2.13.1/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
Expand Down Expand Up @@ -207,6 +209,8 @@ github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
Expand All @@ -218,6 +222,8 @@ github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uia
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
Binary file modified wasm/graphjin.wasm
Binary file not shown.

0 comments on commit 3cdc218

Please sign in to comment.