Compare commits

..

17 Commits

Author SHA1 Message Date
4588932f37 rebase and adittional files change 2025-01-10 13:22:56 +01:00
ac23163088 test language code saving 2025-01-10 13:22:56 +01:00
88cedab3f2 persist selected language code 2025-01-10 13:22:56 +01:00
fa7e7eef57 add key to hold selected langauge code 2025-01-10 13:22:56 +01:00
0f9c2c6fa2 Refactored the code to switch between postgres and gdbm, with db cleanup 2025-01-10 13:22:56 +01:00
801a5dfd5a Replace the connStr if it is not set 2025-01-10 13:22:42 +01:00
lash
fe44fa2bf7 Implement postgres schema 2025-01-10 13:22:42 +01:00
lash
86c5efa3ad Rehabilitate tests 2025-01-10 13:22:42 +01:00
aa1b2bb108 cleanup the generated test data for the schema 2025-01-10 13:21:30 +01:00
7049e02490 pass the dbschema in the context 2025-01-10 13:21:30 +01:00
b0c9197ce4 allow the BuildConnStr to be accessed by different packages 2025-01-10 13:21:30 +01:00
c34fe192c6 use a flag to pass the schema to the context 2025-01-10 13:21:07 +01:00
25c1cc41bb create a schema if it does not exist and use it in the connection 2025-01-10 13:19:28 +01:00
0f6c486ee0 add a db flag to specify the database of choice 2025-01-10 13:19:08 +01:00
66110439a0 pass the base directory to load the .env file 2025-01-10 13:18:29 +01:00
a9c7a40ff3 specify the base directory for loading the .env file 2025-01-10 13:17:23 +01:00
2f634d550b refactor: rename files to snake_case 2025-01-10 11:20:51 +01:00
21 changed files with 44 additions and 43 deletions

View File

@ -50,7 +50,8 @@ func main() {
var langs args.LangVar
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.StringVar(&connStr, "c", "", "connection string")
flag.StringVar(&database, "db", "gdbm", "database to be used")
flag.StringVar(&dbSchema, "schema", "public", "database schema to be used")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")

View File

@ -61,7 +61,8 @@ func main() {
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.StringVar(&connStr, "c", "", "connection string")
flag.StringVar(&database, "db", "gdbm", "database to be used")
flag.StringVar(&dbSchema, "schema", "public", "database schema to be used")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")

View File

@ -49,7 +49,8 @@ func main() {
var langs args.LangVar
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
flag.StringVar(&connStr, "c", "", "connection string")
flag.StringVar(&database, "db", "gdbm", "database to be used")
flag.StringVar(&dbSchema, "schema", "public", "database schema to be used")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host")

View File

@ -44,7 +44,9 @@ func main() {
flag.StringVar(&resourceDir, "resourcedir", scriptDir, "resource dir")
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
flag.StringVar(&connStr, "c", "", "connection string")
flag.StringVar(&database, "db", "gdbm", "database to be used")
flag.StringVar(&dbSchema, "schema", "public", "database schema to be used")
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
flag.BoolVar(&engineDebug, "d", false, "use engine debug output")
flag.UintVar(&size, "s", 160, "max size of output")
flag.StringVar(&gettextDir, "gettext", "", "use gettext translations from given directory")

View File

@ -7,31 +7,22 @@ import (
"os"
"path"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/initializers"
"git.grassecon.net/urdt/ussd/internal/storage"
"git.grassecon.net/urdt/ussd/debug"
"git.defalsify.org/vise.git/db"
"git.defalsify.org/vise.git/logging"
testdataloader "github.com/peteole/testdata-loader"
)
var (
logg = logging.NewVanilla()
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join("services", "registration")
)
func init() {
initializers.LoadEnvVariables()
}
func formatItem(k []byte, v []byte) (string, error) {
o, err := debug.FromKey(k)
if err != nil {
return "", err
}
s := fmt.Sprintf("%vValue: %v\n\n", o, string(v))
return s, nil
initializers.LoadEnvVariables(baseDir)
}
func main() {

View File

@ -36,7 +36,23 @@ type MenuStorageService struct {
userDataStore db.Db
}
func NewMenuStorageService(conn ConnData, resourceDir string) *MenuStorageService {
func BuildConnStr() string {
host := initializers.GetEnv("DB_HOST", "localhost")
user := initializers.GetEnv("DB_USER", "postgres")
password := initializers.GetEnv("DB_PASSWORD", "")
dbName := initializers.GetEnv("DB_NAME", "")
port := initializers.GetEnv("DB_PORT", "5432")
connString := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s",
user, password, host, port, dbName,
)
logg.Debugf("pg conn string", "conn", connString)
return connString
}
func NewMenuStorageService(dbDir string, resourceDir string) *MenuStorageService {
return &MenuStorageService{
conn: conn,
resourceDir: resourceDir,
@ -47,6 +63,11 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D
var newDb db.Db
var err error
schema, ok := ctx.Value("Schema").(string)
if !ok {
return nil, fmt.Errorf("failed to select the schema")
}
if existingDb != nil {
return existingDb, nil
}
@ -64,9 +85,12 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D
} else if dbTyp == DBTYPE_GDBM {
err = ms.ensureDbDir()
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to ensure schema exists: %w", err)
}
connStr = path.Join(connStr, section)
newDb = postgres.NewPgDb().WithSchema(schema)
err = newDb.Connect(ctx, connStr)
} else {
newDb = gdbmstorage.NewThreadGdbmDb()
} else {
return nil, fmt.Errorf("unsupported connection string: '%s'\n", ms.conn.String())
@ -80,28 +104,6 @@ func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.D
return newDb, nil
}
// WithGettext triggers use of gettext for translation of templates and menus.
//
// The first language in `lns` will be used as default language, to resolve node keys to
// language strings.
//
// If `lns` is an empty array, gettext will not be used.
func (ms *MenuStorageService) WithGettext(path string, lns []lang.Language) *MenuStorageService {
if len(lns) == 0 {
logg.Warnf("Gettext requested but no languages supplied")
return ms
}
rs := resource.NewPoResource(lns[0], path)
for _, ln := range(lns) {
rs = rs.WithLanguage(ln)
}
ms.poResource = rs
return ms
}
// ensureSchemaExists creates a new schema if it does not exist
func ensureSchemaExists(ctx context.Context, conn ConnData) error {
h, err := pgxpool.New(ctx, conn.Path())

View File

@ -99,6 +99,9 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
var err error
ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId)
ctx = context.WithValue(ctx, "Database", selectedDatabase)
ctx = context.WithValue(ctx, "Schema", selectedDbSchema)
pfp := path.Join(scriptDir, "pp.csv")
var eventChannel = make(chan bool)