working commit

This commit is contained in:
2026-02-12 12:00:17 +02:00
parent ae15bddb15
commit b279687623
20 changed files with 639 additions and 192 deletions
+7 -7
View File
@@ -9,10 +9,10 @@ import (
func (db *Database) InsertAccount(ctx context.Context, account *descr.Account) error {
var err error
request := `INSERT INTO accounts(id, username, passhash, disabled, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6)`
_, err = db.db.Exec(request, account.ID, account.Username, account.Passhash,
account.Disabled, account.CreatedAt, account.UpdatedAt)
request := `INSERT INTO accounts(id, username, passhash, disabled, created_at, updated_at, created_by, updated_by)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
_, err = db.db.Exec(request, account.ID, account.Username, account.Passhash, account.Disabled,
account.CreatedAt, account.UpdatedAt, account.CreatedBy, account.UpdatedBy)
if err != nil {
return err
}
@@ -21,8 +21,8 @@ func (db *Database) InsertAccount(ctx context.Context, account *descr.Account) e
func (db *Database) UpdateAccountByID(ctx context.Context, accountID string, account *descr.Account) error {
var err error
request := `UPDATE accounts SET username = $1, passhash = $2, disabled = $3, updated_at = $4 WHERE id = $6`
_, err = db.db.Exec(request, account.Username, account.Passhash, account.Disabled, account.UpdatedAt, accountID)
request := `UPDATE accounts SET username = $1, passhash = $2, disabled = $3, updated_at = $4, updated_by = $5 WHERE id = $6`
_, err = db.db.Exec(request, account.Username, account.Passhash, account.Disabled, account.UpdatedAt, account.UpdatedBy, accountID)
if err != nil {
return err
}
@@ -31,7 +31,7 @@ func (db *Database) UpdateAccountByID(ctx context.Context, accountID string, acc
func (db *Database) ReducedListAccounts(ctx context.Context) ([]descr.Account, error) {
var err error
request := `SELECT id, username, disabled, created_at, updated_at FROM accounts`
request := `SELECT id, username, disabled, created_at, updated_at, created_by, updated_by FROM accounts`
res := make([]descr.Account, 0)
err = db.db.Select(&res, request)
if err != nil {
+2 -20
View File
@@ -62,25 +62,7 @@ func (db *Database) ListAllFiles(ctx context.Context) ([]descr.File, error) {
return res, err
}
func (db *Database) GetFileByID(ctx context.Context, fileID int64) (bool, *descr.File, error) {
var err error
var res *descr.File
var exists bool
request := `SELECT * FROM files WHERE id = $1 LiMIT 1`
dbRes := make([]descr.File, 0)
err = db.db.Select(&dbRes, request, fileID)
if err != nil {
return exists, res, err
}
if len(dbRes) == 0 {
return exists, res, err
}
exists = true
res = &dbRes[0]
return exists, res, err
}
func (db *Database) GetFileByCollection(ctx context.Context, collection, name string) (bool, *descr.File, error) {
func (db *Database) GetFileByCollectionName(ctx context.Context, collection, name string) (bool, *descr.File, error) {
var err error
var res *descr.File
var exists bool
@@ -99,7 +81,7 @@ func (db *Database) GetFileByCollection(ctx context.Context, collection, name st
return exists, res, err
}
func (db *Database) DeleteFileByCollection(ctx context.Context, collection, name string) error {
func (db *Database) DeleteFileByCollectionName(ctx context.Context, collection, name string) error {
var err error
request := `DELETE FROM files WHERE collection = $1 AND name = $2`
_, err = db.db.Exec(request, collection, name)
+5 -3
View File
@@ -10,7 +10,9 @@
package maindb
import (
"context"
"testing"
"time"
"mstore/app/descr"
"mstore/pkg/auxtool"
@@ -45,11 +47,11 @@ func TestFile(t *testing.T) {
CreatedBy: creator,
UpdatedBy: creator,
}
err = db.InsertFile(newFile)
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
err = db.InsertFile(ctx, newFile)
require.NoError(t, err)
files, err := db.ListFilesByCollection(collection)
files, err := db.ListFilesByCollection(ctx, collection)
require.NoError(t, err)
require.Equal(t, len(files), 1)
require.Equal(t, files[0].ID, id)
+47 -9
View File
@@ -8,9 +8,20 @@ import (
func (db *Database) InsertGrant(ctx context.Context, grant *descr.Grant) error {
var err error
request := `INSERT INTO grants(id, account_id, operation, created_at)
VALUES ($1, $2, $3, $4)`
_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Operation, grant.CreatedAt)
request := `INSERT INTO grants(id, account_id, operation, pattern, created_at, updated_at, created_by, updated_by)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Operation, grant.Pattern,
grant.CreatedAt, grant.UpdatedAt, grant.CreatedBy, grant.UpdatedBy)
if err != nil {
return err
}
return err
}
func (db *Database) UpdateGrantByID(ctx context.Context, grantID string, grant *descr.Grant) error {
var err error
request := `UPDATE accounts SET pattern = $1, updated_at = $2, updated_by = $3 WHERE id = $4`
_, err = db.db.Exec(request, grant.Pattern, grant.UpdatedAt, grant.UpdatedBy, grantID)
if err != nil {
return err
}
@@ -39,12 +50,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) GetGrantByID(ctx context.Context, id string) (bool, *descr.Grant, error) {
var err error
res := &descr.Grant{}
request := `SELECT * FROM grants WHERE account_id = $1 AND operation = $2 LIMIT 1`
request := `SELECT * FROM grants WHERE id = $1 LIMIT 1`
dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, accountID, operation)
err = db.db.Select(&dbRes, request, id)
if err != nil {
return false, res, err
}
@@ -56,10 +67,37 @@ 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) GetGrantByAccoundIDOperationPattern(ctx context.Context, accountID, operation, pattern string) (bool, *descr.Grant, error) {
var err error
request := `DELETE FROM grants WHERE account_id = $1 AND operation = $2`
_, err = db.db.Exec(request, grantID, operation)
res := &descr.Grant{}
request := `SELECT * FROM grants WHERE account_id = $1 AND operation = $2 AND pattern = $3 LIMIT 1`
dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, accountID, operation, pattern)
if err != nil {
return false, res, err
}
if len(dbRes) == 0 {
return false, res, err
}
res = &dbRes[0]
return true, res, err
}
func (db *Database) DeleteGrantByAccountIDOperationPattern(ctx context.Context, accountID, operation, pattern string) error {
var err error
request := `DELETE FROM grants WHERE account_id = $1 AND operation = $2 AND pattern = $3`
_, err = db.db.Exec(request, accountID, operation, pattern)
if err != nil {
return err
}
return err
}
func (db *Database) DeleteGrantByID(ctx context.Context, grantID string) error {
var err error
request := `DELETE FROM grants WHERE id = $1`
_, err = db.db.Exec(request, grantID)
if err != nil {
return err
}
+60
View File
@@ -0,0 +1,60 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package maindb
import (
"context"
"testing"
"time"
"mstore/app/descr"
"mstore/pkg/auxtool"
"mstore/pkg/auxuuid"
"github.com/stretchr/testify/require"
)
func TestGrant(t *testing.T) {
var err error
dbDir := t.TempDir()
db := NewDatabase(dbDir)
err = db.OpenDatabase()
require.NoError(t, err)
err = db.InitDatabase()
require.NoError(t, err)
id := auxuuid.NewUUID()
accountID := auxuuid.NewUUID()
timenow := auxtool.TimeNow()
creator := "some"
newGrant := &descr.Grant{
ID: id,
AccountID: accountID,
Operation: "opFoo",
Pattern: `*`,
CreatedAt: timenow,
UpdatedAt: timenow,
CreatedBy: creator,
UpdatedBy: creator,
}
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
err = db.InsertGrant(ctx, newGrant)
require.NoError(t, err)
files, err := db.ListGrantsByAccountID(ctx, accountID)
require.NoError(t, err)
require.Equal(t, len(files), 1)
require.Equal(t, files[0].ID, id)
require.Equal(t, files[0].AccountID, accountID)
}
+12 -2
View File
@@ -63,6 +63,8 @@ const schema = `
passhash TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
created_by TEXT NOT NULL,
updated_by TEXT NOT NULL,
disabled BOOL
);
CREATE UNIQUE INDEX IF NOT EXISTS accounts_index01
@@ -75,11 +77,19 @@ const schema = `
id TEXT NOT NULL,
account_id INT NOT NULL,
operation TEXT NOT NULL,
created_at TEXT NOT NULL
pattern TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
created_by TEXT NOT NULL,
updated_by TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS grants_index00
ON grants(id);
CREATE INDEX IF NOT EXISTS grants_index01
ON grants(account_id);
CREATE UNIQUE INDEX IF NOT EXISTS grants_index02
CREATE INDEX IF NOT EXISTS grants_index02
ON grants(account_id, operation);
CREATE UNIQUE INDEX IF NOT EXISTS grants_index03
ON grants(account_id, operation, pattern);
`