diff --git a/controller/migrator.go b/controller/migrator.go index d0011ee..b053a27 100644 --- a/controller/migrator.go +++ b/controller/migrator.go @@ -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 + } } diff --git a/log/log.go b/log/log.go index 734cc9f..b90a39b 100644 --- a/log/log.go +++ b/log/log.go @@ -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 +} diff --git a/schema-migration/000-init.sql b/schema-migration/000-init.sql index 42b982a..f70dee6 100644 --- a/schema-migration/000-init.sql +++ b/schema-migration/000-init.sql @@ -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); diff --git a/schema-migration/002-audit-logs.sql b/schema-migration/002-audit-logs.sql new file mode 100644 index 0000000..7da43b5 --- /dev/null +++ b/schema-migration/002-audit-logs.sql @@ -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;