Skip to content

Commit

Permalink
feat(db): switch to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Dec 8, 2024
1 parent fd6715a commit acdd8f6
Show file tree
Hide file tree
Showing 76 changed files with 579 additions and 522 deletions.
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,10 @@ tmp/
!.env.example

# Project build
backend
tui
backend_bin
tui_bin

.data/

# Log files
logs/

# DB
*.db

# UI Test file
ui/screen/test.go
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ Displays the cammie chat along with some other statistics.

## DB

This project uses an SQLite database.
This project uses a postgres database.
SQLC is used to generate statically typed queries and goose is responsible for the database migrations.

### Usefull commands

- `make db`: Start the database
- `make migrate`: Run database migrations to update your database schema (watch out, migrations might result in minor data loss).
- `make create-migration`: Create a new migration in the [db/migrations](./db/migrations/) directory.
- `make sqlc`: Generate statically typed queries based on the .sql files in the [db/queries](./db/queries/) directory. Add new queries to this directory as needed.
Expand Down Expand Up @@ -60,9 +61,9 @@ To build and run the TUI, use the following commands:
- `make build-tui`: Build the TUI binary.
- `make run-tui`: Run the TUI.
-
The TUI requires one argument: the screen name to display. You can create new screens in the [screens directory](./ui/screen/), and you must add them to the startup command list in [tui.go](./internal/cmd/tui.go).
The TUI requires one argument: the screen name to display. You can create new screens in the [screens directory](./tui/screen/), and you must add them to the startup command list in [tui.go](./internal/cmd/tui.go).

A screen is responsible for creating and managing the layout, consisting of various [views](./ui/view/).
A screen is responsible for creating and managing the layout, consisting of various [views](./tui/view/).

### Logs

Expand Down
54 changes: 54 additions & 0 deletions cmd/backend/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Main entry point for the backend
package main

import (
"github.com/zeusWPI/scc/internal/cmd"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/pkg/config"
"github.com/zeusWPI/scc/pkg/logger"
"go.uber.org/zap"
)

func main() {
// Config
err := config.Init()
if err != nil {
panic(err)
}

// Logger
zapLogger, err := logger.New("backend", true)
if err != nil {
panic(err)
}
zap.ReplaceGlobals(zapLogger)

zap.S().Info("Initializing backend")

// Database
db, err := db.New()
if err != nil {
zap.S().Fatal("DB: Fatal error\n", err)
}

// // Tap
// _, _ = cmd.Tap(db)

// // Zess
// _, _, _ = cmd.Zess(db)

// // Gamification
// _, _ = cmd.Gamification(db)

// Event
_, _ = cmd.Event(db)

// Spotify
spotify, err := cmd.Song(db)
if err != nil {
zap.S().Error("Spotify: Initiating error, integration will not work.\n", err)
}

// API
cmd.API(db, spotify)
}
39 changes: 39 additions & 0 deletions cmd/tui/tui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Main entry point for the tui
package main

import (
"github.com/zeusWPI/scc/internal/cmd"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/pkg/config"
"github.com/zeusWPI/scc/pkg/logger"
"go.uber.org/zap"
)

func main() {
// Config
err := config.Init()
if err != nil {
panic(err)
}

// Logger
zapLogger, err := logger.New("tui", false)
if err != nil {
panic(err)
}
zap.ReplaceGlobals(zapLogger)

zap.S().Info("Initializing TUI")

// Database
db, err := db.New()
if err != nil {
zap.S().Fatal("DB: Fatal error\n", err)
}

// TUI
err = cmd.TUI(db)
if err != nil {
zap.S().Fatal("TUI: Fatal error\n", err)
}
}
4 changes: 2 additions & 2 deletions db/migrations/20241114125504_add_messages_table.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS message (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
ip TEXT NOT NULL,
message TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- +goose StatementEnd

Expand Down
4 changes: 2 additions & 2 deletions db/migrations/20241114135818_add_spotify_table.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS spotify (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
artists TEXT NOT NULL,
spotify_id TEXT NOT NULL,
duration_ms INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- +goose StatementEnd

Expand Down
6 changes: 3 additions & 3 deletions db/migrations/20241114152122_add_tap_table.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS tap (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
order_id INTEGER NOT NULL,
order_created_at TIMESTAMP NOT NULL,
order_created_at TIMESTAMP WITH TIME ZONE NOT NULL,
name TEXT NOT NULL,
category TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
)
-- +goose StatementEnd

Expand Down
8 changes: 4 additions & 4 deletions db/migrations/20241121141143_add_zess_table.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS season (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
start TIMESTAMP NOT NULL,
end TIMESTAMP NOT NULL
start TIMESTAMP WITH TIME ZONE NOT NULL,
"end" TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE TABLE IF NOT EXISTS scan (
id INTEGER PRIMARY KEY,
scan_time TIMESTAMP NOT NULL
scan_time TIMESTAMP WITH TIME ZONE NOT NULL
);
-- +goose StatementEnd

Expand Down
2 changes: 1 addition & 1 deletion db/migrations/20241125113707_add_gamification_table.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS gamification (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
score INTEGER NOT NULL,
avatar VARCHAR(255) NOT NULL
Expand Down
4 changes: 2 additions & 2 deletions db/migrations/20241127133125_add_events_table.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS event (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
date TIMESTAMP NOT NULL,
date TIMESTAMP WITH TIME ZONE NOT NULL,
academic_year TEXT NOT NULL,
location TEXT NOT NULL
);
Expand Down
6 changes: 3 additions & 3 deletions db/migrations/20241127162048_add_song_history_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ DROP COLUMN created_at;
ALTER TABLE spotify RENAME TO song;

CREATE TABLE IF NOT EXISTS song_history (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
song_id INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(song_id) REFERENCES song(id)
);
-- +goose StatementEnd
Expand All @@ -20,5 +20,5 @@ DROP TABLE IF EXISTS song_history;
ALTER TABLE song RENAME TO spotify;

ALTER TABLE spotify
ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
ADD COLUMN created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- +goose StatementEnd
16 changes: 8 additions & 8 deletions db/migrations/20241127165609_add_song_genre.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ ALTER TABLE song
DROP COLUMN artists;

CREATE TABLE IF NOT EXISTS song_genre (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
genre TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS song_artist (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
spotify_id TEXT NOT NULL,
followers INTEGER NOT NULL,
popularity INTEGER NOT NULL
);

CREATE TABLE IF NOT EXISTS song_artist_song (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
artist_id INTEGER NOT NULL,
song_id INTEGER NOT NULL,
FOREIGN KEY(artist_id) REFERENCES artist(id),
FOREIGN KEY(song_id) REFERENCES song(id)
FOREIGN KEY(artist_id) REFERENCES song_artist(id) ON DELETE CASCADE,
FOREIGN KEY(song_id) REFERENCES song(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS song_artist_genre (
id INTEGER PRIMARY KEY,
id SERIAL PRIMARY KEY,
artist_id INTEGER NOT NULL,
genre_id INTEGER NOT NULL,
FOREIGN KEY(artist_id) REFERENCES artist(id),
FOREIGN KEY(genre_id) REFERENCES genre(id)
FOREIGN KEY(artist_id) REFERENCES song_artist(id) ON DELETE CASCADE,
FOREIGN KEY(genre_id) REFERENCES song_genre(id) ON DELETE CASCADE
);
-- +goose StatementEnd

Expand Down
2 changes: 1 addition & 1 deletion db/migrations/20241203173952_event_add_poster.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE event
ADD COLUMN poster BLOB;
ADD COLUMN poster BYTEA;
-- +goose StatementEnd

-- +goose Down
Expand Down
2 changes: 1 addition & 1 deletion db/migrations/20241203231431_gamification_add_avatar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ALTER TABLE gamification
DROP COLUMN avatar;

ALTER TABLE gamification
ADD COLUMN avatar BLOB;
ADD COLUMN avatar BYTEA;
-- +goose StatementEnd

-- +goose Down
Expand Down
8 changes: 4 additions & 4 deletions db/queries/event.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ FROM event;

-- name: CreateEvent :one
INSERT INTO event (name, date, academic_year, location, poster)
VALUES (?, ?, ?, ?, ?)
VALUES ($1, $2, $3, $4, $5)
RETURNING *;

-- name: DeleteEvent :exec
DELETE FROM event
WHERE id = ?;
WHERE id = $1;


-- Other
Expand All @@ -21,11 +21,11 @@ WHERE id = ?;
-- name: GetEventByAcademicYear :many
SELECT *
FROM event
WHERE academic_year = ?;
WHERE academic_year = $1;

-- name: DeleteEventByAcademicYear :exec
DELETE FROM event
WHERE academic_year = ?;
WHERE academic_year = $1;

-- name: GetEventsCurrentAcademicYear :many
SELECT *
Expand Down
8 changes: 4 additions & 4 deletions db/queries/gamification.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ FROM gamification;

-- name: CreateGamification :one
INSERT INTO gamification (name, score, avatar)
VALUES (?, ?, ?)
VALUES ($1, $2, $3)
RETURNING *;

-- name: DeleteGamification :execrows
DELETE FROM gamification
WHERE id = ?;
WHERE id = $1;

-- name: DeleteGamificationAll :execrows
DELETE FROM gamification;
Expand All @@ -22,8 +22,8 @@ DELETE FROM gamification;

-- name: UpdateGamificationScore :one
UPDATE gamification
SET score = ?
WHERE id = ?
SET score = $1
WHERE id = $2
RETURNING *;

-- name: GetAllGamificationByScore :many
Expand Down
12 changes: 6 additions & 6 deletions db/queries/message.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ FROM message;
-- name: GetMessageByID :one
SELECT *
FROM message
WHERE id = ?;
WHERE id = $1;

-- name: CreateMessage :one
INSERT INTO message (name, ip, message)
VALUES (?, ?, ?)
VALUES ($1, $2, $3)
RETURNING *;

-- name: UpdateMessage :one
UPDATE message
SET name = ?, ip = ?, message = ?
WHERE id = ?
SET name = $1, ip = $2, message = $3
WHERE id = $4
RETURNING *;

-- name: DeleteMessage :execrows
DELETE FROM message
WHERE id = ?;
WHERE id = $1;


-- Other
Expand All @@ -37,5 +37,5 @@ LIMIT 1;
-- name: GetMessageSinceID :many
SELECT *
FROM message
WHERE id > ?
WHERE id > $1
ORDER BY created_at ASC;
Loading

0 comments on commit acdd8f6

Please sign in to comment.