Skip to content

Commit

Permalink
remove/cleanup crude import/export code
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Nov 3, 2024
1 parent 0d29ddf commit a1844ba
Show file tree
Hide file tree
Showing 19 changed files with 1 addition and 748 deletions.
58 changes: 0 additions & 58 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package auth

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

Expand Down Expand Up @@ -153,57 +149,3 @@ func (repo *AuthRepository) DeleteAll() error {
_, err := repo.db.Exec("delete from auth")
return err
}

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

rows, err := repo.db.Query("select id,name,password,last_login from auth")
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
e := &AuthEntry{}
err = rows.Scan(&e.ID, &e.Name, &e.Password, &e.LastLogin)
if err != nil {
return err
}

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

return nil
}

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

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

_, err := repo.db.Exec("insert into auth(id,name,password,last_login) values($1,$2,$3,$4)", e.ID, e.Name, e.Password, e.LastLogin)
if err != nil {
return err
}
}

return nil
}
43 changes: 0 additions & 43 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package auth_test

import (
"archive/zip"
"bytes"
"os"
"testing"

"github.com/minetest-go/mtdb/auth"
Expand Down Expand Up @@ -113,23 +110,6 @@ func testAuthRepository(t *testing.T, auth_repo *auth.AuthRepository, priv_repo
assert.NoError(t, err)
assert.Equal(t, 1, len(privs))

// export
buf := bytes.NewBuffer([]byte{})
w := zip.NewWriter(buf)
err = auth_repo.Export(w)
assert.NoError(t, err)
err = priv_repo.Export(w)
assert.NoError(t, err)
err = w.Close()
assert.NoError(t, err)
zipfile, err := os.CreateTemp(os.TempDir(), "auth.zip")
assert.NoError(t, err)
f, err := os.Create(zipfile.Name())
assert.NoError(t, err)
count, err = f.Write(buf.Bytes())
assert.NoError(t, err)
assert.True(t, count > 0)

// delete priv
assert.NoError(t, priv_repo.Delete(*e.ID, "interact"))

Expand All @@ -138,27 +118,4 @@ func testAuthRepository(t *testing.T, auth_repo *auth.AuthRepository, priv_repo

// delete all
assert.NoError(t, auth_repo.DeleteAll())

// import
z, err := zip.OpenReader(zipfile.Name())
assert.NoError(t, err)
err = auth_repo.Import(&z.Reader)
assert.NoError(t, err)
err = priv_repo.Import(&z.Reader)
assert.NoError(t, err)

// check import
e, err = auth_repo.GetByUsername("test")
assert.NoError(t, err)
assert.NotNil(t, e)
assert.Equal(t, "test", e.Name)
assert.Equal(t, "blah", e.Password)
assert.Equal(t, 456, e.LastLogin)
assert.NotNil(t, e.ID)

// check imported priv
privs, err = priv_repo.GetByID(*e.ID)
assert.NoError(t, err)
assert.Equal(t, 1, len(privs))

}
58 changes: 0 additions & 58 deletions auth/user_privileges.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package auth

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

"github.com/minetest-go/mtdb/types"
)
Expand Down Expand Up @@ -50,57 +46,3 @@ func (repo *PrivRepository) Delete(id int64, privilege string) error {
_, err := repo.db.Exec("delete from user_privileges where id = $1 and privilege = $2", id, privilege)
return err
}

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

rows, err := repo.db.Query("select id,privilege from user_privileges")
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
e := &PrivilegeEntry{}
err = rows.Scan(&e.ID, &e.Privilege)
if err != nil {
return err
}

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

return nil
}

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

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

_, err := repo.db.Exec("insert into user_privileges(id,privilege) values($1,$2)", e.ID, e.Privilege)
if err != nil {
return err
}
}

return nil
}
2 changes: 0 additions & 2 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func (b *Block) String() string {
// All positions are in mapblock coordinates, as described here:
// https://github.com/minetest/minetest/blob/master/doc/lua_api.md#mapblock-coordinates
type BlockRepository interface {
types.Backup

// GetByPost returns the map block at positions X,Y,Z.
GetByPos(x, y, z int) (*Block, error)

Expand Down
59 changes: 0 additions & 59 deletions block/block_postgres.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package block

import (
"archive/zip"
"bufio"
"bytes"
"database/sql"

"encoding/json"

"github.com/minetest-go/mtdb/types"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -141,60 +136,6 @@ func (repo *postgresBlockRepository) Count() (int64, error) {
return count, err
}

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

rows, err := r.db.Query("select posX,posY,posZ,data from blocks")
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
block := &Block{}
err = rows.Scan(&block.PosX, &block.PosY, &block.PosZ, &block.Data)
if err != nil {
return err
}

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

return nil
}

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

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

err = r.Update(e)
if err != nil {
return err
}
}

return nil
}

func (r *postgresBlockRepository) Close() error {
return r.db.Close()
}
64 changes: 0 additions & 64 deletions block/block_sqlite.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package block

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

"github.com/minetest-go/mtdb/types"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -154,66 +150,6 @@ func (repo *sqliteBlockRepository) Count() (int64, error) {
return count, err
}

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

rows, err := r.db.Query("select pos from blocks")
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
var pos int64
err = rows.Scan(&pos)
if err != nil {
return err
}

x, y, z := PlainToCoord(pos)
block, err := r.GetByPos(x, y, z)
if err != nil {
return err
}

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

return nil
}

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

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

err = r.Update(e)
if err != nil {
return err
}
}

return nil
}

func (r *sqliteBlockRepository) Close() error {
return r.db.Close()
}
Loading

0 comments on commit a1844ba

Please sign in to comment.