Skip to content

Commit

Permalink
feat(sqlite): created first migration script to initialized database
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Jul 28, 2024
1 parent aa55694 commit 2c473dd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ assets
# Nix
result
api

# Sqlit
*.sqlite
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions db/migrations/000001_create_tables.down.sql
Original file line number Diff line number Diff line change
@@ -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;
62 changes: 62 additions & 0 deletions db/migrations/000001_create_tables.up.sql
Original file line number Diff line number Diff line change
@@ -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
);
14 changes: 13 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down

0 comments on commit 2c473dd

Please sign in to comment.