Adding sqlite3 migrate and make utility commands
parent
012ae2783e
commit
bda1ba8324
@ -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 👍"
|
@ -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…
Reference in New Issue