Skip to content

Commit be38c83

Browse files
db cleanup (#341)
* db cleanup * dev * pg setup * postgres uuid migration * cleanup * fk * sqlite migration * cleanup * wal * max open conns = 1 * fix panic --------- Co-authored-by: BuckarooBanzay <[email protected]>
1 parent 4583880 commit be38c83

38 files changed

+1000425
-7113
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Set up Go
2121
uses: actions/[email protected]
2222
with:
23-
go-version: 1.18
23+
go-version: "1.21"
2424

2525
- name: Set up nodejs
2626
uses: actions/setup-node@v3

.github/workflows/go-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v3
1212
- uses: actions/[email protected]
1313
with:
14-
go-version: '1.18'
14+
go-version: '1.21'
1515

1616
- name: test
1717
run: go test ./... -coverprofile=profile.cov

db/postgres/initialblocks.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const (
1616
SETTING_LAST_Y_BLOCK = "last_y_block"
1717
)
1818

19-
func (this *PostgresAccessor) countBlocks(x1, y1, z1, x2, y2, z2 int) (int, error) {
20-
rows, err := this.db.Query(getBlockCountByInitialTileQuery,
19+
func (a *PostgresAccessor) countBlocks(x1, y1, z1, x2, y2, z2 int) (int, error) {
20+
rows, err := a.db.Query(getBlockCountByInitialTileQuery,
2121
x1, y1, z1, x2, y2, z2,
2222
)
2323

@@ -27,7 +27,7 @@ func (this *PostgresAccessor) countBlocks(x1, y1, z1, x2, y2, z2 int) (int, erro
2727

2828
defer rows.Close()
2929

30-
for rows.Next() {
30+
if rows.Next() {
3131
var count int
3232

3333
err = rows.Scan(&count)
@@ -53,7 +53,7 @@ func (this *PostgresAccessor) countBlocks(x1, y1, z1, x2, y2, z2 int) (int, erro
5353
//Zoom 9:
5454
//10 mapblocks height * 16 * 16 == 2560
5555

56-
func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers []*layer.Layer, limit int) (*db.InitialBlocksResult, error) {
56+
func (a *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers []*layer.Layer, limit int) (*db.InitialBlocksResult, error) {
5757

5858
lastlayer := s.GetInt(SETTING_LAST_LAYER, 0)
5959
lastxblock := s.GetInt(SETTING_LAST_X_BLOCK, -129)
@@ -111,7 +111,7 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers
111111

112112
if lastxblock <= -128 {
113113
//first x entry, check z stride
114-
stridecount := this.intQuery(`
114+
stridecount := a.intQuery(`
115115
select count(*) from blocks
116116
where posz >= $1 and posz <= $2
117117
and posy >= $3 and posy <= $4`,
@@ -139,7 +139,7 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers
139139
}
140140
}
141141

142-
count, err := this.countBlocks(minX, minY, minZ, maxX, maxY, maxZ)
142+
count, err := a.countBlocks(minX, minY, minZ, maxX, maxY, maxZ)
143143

144144
if err != nil {
145145
return nil, err
@@ -150,7 +150,7 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers
150150

151151
if count > 0 {
152152

153-
rows, err := this.db.Query(getBlocksByInitialTileQuery,
153+
rows, err := a.db.Query(getBlocksByInitialTileQuery,
154154
minX, minY, minZ, maxX, maxY, maxZ,
155155
)
156156

db/postgres/postgres.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package postgres
22

33
import (
44
"database/sql"
5+
"embed"
56
"mapserver/coords"
67
"mapserver/db"
7-
"mapserver/public"
88
"time"
99

1010
_ "github.com/lib/pq"
@@ -15,6 +15,9 @@ type PostgresAccessor struct {
1515
db *sql.DB
1616
}
1717

18+
//go:embed migrations/*.sql
19+
var migrations embed.FS
20+
1821
func (db *PostgresAccessor) Migrate() error {
1922
hasMtime := true
2023
_, err := db.db.Query("select max(mtime) from blocks")
@@ -26,7 +29,7 @@ func (db *PostgresAccessor) Migrate() error {
2629
log.Info("Migrating database, this might take a while depending on the mapblock-count")
2730
start := time.Now()
2831

29-
sql, err := public.Files.ReadFile("sql/postgres_mapdb_migrate.sql")
32+
sql, err := migrations.ReadFile("migrations/postgres_mapdb_migrate.sql")
3033
if err != nil {
3134
return err
3235
}
@@ -48,10 +51,10 @@ func convertRows(posx, posy, posz int, data []byte, mtime int64) *db.Block {
4851
return &db.Block{Pos: c, Data: data, Mtime: mtime}
4952
}
5053

51-
func (this *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) {
54+
func (a *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) {
5255
blocks := make([]*db.Block, 0)
5356

54-
rows, err := this.db.Query(getBlocksByMtimeQuery, gtmtime, limit)
57+
rows, err := a.db.Query(getBlocksByMtimeQuery, gtmtime, limit)
5558
if err != nil {
5659
return nil, err
5760
}
@@ -75,8 +78,8 @@ func (this *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db
7578
return blocks, nil
7679
}
7780

78-
func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) {
79-
rows, err := this.db.Query(countBlocksQuery, frommtime, tomtime)
81+
func (a *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) {
82+
rows, err := a.db.Query(countBlocksQuery, frommtime, tomtime)
8083
if err != nil {
8184
panic(err)
8285
}
@@ -97,8 +100,8 @@ func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error)
97100
return 0, nil
98101
}
99102

100-
func (db *PostgresAccessor) GetTimestamp() (int64, error) {
101-
rows, err := db.db.Query(getTimestampQuery)
103+
func (a *PostgresAccessor) GetTimestamp() (int64, error) {
104+
rows, err := a.db.Query(getTimestampQuery)
102105
if err != nil {
103106
return 0, err
104107
}
@@ -119,8 +122,8 @@ func (db *PostgresAccessor) GetTimestamp() (int64, error) {
119122
return 0, nil
120123
}
121124

122-
func (this *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) {
123-
rows, err := this.db.Query(getBlockQuery, pos.X, pos.Y, pos.Z)
125+
func (a *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) {
126+
rows, err := a.db.Query(getBlockQuery, pos.X, pos.Y, pos.Z)
124127
if err != nil {
125128
return nil, err
126129
}

db/postgres/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package postgres
22

3-
func (this *PostgresAccessor) intQuery(q string, params ...interface{}) int {
4-
rows, err := this.db.Query(q, params...)
3+
func (a *PostgresAccessor) intQuery(q string, params ...interface{}) int {
4+
rows, err := a.db.Query(q, params...)
55
if err != nil {
66
panic(err)
77
}

db/sqlite/initialblocks.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"mapserver/db"
66
"mapserver/layer"
77
"mapserver/settings"
8-
9-
_ "modernc.org/sqlite"
108
)
119

1210
const (
@@ -23,7 +21,7 @@ order by b.pos asc, b.mtime asc
2321
limit ?
2422
`
2523

26-
func (this *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers []*layer.Layer, limit int) (*db.InitialBlocksResult, error) {
24+
func (a *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers []*layer.Layer, limit int) (*db.InitialBlocksResult, error) {
2725
result := &db.InitialBlocksResult{}
2826

2927
blocks := make([]*db.Block, 0)
@@ -33,7 +31,7 @@ func (this *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers [
3331
totallegacycount := s.GetInt64(SETTING_TOTAL_LEGACY_COUNT, -1)
3432
if totallegacycount == -1 {
3533
//Query from db
36-
totallegacycount, err := this.CountBlocks()
34+
totallegacycount, err := a.CountBlocks()
3735

3836
if err != nil {
3937
panic(err)
@@ -42,7 +40,7 @@ func (this *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers [
4240
s.SetInt64(SETTING_TOTAL_LEGACY_COUNT, int64(totallegacycount))
4341
}
4442

45-
rows, err := this.db.Query(getLastBlockQuery, lastpos, limit)
43+
rows, err := a.db.Query(getLastBlockQuery, lastpos, limit)
4644
if err != nil {
4745
return nil, err
4846
}

db/sqlite/sqlite.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package sqlite
22

33
import (
44
"database/sql"
5+
"embed"
56
"errors"
67
"mapserver/coords"
78
"mapserver/db"
8-
"mapserver/public"
99
"time"
1010

1111
"github.com/sirupsen/logrus"
@@ -22,6 +22,9 @@ type Sqlite3Accessor struct {
2222
filename string
2323
}
2424

25+
//go:embed migrations/*.sql
26+
var migrations embed.FS
27+
2528
func (db *Sqlite3Accessor) Migrate() error {
2629

2730
//RW connection
@@ -44,7 +47,7 @@ func (db *Sqlite3Accessor) Migrate() error {
4447
}).Info("Migrating database, this might take a while depending on the mapblock-count")
4548
start := time.Now()
4649

47-
sql, err := public.Files.ReadFile("sql/sqlite_mapdb_migrate.sql")
50+
sql, err := migrations.ReadFile("migrations/sqlite_mapdb_migrate.sql")
4851
if err != nil {
4952
return err
5053
}

dev/world.postgres.mt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
gameid = minetest
2+
creative_mode = true
3+
enable_damage = true
4+
5+
auth_backend = postgresql
6+
pgsql_auth_connection = host=postgres user=postgres password=enter dbname=postgres
7+
8+
player_backend = postgresql
9+
pgsql_player_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres
10+
11+
backend = postgresql
12+
pgsql_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres
13+
14+
mod_storage_backend = postgresql
15+
pgsql_mod_storage_connection = host=postgres user=postgres password=enter dbname=postgres
16+
17+
pgsql_mapserver_connection = host=postgres port=5432 user=postgres password=enter dbname=postgres

doc/Overview.png

-13.4 KB
Binary file not shown.

doc/Overview.xml

Lines changed: 0 additions & 1 deletion
This file was deleted.

doc/dev.md

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,37 @@
11

2-
# System overview
3-
4-
<img src="./Overview.png">
5-
62
# Build dependencies
73

8-
* go >= 1.16
9-
* nodejs >= v17.4.0
10-
* npm >= 8.3.0
4+
* docker
5+
* docker-compose
116

127
# Create the frontend bundle
138

149
```bash
15-
cd public
16-
npm ci
17-
npm run bundle
10+
docker-compose up mapserver_frontend
1811
```
1912

20-
# Development setup
21-
22-
Working directory: `./server`
23-
24-
## Preparing the files and map
25-
26-
Copy your `map.sqlite` into the working directory if you want to test with
27-
a sqlite map database
28-
29-
### world.mt
30-
31-
You need a `world.mt` too in order to make the connection to the database.
32-
In the sqlite case:
13+
# Development setup (sqlite)
3314

34-
```
35-
gameid = minetest
36-
backend = sqlite3
37-
creative_mode = false
38-
enable_damage = false
39-
player_backend = files
15+
```bash
16+
# start the engine in the first window/shell
17+
docker-compose up minetest
18+
# and the mapserver in another
19+
docker-compose up mapserver
4020
```
4121

42-
For postgres:
43-
```
44-
gameid = minetest
45-
backend = postgresql
46-
creative_mode = true
47-
enable_damage = true
48-
player_backend = postgresql
49-
pgsql_connection = host=localhost port=5432 user=postgres password=enter dbname=postgres
50-
pgsql_player_connection = host=localhost port=5432 user=postgres password=enter dbname=postgres
51-
```
22+
# Development setup (postgres)
5223

53-
## Running the server
24+
```bash
25+
# start postgres in the background
26+
docker-compose -f docker-compose.yml -f docker-compose.postgres.yml up -d postgres
27+
# start the engine in the first window/shell
28+
docker-compose -f docker-compose.yml -f docker-compose.postgres.yml up minetest
29+
# and the mapserver in another
30+
docker-compose -f docker-compose.yml -f docker-compose.postgres.yml up mapserver
31+
```
5432

55-
* Create a `mapserver.json` with `go run . -createconfig`
56-
* Change the value `webdev` in the `mapserver.json` to `true`
57-
* Start the server with `go run .` or with debug output: `go run . -debug`
58-
* The web files in `public/` can now be changed on the fly without restarting the server
33+
Utilities:
34+
```sh
35+
# psql
36+
docker-compose -f docker-compose.yml -f docker-compose.postgres.yml exec postgres psql -U postgres
37+
```

docker-compose.postgres.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: "3.6"
2+
3+
services:
4+
postgres:
5+
image: postgres:16.1
6+
restart: always
7+
environment:
8+
POSTGRES_PASSWORD: enter
9+
volumes:
10+
- "pg_data:/var/lib/postgresql/data"
11+
12+
mapserver:
13+
volumes:
14+
- "./dev/world.postgres.mt:/data/world/world.mt"
15+
16+
minetest:
17+
volumes:
18+
- "./dev/world.postgres.mt:/root/.minetest/worlds/world/world.mt"
19+
20+
volumes:
21+
pg_data: {}

docker-compose.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ version: "3.6"
22

33
services:
44
mapserver_frontend:
5-
image: node:alpine3.13
5+
image: node:21.5.0-alpine
66
volumes:
77
- "dev_home:/root"
88
- ".:/data"
99
working_dir: /data/public
10-
command: ["npm", "i"]
10+
command: ["npm", "ci"]
1111

1212
mapserver:
13-
image: golang:1.20.3
13+
image: golang:1.21.5
1414
depends_on:
1515
- minetest
1616
- mapserver_frontend
1717
volumes:
1818
- "dev_home:/root"
1919
- "world_data:/data/world"
20+
- "go_dir:/go"
21+
- "go_cache:/.cache"
2022
- ".:/data"
2123
- "./public:/data/world/public"
2224
- "./dev/mapserver.json:/data/world/mapserver.json"
@@ -26,7 +28,7 @@ services:
2628
command: ["go", "run", ".."]
2729

2830
minetest:
29-
image: registry.gitlab.com/minetest/minetest/server:5.6.1
31+
image: registry.gitlab.com/minetest/minetest/server:5.7.0
3032
user: root
3133
volumes:
3234
- "world_data:/root/.minetest/worlds/world"
@@ -38,4 +40,6 @@ services:
3840

3941
volumes:
4042
world_data: {}
41-
dev_home: {}
43+
dev_home: {}
44+
go_dir: {}
45+
go_cache: {}

0 commit comments

Comments
 (0)