From bda1ba8324015dcc14b5a0fb8c4f85ab979da1ab Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 18 Jan 2025 15:27:05 -0500 Subject: [PATCH] Adding sqlite3 migrate and make utility commands --- .gitignore | 7 ++++++- Makefile | 26 +++++++++++++++++++++++++- README.md | 6 +++++- go.mod | 2 ++ go.sum | 2 ++ migrations/10_snippets.down.sql | 1 + migrations/10_snippets.up.sql | 20 ++++++++++++++++++++ 7 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 migrations/10_snippets.down.sql create mode 100644 migrations/10_snippets.up.sql diff --git a/.gitignore b/.gitignore index 26ef082..3a2321a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,9 @@ # Go workspace file go.work -tmp/ \ No newline at end of file +tmp/ + +# Sqlite +*.db +*.db-shm +*.db-wal diff --git a/Makefile b/Makefile index 7070b2d..ceb0eb3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,31 @@ +SQL_DATABASE?=./ratchet.db + test: go test -v ./... PHONEY: test serve: go run ./cmd/ratchetd/main.go -PHONEY: serve \ No newline at end of file +PHONEY: serve + + +# SQLite Commands + +sql-cli: + sqlite3 $(SQL_DATABASE) -cmd ".headers on" -cmd ".mode column" -cmd ".tables" + +init-db: run-migrate + sqlite3 $(SQL_DATABASE) "PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL;" + +seed-db: + sqlite3 $(SQL_DATABASE) "INSERT INTO snippets (title, content, expires_at) VALUES ('placeholder', 'placeholder content', datetime('now', '+6 months'));" + +run-migrate: + migrate -database sqlite3://$(SQL_DATABASE) -path ./migrations up + +# Checks system dependencies needed to run the local dev environment +check-system-deps: + @echo "Checking system dependencies..." + @command -v sqlite3 > /dev/null || (echo "Missing sqlite3 command. brew install sqlite"; exit 1) + @command -v migrate > /dev/null || (echo "Missing migrate command. go install -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@latest"; exit 1) + @echo "System dependencies fulfilled 👍" \ No newline at end of file diff --git a/README.md b/README.md index f995bd6..a3e6226 100644 --- a/README.md +++ b/README.md @@ -84,4 +84,8 @@ func downloadHandler(w http.ResponseWriter, r *http.Request) { } ``` -Warning: http.ServeFile() does not automatically sanitize the file path. If you’re constructing a file path from untrusted user input, to avoid directory traversal attacks you must sanitize the input with filepath.Clean() before using it. \ No newline at end of file +Warning: http.ServeFile() does not automatically sanitize the file path. If you’re constructing a file path from untrusted user input, to avoid directory traversal attacks you must sanitize the input with filepath.Clean() before using it. + +## Databases + +The Let's-go book calls for MySQL. We use [go-sqlite3](ggithub.com/mattn/go-sqlite3) and [go-migrate]() tool to manage migrations instead. Use `make check-system-deps` to validate all tools for this repository are installed. diff --git a/go.mod b/go.mod index f5929ae..6c5902a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.runcible.io/learning/ratchet go 1.23.3 + +require github.com/mattn/go-sqlite3 v1.14.24 // indirect diff --git a/go.sum b/go.sum index e69de29..9dcdc9b 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/migrations/10_snippets.down.sql b/migrations/10_snippets.down.sql new file mode 100644 index 0000000..6c0a277 --- /dev/null +++ b/migrations/10_snippets.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS snippets; \ No newline at end of file diff --git a/migrations/10_snippets.up.sql b/migrations/10_snippets.up.sql new file mode 100644 index 0000000..be37e4a --- /dev/null +++ b/migrations/10_snippets.up.sql @@ -0,0 +1,20 @@ +PRAGMA foreign_keys=1; +CREATE TABLE snippets ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT, + content TEXT, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, + expires_at DATETIME NOT NULL +); + +-- Add an index on the created column. +CREATE INDEX idx_snippets_created ON snippets(created_at); + +-- Add a trigger to keep timestamp updated. +CREATE TRIGGER snippet_update_timestamp +AFTER UPDATE ON snippets +FOR EACH ROW +BEGIN + UPDATE snippets SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id; +END;