home to your local SPACEGIRL 💫 arimelody.space
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

audit log basic db implementation

+62 -8
+5 -1
controller/migrator.go
··· 8 8 "github.com/jmoiron/sqlx" 9 9 ) 10 10 11 - const DB_VERSION int = 2 11 + const DB_VERSION int = 3 12 12 13 13 func CheckDBVersionAndMigrate(db *sqlx.DB) { 14 14 db.MustExec("CREATE SCHEMA IF NOT EXISTS arimelody") ··· 40 40 // into the old database in order for this to work LOL 41 41 ApplyMigration(db, "001-pre-versioning") 42 42 oldDBVersion = 2 43 + 44 + case 2: 45 + ApplyMigration(db, "002-audit-logs") 46 + oldDBVersion = 3 43 47 44 48 } 45 49 }
+33 -5
log/log.go
··· 22 22 ) 23 23 24 24 const ( 25 - TYPE_ACCOUNT = "account" 25 + TYPE_ACCOUNT string = "account" 26 + TYPE_MUSIC string = "music" 27 + TYPE_BLOG string = "blog" 28 + TYPE_ARTWORK string = "artwork" 29 + TYPE_MISC string = "misc" 30 + ) 31 + 32 + type LogLevel int 33 + const ( 34 + LEVEL_INFO LogLevel = 0 35 + LEVEL_WARN LogLevel = 1 26 36 ) 27 37 28 38 func (self *Logger) Info(logType string, format string, args ...any) { 29 - fmt.Printf(fmt.Sprintf("[%s] INFO: %s", logType, format), args...) 30 - // TODO: push logs to DB 39 + logString := fmt.Sprintf(format, args...) 40 + fmt.Printf("[%s] INFO: %s", logType, logString) 41 + err := createLog(self.DB, LEVEL_INFO, logType, logString) 42 + if err != nil { 43 + fmt.Fprintf(os.Stderr, "WARN: Failed to push log to database: %v\n", err) 44 + } 31 45 } 32 46 33 47 func (self *Logger) Warn(logType string, format string, args ...any) { 34 - fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] WARN: %s", logType, format), args...) 35 - // TODO: push logs to DB 48 + logString := fmt.Sprintf(format, args...) 49 + fmt.Fprintf(os.Stderr, "[%s] WARN: %s", logType, logString) 50 + err := createLog(self.DB, LEVEL_WARN, logType, logString) 51 + if err != nil { 52 + fmt.Fprintf(os.Stderr, "WARN: Failed to push log to database: %v\n", err) 53 + } 36 54 } 37 55 38 56 func (self *Logger) Fatal(logType string, format string, args ...any) { ··· 56 74 // or just not deleting logs at all 57 75 return nil 58 76 } 77 + 78 + func createLog(db *sqlx.DB, logLevel LogLevel, logType string, content string) error { 79 + _, err := db.Exec( 80 + "INSERT INTO auditlog (level, type, content) VALUES ($1,$2,$3)", 81 + logLevel, 82 + logType, 83 + content, 84 + ) 85 + return err 86 + }
+12 -2
schema-migration/000-init.sql
··· 2 2 -- Tables 3 3 -- 4 4 5 + -- Audit logs 6 + CREATE TABLE arimelody.auditlog ( 7 + id UUID DEFAULT gen_random_uuid(), 8 + level int NOT NULL DEFAULT 0, 9 + type TEXT NOT NULL, 10 + content TEXT NOT NULL, 11 + created_at TIMESTAMP NOT NULL DEFAULT current_timestamp 12 + ); 13 + 5 14 -- Accounts 6 15 CREATE TABLE arimelody.account ( 7 16 id UUID DEFAULT gen_random_uuid(), ··· 9 18 password TEXT NOT NULL, 10 19 email TEXT, 11 20 avatar_url TEXT, 12 - created_at TIMESTAMP DEFAULT current_timestamp 21 + created_at TIMESTAMP NOT NULL DEFAULT current_timestamp 13 22 ); 14 23 ALTER TABLE arimelody.account ADD CONSTRAINT account_pk PRIMARY KEY (id); 15 24 ··· 74 83 buyname text, 75 84 buylink text, 76 85 copyright text, 77 - copyrightURL text 86 + copyrightURL text, 87 + created_at TIMESTAMP NOT NULL DEFAULT current_timestamp, 78 88 ); 79 89 ALTER TABLE arimelody.musicrelease ADD CONSTRAINT musicrelease_pk PRIMARY KEY (id); 80 90
+12
schema-migration/002-audit-logs.sql
··· 1 + -- Audit logs 2 + CREATE TABLE arimelody.auditlog ( 3 + id UUID DEFAULT gen_random_uuid(), 4 + level int NOT NULL DEFAULT 0, 5 + type TEXT NOT NULL, 6 + content TEXT NOT NULL, 7 + created_at TIMESTAMP NOT NULL DEFAULT current_timestamp 8 + ); 9 + 10 + -- Need moar timestamps 11 + ALTER TABLE arimelody.musicrelease ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT current_timestamp; 12 + ALTER TABLE arimelody.account ALTER COLUMN created_at SET NOT NULL;