init import

This commit is contained in:
Олег Бородин
2026-05-24 11:02:51 +02:00
commit 25365aef77
154 changed files with 21501 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
package maindb
import (
"context"
"mbase/pkg/descr"
)
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, 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
}
return err
}
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, 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
}
return err
}
func (db *Database) ReducedListAccounts(ctx context.Context) ([]descr.Account, error) {
var err error
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 {
return res, err
}
return res, err
}
func (db *Database) ListAccounts(ctx context.Context) ([]descr.Account, error) {
var err error
request := `SELECT * FROM accounts`
res := make([]descr.Account, 0)
err = db.db.Select(&res, request)
if err != nil {
return res, err
}
return res, err
}
func (db *Database) GetAccountByID(ctx context.Context, accountID string) (bool, *descr.Account, error) {
var err error
var res *descr.Account
var exists bool = false
request := `SELECT * FROM accounts WHERE id = $1 LiMIT 1`
dbRes := make([]descr.Account, 0)
err = db.db.Select(&dbRes, request, accountID)
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) GetAccountByUsername(ctx context.Context, username string) (bool, *descr.Account, error) {
var err error
var res *descr.Account
var exists bool
request := `SELECT * FROM accounts WHERE username = $1 LIMIT 1`
dbRes := make([]descr.Account, 0)
err = db.db.Select(&dbRes, request, username)
if err != nil {
return exists, res, err
}
if len(dbRes) == 0 {
return false, res, err
}
exists = true
res = &dbRes[0]
return exists, res, err
}
func (db *Database) DeleteAccountByID(ctx context.Context, accountID string) error {
var err error
request := `DELETE FROM accounts WHERE id = $1`
_, err = db.db.Exec(request, accountID)
if err != nil {
return err
}
return err
}
func (db *Database) DeleteAccountByUsername(ctx context.Context, username string) error {
var err error
request := `DELETE FROM accounts WHERE username = $1`
_, err = db.db.Exec(request, username)
if err != nil {
return err
}
return err
}
+149
View File
@@ -0,0 +1,149 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package maindb
import (
"context"
"mbase/pkg/descr"
)
func (db *Database) InsertGrant(ctx context.Context, grant *descr.Grant) error {
var err error
request := `INSERT INTO grants(id, account_id, right, 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.Right, 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 grants 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
}
return err
}
func (db *Database) ListGrantsByAccountID(ctx context.Context, accountID string) ([]descr.Grant, error) {
var err error
request := `SELECT * FROM grants WHERE account_id = $1`
res := make([]descr.Grant, 0)
err = db.db.Select(&res, request, accountID)
if err != nil {
return res, err
}
return res, err
}
func (db *Database) ListGrants(ctx context.Context) ([]descr.Grant, error) {
var err error
request := `SELECT * FROM grants`
res := make([]descr.Grant, 0)
err = db.db.Select(&res, request)
if err != nil {
return res, err
}
return res, err
}
func (db *Database) GetGrantByID(ctx context.Context, garntID string) (bool, *descr.Grant, error) {
var err error
res := &descr.Grant{}
request := `SELECT * FROM grants WHERE id = $1 LIMIT 1`
dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, garntID)
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) GetGrantByAccoundIDRight(ctx context.Context, accountID, right string) (bool, *descr.Grant, error) {
var err error
res := &descr.Grant{}
request := `SELECT * FROM grants WHERE account_id = $1 AND right = $2 LIMIT 1`
dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, accountID, right)
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) ListGrantsByAccoundIDRight(ctx context.Context, accountID, right string) (bool, []descr.Grant, error) {
var err error
request := `SELECT * FROM grants WHERE account_id = $1 AND right = $2`
res := make([]descr.Grant, 0)
err = db.db.Select(&res, request, accountID, right)
if err != nil {
return false, res, err
}
if len(res) == 0 {
return false, res, err
}
return true, res, err
}
func (db *Database) GetGrantByAccoundIDRightPattern(ctx context.Context, accountID, right, pattern string) (bool, *descr.Grant, error) {
var err error
res := &descr.Grant{}
request := `SELECT * FROM grants WHERE account_id = $1 AND right = $2 AND pattern = $3 LIMIT 1`
dbRes := make([]descr.Grant, 0)
err = db.db.Select(&dbRes, request, accountID, right, 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) DeleteGrantByAccountIDRightPattern(ctx context.Context, accountID, right, pattern string) error {
var err error
request := `DELETE FROM grants WHERE account_id = $1 AND right = $2 AND pattern = $3`
_, err = db.db.Exec(request, accountID, right, 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
}
return err
}
func (db *Database) DeleteAllGrantsForAccountID(ctx context.Context, grantID string) error {
var err error
request := `DELETE FROM grants WHERE account_id = $1`
_, err = db.db.Exec(request, grantID)
if err != nil {
return err
}
return err
}
+58
View File
@@ -0,0 +1,58 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package maindb
import (
"context"
"fmt"
"testing"
"time"
"mbase/pkg/auxtool"
"mbase/pkg/auxuuid"
"mbase/pkg/descr"
"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 := auxuuid.NewUUID()
newGrant := &descr.Grant{
ID: id,
AccountID: accountID,
Right: "rigthFoo",
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)
require.Equal(t, files[0].CreatedBy, creator)
fmt.Println(files[0].CreatedBy)
}
+83
View File
@@ -0,0 +1,83 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
*
*/
package maindb
import (
"context"
"mbase/pkg/auxpwd"
"mbase/pkg/auxtool"
"mbase/pkg/auxuuid"
"mbase/pkg/descr"
"mbase/pkg/terms"
)
func (db *Database) WriteAnonymous(ctx context.Context) error {
var err error
now := auxtool.TimeNow()
password := auxtool.RandomString(64)
passhash := auxpwd.MakeSHA256Hash([]byte(password))
accountDescr := &descr.Account{
ID: terms.AnonymousID,
Username: terms.AnonimousUsername,
Passhash: passhash,
Disabled: false,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: terms.ServerID,
UpdatedBy: terms.ServerID,
}
err = db.InsertAccount(ctx, accountDescr)
if err != nil {
return err
}
return err
}
func (db *Database) WriteInituser(ctx context.Context) error {
var err error
now := auxtool.TimeNow()
passhash := auxpwd.MakeSHA256Hash([]byte(terms.InitUsername))
accountDescr := &descr.Account{
ID: terms.InitID,
Username: terms.InitUsername,
Passhash: passhash,
Disabled: false,
CreatedAt: now,
UpdatedAt: now,
CreatedBy: terms.ServerID,
UpdatedBy: terms.ServerID,
}
err = db.InsertAccount(ctx, accountDescr)
if err != nil {
return err
}
fullRights := []string{
terms.RightWriteAccounts,
terms.RightReadAccounts,
}
for _, right := range fullRights {
grantDescr := &descr.Grant{
ID: auxuuid.NewUUID(),
AccountID: accountDescr.ID,
Right: right,
Pattern: ".*",
CreatedAt: now,
UpdatedAt: now,
CreatedBy: terms.ServerID,
UpdatedBy: terms.ServerID,
}
err = db.InsertGrant(ctx, grantDescr)
if err != nil {
return err
}
}
return err
}
+50
View File
@@ -0,0 +1,50 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package maindb
import (
"fmt"
"path/filepath"
"mbase/app/logger"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
type Database struct {
datapath string
logg *logger.Logger
db *sqlx.DB
}
func NewDatabase(datapath string) *Database {
return &Database{
datapath: datapath,
logg: logger.NewLoggerWithSubject("maindb"),
}
}
func (db *Database) OpenDatabase() error {
var err error
dbPath := filepath.Join(db.datapath, "mbase.db")
db.db, err = sqlx.Open("sqlite3", fmt.Sprintf("%s?cache=shared&mode=rwc&_journal_mode=WAL", dbPath))
if err != nil {
return fmt.Errorf("Open database error: %v", err)
}
err = db.db.Ping()
if err != nil {
return fmt.Errorf("Ping database error: %v", err)
}
return err
}
func (db *Database) InitDatabase() error {
var err error
_, err = db.db.Exec(schema)
if err != nil {
return fmt.Errorf("Init database error: %v", err)
}
return err
}
+43
View File
@@ -0,0 +1,43 @@
/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*/
package maindb
const schema = `
--- DROP TABLE IF EXISTS accounts;
CREATE TABLE IF NOT EXISTS accounts (
id TEXT NOT NULL,
username TEXT NOT NULL,
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
ON accounts(id);
CREATE UNIQUE INDEX IF NOT EXISTS accounts_index02
ON accounts(username);
--- DROP TABLE IF EXISTS grants;
CREATE TABLE IF NOT EXISTS grants (
id TEXT NOT NULL,
account_id INT NOT NULL,
right 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 INDEX IF NOT EXISTS grants_index02
ON grants(account_id, right);
CREATE UNIQUE INDEX IF NOT EXISTS grants_index03
ON grants(account_id, right, pattern);
`