diff --git a/block/block_migrate_test.go b/block/block_migrate_test.go index a133b44..5c74103 100644 --- a/block/block_migrate_test.go +++ b/block/block_migrate_test.go @@ -20,8 +20,7 @@ func TestMigrateBlockSQlite(t *testing.T) { } func TestMigrateBlockPostgres(t *testing.T) { - db, err := getPostgresDB(t) - assert.NoError(t, err) + db := getPostgresDB(t) assert.NoError(t, block.MigrateBlockDB(db, types.DATABASE_POSTGRES)) } diff --git a/block/block_postgres_test.go b/block/block_postgres_test.go index d5028c5..a3d8668 100644 --- a/block/block_postgres_test.go +++ b/block/block_postgres_test.go @@ -10,8 +10,7 @@ import ( ) func setupPostgress(t *testing.T) (block.BlockRepository, *sql.DB) { - db, err := getPostgresDB(t) - assert.NoError(t, err) + db := getPostgresDB(t) // Cleanup any previous data db.Exec("delete from blocks") diff --git a/block/util_test.go b/block/util_test.go index 78305cd..6995f8b 100644 --- a/block/util_test.go +++ b/block/util_test.go @@ -8,9 +8,10 @@ import ( _ "github.com/lib/pq" "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" ) -func getPostgresDB(t *testing.T) (*sql.DB, error) { +func getPostgresDB(t *testing.T) *sql.DB { if os.Getenv("PGHOST") == "" { t.SkipNow() } @@ -23,7 +24,9 @@ func getPostgresDB(t *testing.T) (*sql.DB, error) { os.Getenv("PGHOST"), os.Getenv("PGDATABASE")) - return sql.Open("postgres", connStr) + db, err := sql.Open("postgres", connStr) + assert.NoError(t, err) + return db } type testingLogWriter struct { diff --git a/docker-compose.yml b/docker-compose.yml index 6827bb1..89507b2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,25 +8,28 @@ services: POSTGRES_PASSWORD: enter volumes: - "postgres:/var/lib/postgresql/data" - command: -c port=9432 integration-test: image: golang:1.21.4 depends_on: - postgres volumes: + - "go_dir:/go" + - "go_cache:/.cache" - ".:/data" environment: - PGUSER=postgres - PGPASSWORD=enter - PGHOST=postgres + - PGPORT=5432 - PGDATABASE=postgres - - PGPORT=9432 - LOGLEVEL=debug - CGO_ENABLED=0 - GO_FLAGS=-count=1 working_dir: /data - command: ["go", "test", "./...", "-v", "-cover"] + command: ["go", "test", "./...", "-cover"] volumes: postgres: {} + go_cache: {} + go_dir: {} \ No newline at end of file diff --git a/mod_storage/migrate_test.go b/mod_storage/migrate_test.go index d32d5a1..7b62ae6 100644 --- a/mod_storage/migrate_test.go +++ b/mod_storage/migrate_test.go @@ -18,3 +18,8 @@ func TestMigrateModStorageSQlite(t *testing.T) { assert.NoError(t, mod_storage.MigrateModStorageDB(db, types.DATABASE_SQLITE)) } + +func TestMigrateModStoragePostgres(t *testing.T) { + db := getPostgresDB(t) + assert.NoError(t, mod_storage.MigrateModStorageDB(db, types.DATABASE_POSTGRES)) +} diff --git a/mod_storage/postgres.go b/mod_storage/postgres.go index 1d339bb..06bf578 100644 --- a/mod_storage/postgres.go +++ b/mod_storage/postgres.go @@ -13,7 +13,7 @@ type modStoragePostgresRepository struct { } 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) + row := repo.db.QueryRow("select modname,key,value from mod_storage where modname = $1 and key = $2", modname, key) entry := &ModStorageEntry{} err := row.Scan(&entry.ModName, &entry.Key, &entry.Value) if err == sql.ErrNoRows { @@ -23,22 +23,22 @@ func (repo *modStoragePostgresRepository) Get(modname string, key []byte) (*ModS } 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) + _, err := repo.db.Exec("insert into mod_storage(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) + _, err := repo.db.Exec("update mod_storage 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) + _, err := repo.db.Exec("delete from mod_storage where modname = $1 and key = $2", modname, key) return err } func (repo *modStoragePostgresRepository) Count() (int64, error) { - row := repo.db.QueryRow("select count(*) from entries") + row := repo.db.QueryRow("select count(*) from mod_storage") count := int64(0) err := row.Scan(&count) return count, err @@ -51,7 +51,7 @@ func (repo *modStoragePostgresRepository) Export(z *zip.Writer) error { } enc := json.NewEncoder(w) - rows, err := repo.db.Query("select modname,key,value from entries") + rows, err := repo.db.Query("select modname,key,value from mod_storage") if err != nil { return err } @@ -89,7 +89,7 @@ func (repo *modStoragePostgresRepository) Import(z *zip.Reader) error { 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) + _, err := repo.db.Exec("insert into mod_storage(modname,key,value) values($1,$2,$3) on conflict set value = $3", e.ModName, e.Key, e.Value) if err != nil { return err } diff --git a/mod_storage/postgres_test.go b/mod_storage/postgres_test.go new file mode 100644 index 0000000..6b22eb7 --- /dev/null +++ b/mod_storage/postgres_test.go @@ -0,0 +1,39 @@ +package mod_storage_test + +import ( + "testing" + + "github.com/minetest-go/mtdb/mod_storage" + "github.com/minetest-go/mtdb/types" + "github.com/stretchr/testify/assert" +) + +func TestModStoragePostgresRepo(t *testing.T) { + // open db + db := getPostgresDB(t) + repo := mod_storage.NewModStorageRepository(db, types.DATABASE_POSTGRES) + assert.NotNil(t, repo) + + // cleanup + _, err := db.Exec("delete from mod_storage") + assert.NoError(t, err) + + // create + entry := &mod_storage.ModStorageEntry{ + ModName: "mymod", + Key: []byte("mykey"), + Value: []byte("myvalue"), + } + assert.NoError(t, repo.Create(entry)) + + // count + entry_count, err := repo.Count() + assert.NoError(t, err) + assert.Equal(t, int64(1), entry_count) + + // delete + assert.NoError(t, repo.Delete("mymod", []byte("mykey"))) + entry, err = repo.Get("mymod", []byte("mykey")) + assert.NoError(t, err) + assert.Nil(t, entry) +} diff --git a/mod_storage/util_test.go b/mod_storage/util_test.go index 044b499..5ee7816 100644 --- a/mod_storage/util_test.go +++ b/mod_storage/util_test.go @@ -1,12 +1,34 @@ package mod_storage_test import ( + "database/sql" + "fmt" "io" "os" + "testing" _ "github.com/lib/pq" + "github.com/stretchr/testify/assert" ) +func getPostgresDB(t *testing.T) *sql.DB { + if os.Getenv("PGHOST") == "" { + t.SkipNow() + } + + connStr := fmt.Sprintf( + "user=%s password=%s port=%s host=%s dbname=%s sslmode=disable", + os.Getenv("PGUSER"), + os.Getenv("PGPASSWORD"), + os.Getenv("PGPORT"), + os.Getenv("PGHOST"), + os.Getenv("PGDATABASE")) + + db, err := sql.Open("postgres", connStr) + assert.NoError(t, err) + return db +} + func copyFileContents(src, dst string) (err error) { in, err := os.Open(src) if err != nil { diff --git a/worldconfig/parse.go b/worldconfig/parse.go index 9b4b852..ec4258f 100644 --- a/worldconfig/parse.go +++ b/worldconfig/parse.go @@ -17,6 +17,7 @@ const ( CONFIG_AUTH_BACKEND = "auth_backend" CONFIG_STORAGE_BACKEND = "mod_storage_backend" CONFIG_PLAYER_BACKEND = "player_backend" + CONFIG_MOD_STORAGE_BACKEND = "mod_storage_backend" CONFIG_PSQL_PLAYER_CONNECTION = "pgsql_player_connection" CONFIG_PSQL_MAP_CONNECTION = "pgsql_connection" CONFIG_PSQL_AUTH_CONNECTION = "pgsql_auth_connection" diff --git a/worldconfig/parse_test.go b/worldconfig/parse_test.go index f25ccb6..0b1df88 100644 --- a/worldconfig/parse_test.go +++ b/worldconfig/parse_test.go @@ -1,7 +1,6 @@ package worldconfig_test import ( - "fmt" "testing" "github.com/minetest-go/mtdb/worldconfig" @@ -17,20 +16,13 @@ func TestInvalidFile(t *testing.T) { func TestParseSqlite(t *testing.T) { cfg, err := worldconfig.Parse("./testdata/world.mt.sqlite") assert.NoError(t, err) - if cfg[worldconfig.CONFIG_AUTH_BACKEND] != worldconfig.BACKEND_SQLITE3 { - t.Fatal("not sqlite3") - } + assert.Equal(t, worldconfig.BACKEND_SQLITE3, cfg[worldconfig.CONFIG_AUTH_BACKEND]) } func TestParsePostgres(t *testing.T) { cfg, err := worldconfig.Parse("./testdata/world.mt.postgres") assert.NoError(t, err) - fmt.Println(cfg) - if cfg[worldconfig.CONFIG_AUTH_BACKEND] != worldconfig.BACKEND_POSTGRES { - t.Fatal("not postgres") - } - - if cfg[worldconfig.CONFIG_PSQL_AUTH_CONNECTION] != "host=/var/run/postgresql user=postgres password=enter dbname=postgres" { - t.Fatal("param err") - } + assert.Equal(t, worldconfig.BACKEND_POSTGRES, cfg[worldconfig.CONFIG_AUTH_BACKEND]) + assert.Equal(t, "host=/var/run/postgresql user=postgres password=enter dbname=postgres", cfg[worldconfig.CONFIG_PSQL_AUTH_CONNECTION]) + assert.Equal(t, "host=postgres port=5432 user=postgres password=enter dbname=postgres", cfg[worldconfig.CONFIG_PSQL_MOD_STORAGE_CONNECTION]) } diff --git a/worldconfig/testdata/world.mt.postgres b/worldconfig/testdata/world.mt.postgres index f2204c6..8de98e1 100644 --- a/worldconfig/testdata/world.mt.postgres +++ b/worldconfig/testdata/world.mt.postgres @@ -1,9 +1,11 @@ gameid = minetest backend = postgresql auth_backend = postgresql +mod_storage_backend = postgresql creative_mode = false enable_damage = true player_backend = postgresql pgsql_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres pgsql_player_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres pgsql_auth_connection = host=/var/run/postgresql user=postgres password=enter dbname=postgres +pgsql_mod_storage_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres