From e0de61dd94c97600b551ddf10d0061a4362f03f9 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 18 Mar 2024 21:24:26 +0100 Subject: [PATCH 1/4] Type fixes in frontend, Register & YourProjectsPage --- frontend/src/Components/Register.tsx | 4 ++-- frontend/src/Pages/YourProjectsPage.tsx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frontend/src/Components/Register.tsx b/frontend/src/Components/Register.tsx index facca39..7b003cb 100644 --- a/frontend/src/Components/Register.tsx +++ b/frontend/src/Components/Register.tsx @@ -48,7 +48,7 @@ export default function Register(): JSX.Element { { setUsername(e.target.value); }} @@ -56,7 +56,7 @@ export default function Register(): JSX.Element { { setPassword(e.target.value); }} diff --git a/frontend/src/Pages/YourProjectsPage.tsx b/frontend/src/Pages/YourProjectsPage.tsx index 4c6e77f..32ba0ed 100644 --- a/frontend/src/Pages/YourProjectsPage.tsx +++ b/frontend/src/Pages/YourProjectsPage.tsx @@ -53,7 +53,7 @@ function UserProjectPage(): JSX.Element { const buttons = <>; - return ; + return ; } export default UserProjectPage; From c31f145c358bdaabf70122b7c70bbcd3703ccc6e Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 18 Mar 2024 22:07:02 +0100 Subject: [PATCH 2/4] Database sample data, make target and go code --- backend/Makefile | 9 ++++ backend/internal/database/db.go | 43 +++++++++++++++++++ .../database/sample_data/0010_sample_data.sql | 7 +++ backend/main.go | 5 +++ 4 files changed, 64 insertions(+) create mode 100644 backend/internal/database/sample_data/0010_sample_data.sql diff --git a/backend/Makefile b/backend/Makefile index da0e254..65a2f3c 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -10,6 +10,7 @@ DB_FILE = db.sqlite3 # Directory containing migration SQL scripts MIGRATIONS_DIR = internal/database/migrations +SAMPLE_DATA_DIR = internal/database/sample_data # Build target build: @@ -54,6 +55,14 @@ migrate: sqlite3 $(DB_FILE) < $$file; \ done +sampledata: + @echo "If this ever fails, run make clean and try again" + @echo "Migrating database $(DB_FILE) using SQL scripts in $(SAMPLE_DATA_DIR)" + @for file in $(wildcard $(SAMPLE_DATA_DIR)/*.sql); do \ + echo "Applying migration: $$file"; \ + sqlite3 $(DB_FILE) < $$file; \ + done + # Target added primarily for CI/CD to ensure that the database is created before running tests db.sqlite3: make migrate diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index e2aa366..95cf8ed 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -20,6 +20,7 @@ type Database interface { GetUserId(username string) (int, error) AddProject(name string, description string, username string) error Migrate() error + MigrateSampleData() error GetProjectId(projectname string) (int, error) AddWeeklyReport(projectName string, userName string, week int, developmentTime int, meetingTime int, adminTime int, ownWorkTime int, studyTime int, testingTime int) error AddUserToProject(username string, projectname string, role string) error @@ -49,6 +50,9 @@ type UserProjectMember struct { //go:embed migrations var scripts embed.FS +//go:embed sample_data +var sampleData embed.FS + // TODO: Possibly break these out into separate files bundled with the embed package? const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)" const projectInsert = "INSERT INTO projects (name, description, owner_user_id) SELECT ?, ?, id FROM users WHERE username = ?" @@ -378,3 +382,42 @@ func (d *Db) Migrate() error { return nil } + +// MigrateSampleData applies sample data to the database. +func (d *Db) MigrateSampleData() error { + // Insert sample data + files, err := sampleData.ReadDir("sample_data") + if err != nil { + return err + } + + if len(files) == 0 { + println("No sample data files found") + } + tr := d.MustBegin() + + // Iterate over each SQL file and execute it + for _, file := range files { + if file.IsDir() || filepath.Ext(file.Name()) != ".sql" { + continue + } + + // This is perhaps not the most elegant way to do this + sqlBytes, err := sampleData.ReadFile("sample_data/" + file.Name()) + if err != nil { + return err + } + + sqlQuery := string(sqlBytes) + _, err = tr.Exec(sqlQuery) + if err != nil { + return err + } + } + + if tr.Commit() != nil { + return err + } + + return nil +} diff --git a/backend/internal/database/sample_data/0010_sample_data.sql b/backend/internal/database/sample_data/0010_sample_data.sql new file mode 100644 index 0000000..f3d9412 --- /dev/null +++ b/backend/internal/database/sample_data/0010_sample_data.sql @@ -0,0 +1,7 @@ +INSERT OR IGNORE INTO users (username, password) VALUES + ('admin', 'password'), + ('user', 'password'); + +INSERT OR IGNORE INTO projects (name, description, owner_user_id) VALUES + ('Project 1', 'Description 1', 1), + ('Project 2', 'Description 2', 2); \ No newline at end of file diff --git a/backend/main.go b/backend/main.go index da89043..24c3702 100644 --- a/backend/main.go +++ b/backend/main.go @@ -48,11 +48,16 @@ func main() { fmt.Println("Error migrating database: ", err) } + if err = db.MigrateSampleData(); err != nil { + fmt.Println("Error migrating sample data: ", err) + } + // Get our global state gs := handlers.NewGlobalState(db) // Create the server server := fiber.New() + // Mounts the swagger documentation, this is available at /swagger/index.html server.Get("/swagger/*", swagger.HandlerDefault) // Mount our static files (Beware of the security implications of this!) From f5a914330f278a5d4ab7e3ece6bb99fb26e63a7d Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 18 Mar 2024 22:10:19 +0100 Subject: [PATCH 3/4] Removed userId identifier from user table, introducing numerous errors --- backend/internal/database/migrations/0010_users.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/internal/database/migrations/0010_users.sql b/backend/internal/database/migrations/0010_users.sql index d2e2dd1..a3ee5b1 100644 --- a/backend/internal/database/migrations/0010_users.sql +++ b/backend/internal/database/migrations/0010_users.sql @@ -4,7 +4,6 @@ -- password is the hashed password CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, - userId TEXT DEFAULT (HEX(RANDOMBLOB(4))) NOT NULL UNIQUE, username VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL ); From 472940cedc91f42054dd7bd43965740a7a603d91 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 18 Mar 2024 22:20:25 +0100 Subject: [PATCH 4/4] Remove index from userId --- backend/internal/database/migrations/0010_users.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/internal/database/migrations/0010_users.sql b/backend/internal/database/migrations/0010_users.sql index a3ee5b1..e7c770b 100644 --- a/backend/internal/database/migrations/0010_users.sql +++ b/backend/internal/database/migrations/0010_users.sql @@ -9,5 +9,4 @@ CREATE TABLE IF NOT EXISTS users ( ); -- Users are commonly searched by username and userId -CREATE INDEX IF NOT EXISTS users_username_index ON users (username); -CREATE INDEX IF NOT EXISTS users_userId_index ON users (userId); \ No newline at end of file +CREATE INDEX IF NOT EXISTS users_username_index ON users (username); \ No newline at end of file