Adding sqlite3 migrate and make utility commands

drew/lets-go
Drew Bednar 13 hours ago
parent 012ae2783e
commit bda1ba8324

7
.gitignore vendored

@ -21,4 +21,9 @@
# Go workspace file
go.work
tmp/
tmp/
# Sqlite
*.db
*.db-shm
*.db-wal

@ -1,7 +1,31 @@
SQL_DATABASE?=./ratchet.db
test:
go test -v ./...
PHONEY: test
serve:
go run ./cmd/ratchetd/main.go
PHONEY: serve
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 👍"

@ -84,4 +84,8 @@ func downloadHandler(w http.ResponseWriter, r *http.Request) {
}
```
Warning: http.ServeFile() does not automatically sanitize the file path. If youre constructing a file path from untrusted user input, to avoid directory traversal attacks you must sanitize the input with filepath.Clean() before using it.
Warning: http.ServeFile() does not automatically sanitize the file path. If youre 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.

@ -1,3 +1,5 @@
module git.runcible.io/learning/ratchet
go 1.23.3
require github.com/mattn/go-sqlite3 v1.14.24 // indirect

@ -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=

@ -0,0 +1 @@
DROP TABLE IF EXISTS snippets;

@ -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;
Loading…
Cancel
Save