audit log basic db implementation

This commit is contained in:
ari melody 2025-02-06 13:45:33 +00:00
parent e80a6753a5
commit aa144b719a
Signed by: ari
GPG key ID: CF99829C92678188
4 changed files with 62 additions and 8 deletions

View file

@ -8,7 +8,7 @@ import (
"github.com/jmoiron/sqlx"
)
const DB_VERSION int = 2
const DB_VERSION int = 3
func CheckDBVersionAndMigrate(db *sqlx.DB) {
db.MustExec("CREATE SCHEMA IF NOT EXISTS arimelody")
@ -41,6 +41,10 @@ func CheckDBVersionAndMigrate(db *sqlx.DB) {
ApplyMigration(db, "001-pre-versioning")
oldDBVersion = 2
case 2:
ApplyMigration(db, "002-audit-logs")
oldDBVersion = 3
}
}

View file

@ -22,17 +22,35 @@ type (
)
const (
TYPE_ACCOUNT = "account"
TYPE_ACCOUNT string = "account"
TYPE_MUSIC string = "music"
TYPE_BLOG string = "blog"
TYPE_ARTWORK string = "artwork"
TYPE_MISC string = "misc"
)
type LogLevel int
const (
LEVEL_INFO LogLevel = 0
LEVEL_WARN LogLevel = 1
)
func (self *Logger) Info(logType string, format string, args ...any) {
fmt.Printf(fmt.Sprintf("[%s] INFO: %s", logType, format), args...)
// TODO: push logs to DB
logString := fmt.Sprintf(format, args...)
fmt.Printf("[%s] INFO: %s", logType, logString)
err := createLog(self.DB, LEVEL_INFO, logType, logString)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to push log to database: %v\n", err)
}
}
func (self *Logger) Warn(logType string, format string, args ...any) {
fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] WARN: %s", logType, format), args...)
// TODO: push logs to DB
logString := fmt.Sprintf(format, args...)
fmt.Fprintf(os.Stderr, "[%s] WARN: %s", logType, logString)
err := createLog(self.DB, LEVEL_WARN, logType, logString)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to push log to database: %v\n", err)
}
}
func (self *Logger) Fatal(logType string, format string, args ...any) {
@ -56,3 +74,13 @@ func (self *Logger) Delete(id string) error {
// or just not deleting logs at all
return nil
}
func createLog(db *sqlx.DB, logLevel LogLevel, logType string, content string) error {
_, err := db.Exec(
"INSERT INTO auditlog (level, type, content) VALUES ($1,$2,$3)",
logLevel,
logType,
content,
)
return err
}

View file

@ -2,6 +2,15 @@
-- Tables
--
-- Audit logs
CREATE TABLE arimelody.auditlog (
id UUID DEFAULT gen_random_uuid(),
level int NOT NULL DEFAULT 0,
type TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp
);
-- Accounts
CREATE TABLE arimelody.account (
id UUID DEFAULT gen_random_uuid(),
@ -9,7 +18,7 @@ CREATE TABLE arimelody.account (
password TEXT NOT NULL,
email TEXT,
avatar_url TEXT,
created_at TIMESTAMP DEFAULT current_timestamp
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp
);
ALTER TABLE arimelody.account ADD CONSTRAINT account_pk PRIMARY KEY (id);
@ -74,7 +83,8 @@ CREATE TABLE arimelody.musicrelease (
buyname text,
buylink text,
copyright text,
copyrightURL text
copyrightURL text,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp,
);
ALTER TABLE arimelody.musicrelease ADD CONSTRAINT musicrelease_pk PRIMARY KEY (id);

View file

@ -0,0 +1,12 @@
-- Audit logs
CREATE TABLE arimelody.auditlog (
id UUID DEFAULT gen_random_uuid(),
level int NOT NULL DEFAULT 0,
type TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT current_timestamp
);
-- Need moar timestamps
ALTER TABLE arimelody.musicrelease ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT current_timestamp;
ALTER TABLE arimelody.account ALTER COLUMN created_at SET NOT NULL;