working commit

This commit is contained in:
2026-06-09 14:10:53 +02:00
parent ee30858d11
commit 6e92720e69
26 changed files with 339 additions and 165 deletions
-29
View File
@@ -10,34 +10,6 @@ import (
_ "github.com/mattn/go-sqlite3"
)
const schema = `
--- DROP TABLE IF EXISTS account;
CREATE TABLE IF NOT EXISTS account (
id TEXT NOT NULL,
username TEXT NOT NULL,
passhash TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
disabled BOOL
);
CREATE UNIQUE INDEX IF NOT EXISTS account_index01
ON account(id);
CREATE UNIQUE INDEX IF NOT EXISTS account_index02
ON account(username);
--- DROP TABLE IF EXISTS grant;
CREATE TABLE IF NOT EXISTS grant (
id TEXT NOT NULL,
account_id TEXT NOT NULL,
operation TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS grant_index01
ON grant(account_id);
CREATE UNIQUE INDEX IF NOT EXISTS grant_index02
ON grant(account_id, operation);
`
type Database struct {
datapath string
db *sqlx.DB
@@ -61,7 +33,6 @@ func (db *Database) OpenDatabase() error {
if err != nil {
return err
}
err = db.db.Ping()
if err != nil {
return err
+8 -8
View File
@@ -8,9 +8,9 @@ import (
func (db *Database) InsertGrant(ctx context.Context, grant *descr.Grant) error {
var err error
request := `INSERT INTO grant(id, account_id, operation, created_at)
request := `INSERT INTO grant(id, account_id, grantname, created_at)
VALUES ($1, $2, $3, $4)`
_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Operation, grant.CreatedAt)
_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Grantname, grant.CreatedAt)
if err != nil {
return err
}
@@ -39,12 +39,12 @@ func (db *Database) ListGrants(ctx context.Context) ([]descr.Grant, error) {
return res, err
}
func (db *Database) GetGrant(ctx context.Context, accountID, operation string) (bool, *descr.Grant, error) {
func (db *Database) GetGrant(ctx context.Context, accountID, grantname string) (bool, *descr.Grant, error) {
var err error
res := &descr.Grant{}
request := `SELECT * FROM grant WHERE account_id = $1 AND operation = $2 LIMIT 1`
request := `SELECT * FROM grant WHERE account_id = $1 AND grantname = $2 LIMIT 1`
dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, accountID, operation)
err = db.db.Select(&dbRes, request, accountID, grantname)
if err != nil {
return false, res, err
}
@@ -56,10 +56,10 @@ func (db *Database) GetGrant(ctx context.Context, accountID, operation string) (
return true, res, err
}
func (db *Database) DeleteGrantByAccountID(ctx context.Context, grantID, operation string) error {
func (db *Database) DeleteGrantByAccountID(ctx context.Context, grantID, grantname string) error {
var err error
request := `DELETE FROM grant WHERE account_id = $1 AND operation = $2`
_, err = db.db.Exec(request, grantID, operation)
request := `DELETE FROM grant WHERE account_id = $1 AND grantname = $2`
_, err = db.db.Exec(request, grantID, grantname)
if err != nil {
return err
}
+29
View File
@@ -0,0 +1,29 @@
package maindb
const schema = `
--- DROP TABLE IF EXISTS account;
CREATE TABLE IF NOT EXISTS account (
id TEXT NOT NULL,
username TEXT NOT NULL,
passhash TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
disabled BOOL
);
CREATE UNIQUE INDEX IF NOT EXISTS account_index01
ON account(id);
CREATE UNIQUE INDEX IF NOT EXISTS account_index02
ON account(username);
--- DROP TABLE IF EXISTS grant;
CREATE TABLE IF NOT EXISTS grant (
id TEXT NOT NULL,
account_id TEXT NOT NULL,
grantname TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS grant_index01
ON grant(account_id);
CREATE UNIQUE INDEX IF NOT EXISTS grant_index02
ON grant(account_id, grantname);
`