Skip to content

Commit

Permalink
add postgres mod storage
Browse files Browse the repository at this point in the history
fixes #84
  • Loading branch information
BuckarooBanzay committed Dec 29, 2023
1 parent fc62e0c commit ea23b0b
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 15 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func New(world_dir string) (*Context, error) {
ctx.mod_storage_db, err = connectAndMigrate(
dbtype,
path.Join(world_dir, "mod_storage.sqlite"),
"not implemented",
wc[worldconfig.CONFIG_PSQL_MOD_STORAGE_CONNECTION],
mod_storage.MigrateModStorageDB,
)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions mod_storage/mod_storage_migrate.go → mod_storage/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func MigrateModStorageDB(db *sql.DB, dbtype types.DatabaseType) error {
value BLOB NOT NULL,
PRIMARY KEY (modname, key)
)`)
case types.DATABASE_POSTGRES:
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS mod_storage (
modname TEXT NOT NULL,
key BYTEA NOT NULL,
value BYTEA NOT NULL,
PRIMARY KEY (modname, key)
)`)
}
return err
}
File renamed without changes.
2 changes: 2 additions & 0 deletions mod_storage/mod_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func NewModStorageRepository(db *sql.DB, dbtype types.DatabaseType) ModStorageRe
switch dbtype {
case types.DATABASE_SQLITE:
return &modStorageSqliteRepository{db: db}
case types.DATABASE_POSTGRES:
return &modStoragePostgresRepository{db: db}
default:
return nil
}
Expand Down
99 changes: 99 additions & 0 deletions mod_storage/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package mod_storage

import (
"archive/zip"
"bufio"
"bytes"
"database/sql"
"encoding/json"
)

type modStoragePostgresRepository struct {
db *sql.DB
}

func (repo *modStoragePostgresRepository) Get(modname string, key []byte) (*ModStorageEntry, error) {
row := repo.db.QueryRow("select modname,key,value from entries where modname = $1 and key = $2", modname, key)
entry := &ModStorageEntry{}
err := row.Scan(&entry.ModName, &entry.Key, &entry.Value)
if err == sql.ErrNoRows {
return nil, nil
}
return entry, err
}

func (repo *modStoragePostgresRepository) Create(entry *ModStorageEntry) error {
_, err := repo.db.Exec("insert into entries(modname,key,value) values($1,$2,$3)", entry.ModName, entry.Key, entry.Value)
return err
}

func (repo *modStoragePostgresRepository) Update(entry *ModStorageEntry) error {
_, err := repo.db.Exec("update entries set value = $1 where modname = $2 and key = $3", entry.Value, entry.ModName, entry.Key)
return err
}

func (repo *modStoragePostgresRepository) Delete(modname string, key []byte) error {
_, err := repo.db.Exec("delete from entries where modname = $1 and key = $2", modname, key)
return err
}

func (repo *modStoragePostgresRepository) Count() (int64, error) {
row := repo.db.QueryRow("select count(*) from entries")
count := int64(0)
err := row.Scan(&count)
return count, err
}

func (repo *modStoragePostgresRepository) Export(z *zip.Writer) error {
w, err := z.Create("mod_storage.json")
if err != nil {
return err
}
enc := json.NewEncoder(w)

rows, err := repo.db.Query("select modname,key,value from entries")
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
e := &ModStorageEntry{}
err = rows.Scan(&e.ModName, &e.Key, &e.Value)
if err != nil {
return err
}

err = enc.Encode(e)
if err != nil {
return err
}
}

return nil
}

func (repo *modStoragePostgresRepository) Import(z *zip.Reader) error {
f, err := z.Open("mod_storage.json")
if err != nil {
return err
}
defer f.Close()

sc := bufio.NewScanner(f)
for sc.Scan() {
dc := json.NewDecoder(bytes.NewReader(sc.Bytes()))
e := &ModStorageEntry{}
err = dc.Decode(e)
if err != nil {
return err
}

_, err := repo.db.Exec("insert into entries(modname,key,value) values($1,$2,$3) on conflict set value = $3", e.ModName, e.Key, e.Value)
if err != nil {
return err
}
}

return nil
}
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions test-env.sh

This file was deleted.

15 changes: 8 additions & 7 deletions worldconfig/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const (
)

const (
CONFIG_PSQL_AUTH_CONNECTION = "pgsql_auth_connection"
CONFIG_AUTH_BACKEND = "auth_backend"
CONFIG_STORAGE_BACKEND = "mod_storage_backend"
CONFIG_PLAYER_BACKEND = "player_backend"
CONFIG_PSQL_PLAYER_CONNECTION = "pgsql_player_connection"
CONFIG_PSQL_MAP_CONNECTION = "pgsql_connection"
CONFIG_MAP_BACKEND = "backend"
CONFIG_MAP_BACKEND = "backend"
CONFIG_AUTH_BACKEND = "auth_backend"
CONFIG_STORAGE_BACKEND = "mod_storage_backend"
CONFIG_PLAYER_BACKEND = "player_backend"
CONFIG_PSQL_PLAYER_CONNECTION = "pgsql_player_connection"
CONFIG_PSQL_MAP_CONNECTION = "pgsql_connection"
CONFIG_PSQL_AUTH_CONNECTION = "pgsql_auth_connection"
CONFIG_PSQL_MOD_STORAGE_CONNECTION = "pgsql_mod_storage_connection"
)

const DEFAULT_CONFIG = `
Expand Down

0 comments on commit ea23b0b

Please sign in to comment.