This commit is contained in:
Mariano Z. 2025-05-03 17:36:17 -03:00
commit 682f25edcd
Signed by: marianozunino
GPG key ID: 4C73BAD25156DACE
19 changed files with 1907 additions and 0 deletions

31
db/db.go Normal file
View file

@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package db
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

12
db/models.go Normal file
View file

@ -0,0 +1,12 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package db
type Config struct {
ApiToken string `json:"api_token"`
ZoneID string `json:"zone_id"`
Domain string `json:"domain"`
UpdatePeriod string `json:"update_period"`
}

32
db/queries.sql Normal file
View file

@ -0,0 +1,32 @@
-- name: GetConfig :one
SELECT * FROM config LIMIT 1;
-- name: UpsertConfig :exec
INSERT INTO config (api_token, zone_id, domain, update_period)
VALUES (?, ?, ?, ?)
ON CONFLICT DO UPDATE SET
api_token = excluded.api_token,
zone_id = excluded.zone_id,
domain = excluded.domain,
update_period = excluded.update_period;
-- name: InitSchema :exec
-- This query is used to ensure the schema is set up properly
CREATE TABLE IF NOT EXISTS config (
api_token TEXT,
zone_id TEXT,
domain TEXT NOT NULL DEFAULT 'mz.uy',
update_period TEXT NOT NULL DEFAULT '0 */6 * * *'
);
CREATE TRIGGER IF NOT EXISTS enforce_single_config
BEFORE INSERT ON config
WHEN (SELECT COUNT(*) FROM config) > 0
BEGIN
SELECT RAISE(FAIL, 'Only one config record allowed');
END;
-- Insert default config if none exists
INSERT OR IGNORE INTO config (domain, update_period)
SELECT 'mz.uy', '0 */6 * * *'
WHERE NOT EXISTS (SELECT 1 FROM config);

68
db/queries.sql.go Normal file
View file

@ -0,0 +1,68 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: queries.sql
package db
import (
"context"
)
const getConfig = `-- name: GetConfig :one
SELECT api_token, zone_id, domain, update_period FROM config LIMIT 1
`
func (q *Queries) GetConfig(ctx context.Context) (Config, error) {
row := q.db.QueryRowContext(ctx, getConfig)
var i Config
err := row.Scan(
&i.ApiToken,
&i.ZoneID,
&i.Domain,
&i.UpdatePeriod,
)
return i, err
}
const initSchema = `-- name: InitSchema :exec
CREATE TABLE IF NOT EXISTS config (
api_token TEXT,
zone_id TEXT,
domain TEXT NOT NULL DEFAULT 'mz.uy',
update_period TEXT NOT NULL DEFAULT '0 */6 * * *'
)
`
// This query is used to ensure the schema is set up properly
func (q *Queries) InitSchema(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initSchema)
return err
}
const upsertConfig = `-- name: UpsertConfig :exec
INSERT INTO config (api_token, zone_id, domain, update_period)
VALUES (?, ?, ?, ?)
ON CONFLICT DO UPDATE SET
api_token = excluded.api_token,
zone_id = excluded.zone_id,
domain = excluded.domain,
update_period = excluded.update_period
`
type UpsertConfigParams struct {
ApiToken string `json:"api_token"`
ZoneID string `json:"zone_id"`
Domain string `json:"domain"`
UpdatePeriod string `json:"update_period"`
}
func (q *Queries) UpsertConfig(ctx context.Context, arg UpsertConfigParams) error {
_, err := q.db.ExecContext(ctx, upsertConfig,
arg.ApiToken,
arg.ZoneID,
arg.Domain,
arg.UpdatePeriod,
)
return err
}

6
db/schema.sql Normal file
View file

@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS config (
api_token TEXT NOT NULL DEFAULT '',
zone_id TEXT NOT NULL DEFAULT '',
domain TEXT NOT NULL DEFAULT 'mz.uy',
update_period TEXT NOT NULL DEFAULT '0 */6 * * *'
);

15
db/util.go Normal file
View file

@ -0,0 +1,15 @@
package db
import (
"context"
"database/sql"
)
// InitSchema initializes the database schema
func InitSchema(db *sql.DB) error {
// Create a new Queries instance
q := New(db)
// Execute the initialization query
return q.InitSchema(context.Background())
}