diff --git a/.gitignore b/.gitignore index 23fc5df..190585d 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ assets # Nix result api + +# Sqlit +*.sqlite diff --git a/Makefile b/Makefile index a410a48..050e4ec 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ DEST_DIR = /opt/komputer ARCH = $(shell uname -m) OUTPUT_DIR=./build PROTOBUF_API_DEST=./api +DB_PATH ?= db.sqlite ifeq ($(ARCH), x86_64) GOARCH="amd64" @@ -38,6 +39,9 @@ test-server: protobuf all: bot-prod sever tui test +update-database: + migrate -database sqlite3://$(DB_PATH) -path db/migrations up + clean: cleanProto ifneq ("$(wildcard $(OUTPUT_DIR))", "") rm -r $(OUTPUT_DIR) diff --git a/db/migrations/000001_create_tables.down.sql b/db/migrations/000001_create_tables.down.sql new file mode 100644 index 0000000..9991ba9 --- /dev/null +++ b/db/migrations/000001_create_tables.down.sql @@ -0,0 +1,5 @@ +drop table if exists jokes; +drop trigger if exists joke_add_event_trigger; +drop trigger if exists joke_update_event_trigger; +drop table if exists jokes_audit; +drop table if exists jokes_audit; \ No newline at end of file diff --git a/db/migrations/000001_create_tables.up.sql b/db/migrations/000001_create_tables.up.sql new file mode 100644 index 0000000..93c2f79 --- /dev/null +++ b/db/migrations/000001_create_tables.up.sql @@ -0,0 +1,62 @@ +create table jokes +( + id integer primary key autoincrement, + question text, + answer text not null, + type text not null, + category text not null, + userID text, + guildID text +); + +create index jokes_index on jokes (id, guildID); + +create table jokes_audit +( + id primary key, + status text default 'CREATED', + joke_id integer not null, + question text, + answer text not null, + type text not null, + category text not null, + userID text, + guildID text +); + +create trigger joke_add_event_trigger + after insert + on jokes +begin + insert + into jokes_audit(joke_id, question, answer, type, category, userID, guildID) + values (new.id, new.question, new.answer, new.type, new.category, new.userID, new.guildID); +end; + +create trigger joke_update_event_trigger + after update + on jokes +begin + insert + into jokes_audit(status, joke_id, question, answer, type, category, userID, guildID) + values ('UPDATED', new.id, new.question, new.answer, new.type, + new.category, + new.userID, new.guildID); +end; + +create trigger joke_delete_event_trigger + before delete + on jokes +begin + insert + into jokes_audit(status, joke_id, question, answer, type, category, userID, guildID) + values ('DELETE', old.id, old.question, old.answer, old.type, + old.category, + old.userID, old.guildID); +end; + +create table admins +( + id primary key, + name text unique not null +); \ No newline at end of file diff --git a/shell.nix b/shell.nix index daf05f2..0de9f0f 100644 --- a/shell.nix +++ b/shell.nix @@ -1,8 +1,20 @@ -{ mkShell, go, gopls, ffmpeg, nixfmt-classic, protoc-gen-go, protobuf, protoc-gen-go-grpc, act, ... }: mkShell { +{ mkShell +, go +, gopls +, ffmpeg +, nixfmt-classic +, protoc-gen-go +, protobuf +, protoc-gen-go-grpc +, act +, go-migrate +, ... +}: mkShell { hardeningDisable = [ "all" ]; nativeBuildInputs = [ go protobuf + go-migrate act ]; buildInputs = [