Skip to content

Commit 3bf8bdb

Browse files
knadh13unk0wn
andauthored
Split queries.sql into multiple files for better readability and maintainability. Closes #2738. (#2776)
Co-authored-by: 13unk0wn <n0b0dy0.000729@gmail.com>
1 parent 8170489 commit 3bf8bdb

File tree

13 files changed

+1402
-1386
lines changed

13 files changed

+1402
-1386
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ FRONTEND_EMAIL_BUILDER_DEPS = \
3333

3434
BIN := listmonk
3535
STATIC := config.toml.sample \
36-
schema.sql queries.sql permissions.json \
36+
schema.sql queries:/queries permissions.json \
3737
static/public:/public \
3838
static/email-templates \
3939
frontend/dist:/admin \
4040
i18n:/i18n
4141

42+
SQL := $(shell find . -type f -name "*.sql") $(shell find queries -type f -name "*.sql")
43+
SRC := $(shell find . -type f -name "*.go")
44+
4245
.PHONY: build
4346
build: $(BIN)
4447

@@ -54,7 +57,7 @@ $(FRONTEND_EMAIL_BUILDER_YARN_MODULES): frontend/package.json frontend/yarn.lock
5457
touch -c $(FRONTEND_EMAIL_BUILDER_YARN_MODULES)
5558

5659
# Build the backend to ./listmonk.
57-
$(BIN): $(shell find . -type f -name "*.go") go.mod go.sum schema.sql queries.sql permissions.json
60+
$(BIN): $(SRC) go.mod go.sum schema.sql $(SQL) permissions.json
5861
CGO_ENABLED=0 go build -o ${BIN} -ldflags="-s -w -X 'main.buildString=${BUILDSTR}' -X 'main.versionString=${VERSION}'" cmd/*.go
5962

6063
# Run the backend in dev mode. The frontend assets in dev mode are loaded from disk from frontend/dist.

cmd/init.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ import (
5454
)
5555

5656
const (
57-
queryFilePath = "queries.sql"
58-
emailMsgr = "email"
57+
// Path to the SQL queries directory in the embedded FS.
58+
queryFilePath = "/queries"
59+
60+
emailMsgr = "email"
5961
)
6062

6163
// UrlConfig contains various URL constants used in the app.
@@ -199,7 +201,7 @@ func initFS(appDir, frontendDir, staticDir, i18nDir string) stuffbin.FileSystem
199201
// These paths are joined with appDir.
200202
appFiles = []string{
201203
"./config.toml.sample:config.toml.sample",
202-
"./queries.sql:queries.sql",
204+
"./queries:queries",
203205
"./schema.sql:schema.sql",
204206
"./permissions.json:permissions.json",
205207
}
@@ -333,19 +335,35 @@ func initDB() *sqlx.DB {
333335
return db.Unsafe()
334336
}
335337

336-
// readQueries reads named SQL queries from the SQL queries file into a query map.
337-
func readQueries(sqlFile string, fs stuffbin.FileSystem) goyesql.Queries {
338-
// Load SQL queries.
339-
qB, err := fs.Read(sqlFile)
338+
func readQueries(dir string, fs stuffbin.FileSystem) goyesql.Queries {
339+
out := goyesql.Queries{}
340+
341+
// Glob all the .sql files in the queries directory.
342+
qPath := path.Join(dir, "/*.sql")
343+
files, err := fs.Glob(qPath)
340344
if err != nil {
341-
lo.Fatalf("error reading SQL file %s: %v", sqlFile, err)
345+
lo.Fatalf("error reading *.sql query files from %s: %v", qPath, err)
342346
}
343-
qMap, err := goyesql.ParseBytes(qB)
344-
if err != nil {
345-
lo.Fatalf("error parsing SQL queries: %v", err)
347+
348+
// Read and merge queries from all files into one map.
349+
for _, file := range files {
350+
// Read the SQL file.
351+
b, err := fs.Read(file)
352+
if err != nil {
353+
lo.Fatalf("error reading SQL file %s: %v", file, err)
354+
}
355+
356+
// Parse queries in it into a map.
357+
mp, err := goyesql.ParseBytes(b)
358+
if err != nil {
359+
lo.Fatalf("error parsing SQL queries: %v", err)
360+
}
361+
362+
// Merge into the main query map.
363+
maps.Copy(out, mp)
346364
}
347365

348-
return qMap
366+
return out
349367
}
350368

351369
// prepareQueries queries prepares a query map and returns a *Queries

0 commit comments

Comments
 (0)