Skip to content

Commit

Permalink
Move DB seed data to separate SQL scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
szabolcs-horvath committed Dec 21, 2024
1 parent 8e29518 commit 86dd679
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PORT="6969"
DB_FILE="sqlite/nutrition-tracker-test.db"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ go.work.sum
# out
out/

# .db file
# .db files
*.db

# generated files
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SQLITE_DB_FILE ?= sqlite/nutrition-tracker.db
IT_SQLITE_DB_FILE ?= sqlite/nutrition-tracker-test.db
SQLITE_MIGRATIONS_DIR ?= sqlite/migrations
SQLC_VERSION ?= v1.27.0
GOLANG_MIGRATE_VERSION ?= v4.18.1
Expand Down Expand Up @@ -27,13 +28,18 @@ unit-test: sqlc
mkdir -p $(GOCOVERDIR)
go test ./... -v -coverprofile=$(GOCOVERDIR)/unit-coverage.out -covermode=atomic -coverpkg=./...

integration-test: sqlc
integration-test: sqlc init-test-db
rm -rf $(GOCOVERDIR)/it-coverage
mkdir -p $(GOCOVERDIR)/it-coverage
go build -o out/nutrition-tracker-integration-test -mod=readonly -covermode=atomic
integration-test/integration-test.sh out/nutrition-tracker-integration-test $(GOCOVERDIR)/it-coverage
go tool covdata textfmt -i=$(GOCOVERDIR)/it-coverage -o=$(GOCOVERDIR)/it-coverage.out

init-test-db:
rm -f $(IT_SQLITE_DB_FILE)
make migrate-up-file SPECIFIED_DB_FILE=$(IT_SQLITE_DB_FILE)
sqlite3 $(IT_SQLITE_DB_FILE) < sqlite/seed_test.sql

coverage: unit-test integration-test
go run ./.github/merge-coverprofiles.go $(GOCOVERDIR)/merged-coverage.out $(GOCOVERDIR)/unit-coverage.out $(GOCOVERDIR)/it-coverage.out
go tool cover -html=$(GOCOVERDIR)/merged-coverage.out
Expand Down
3 changes: 2 additions & 1 deletion repository/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package repository
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/szabolcs-horvath/nutrition-tracker/util"
)

func GetDB() (*sql.DB, error) {
return sql.Open("sqlite3", "sqlite/nutrition-tracker.db")
return sql.Open("sqlite3", util.SafeGetEnv("DB_FILE"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ CREATE TABLE IF NOT EXISTS languages (
native_name TEXT NOT NULL
);

--Default languages
INSERT INTO languages(id, name, native_name)
VALUES (1, 'en', 'English'),
(2, 'hu', 'magyar');

ALTER TABLE users
ADD COLUMN language_id INTEGER REFERENCES languages NOT NULL DEFAULT 1;

Expand All @@ -25,11 +20,6 @@ CREATE TABLE IF NOT EXISTS portions (
CHECK ( user_created = 0 OR language_id IS NOT NULL )
);

--Default portions
INSERT INTO portions(id, name, weigth_in_grams, volume_in_ml, owner_id, user_created, language_id)
VALUES (1, 'g', 1, NULL, NULL, FALSE, NULL),
(2, 'ml', NULL, 1, NULL, FALSE, NULL);

ALTER TABLE items
ADD COLUMN language_id INTEGER REFERENCES languages NOT NULL DEFAULT 1;

Expand Down
16 changes: 15 additions & 1 deletion sqlite/seed.sql
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
--TODO move all initial non-user-created data here
-- languages
INSERT INTO languages(name, native_name)
VALUES ('en', 'English'),
('hu', 'magyar');

-- portions
INSERT INTO portions(name, owner_id, language_id, liquid, weigth_in_grams, volume_in_ml)
VALUES ('g', NULL, NULL, FALSE, 1, NULL),
('ml', NULL, NULL, TRUE, NULL, 1),
('dkg', NULL, NULL, FALSE, 10, NULL),
('cl', NULL, NULL, TRUE, NULL, 10),
('dl', NULL, NULL, TRUE, NULL, 100);

-- items
-- TODO
68 changes: 68 additions & 0 deletions sqlite/seed_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-- users
INSERT INTO users(id, language_id)
VALUES (1, 1),
(2, 2);

-- meals
INSERT INTO meals(id, owner_id, notification_id, name, time, calories_quota, fats_quota, fats_saturated_quota, carbs_quota, carbs_sugar_quota,
carbs_slow_release_quota, carbs_fast_release_quota, proteins_quota, salt_quota, archived)
VALUES (1, 1, NULL, 'Breakfast', '08:00', 500, 20, 10, 50, 10, 20, 20, 20, 5, FALSE),
(2, 1, NULL, 'Lunch', '12:00', 800, 30, 15, 80, 20, 40, 40, 30, 5, FALSE),
(3, 1, NULL, 'Dinner', '18:00', 700, 25, 12, 70, 15, 30, 30, 25, 5, FALSE),
(4, 2, NULL, 'Reggeli', '08:00', 500, 20, 10, 50, 10, 20, 20, 20, 5, FALSE),
(5, 2, NULL, 'Ebéd', '12:00', 800, 30, 15, 80, 20, 40, 40, 30, 5, FALSE),
(6, 2, NULL, 'Vacsora', '18:00', 700, 25, 12, 70, 15, 30, 30, 25, 5, FALSE);

-- languages
INSERT INTO languages(id, name, native_name)
VALUES (1, 'en', 'English'),
(2, 'hu', 'magyar');

-- portions
INSERT INTO portions(id, name, owner_id, language_id, liquid, weigth_in_grams, volume_in_ml)
VALUES (1, 'g', NULL, NULL, FALSE, 1, NULL),
(2, 'ml', NULL, NULL, TRUE, NULL, 1),
(3, 'dkg', NULL, NULL, FALSE, 10, NULL),
(4, 'cl', NULL, NULL, TRUE, NULL, 10),
(5, 'dl', NULL, NULL, TRUE, NULL, 100),
(6, 'piece', 1, 1, FALSE, 50, NULL),
(7, 'darab', 2, 2, FALSE, 50, NULL),
(8, 'cup', 1, 1, TRUE, NULL, 250),
(9, 'csésze', 2, 2, TRUE, NULL, 250);

-- items
INSERT INTO items(id, name, icon, owner_id, language_id, liquid, default_portion_id, calories_per_100, fats_per_100, fats_saturated_per_100, carbs_per_100,
carbs_sugar_per_100, carbs_slow_release_per_100, carbs_fast_release_per_100, proteins_per_100, salt_per_100)
VALUES (1, 'Apple', NULL, NULL, 1, FALSE, 6, 52, 0.2, 0.1, 14, 10, 10, 4, 0.3, 0.002),
(2, 'Alma', NULL, NULL, 2, FALSE, 7, 52, 0.2, 0.1, 14, 10, 10, 4, 0.3, 0.002),
(3, 'Banana', NULL, NULL, 1, FALSE, 6, 89, 0.3, 0.1, 23, 12, 10, 13, 1.1, 0.001),
(4, 'Banán', NULL, NULL, 2, FALSE, 7, 89, 0.3, 0.1, 23, 12, 10, 13, 1.1, 0.001),
(5, 'Bread', NULL, 1, 1, FALSE, 1, 265, 1.1, 0.2, 49, 2, 40, 9, 8.5, 0.5),
(6, 'Kenyér', NULL, 2, 2, FALSE, 1, 265, 1.1, 0.2, 49, 2, 40, 9, 8.5, 0.5),
(7, 'Butter', NULL, 1, 1, FALSE, 1, 717, 81.1, 51.4, 0.1, 0.1, 0, 0, 0.9, 1.8),
(8, 'Vaj', NULL, 2, 2, FALSE, 1, 717, 81.1, 51.4, 0.1, 0.1, 0, 0, 0.9, 1.8),
(9, 'Cheese', NULL, 1, 1, FALSE, 1, 402, 33.1, 21.1, 1.3, 0.5, 0, 0, 25, 1.6),
(10, 'Sajt', NULL, 2, 2, FALSE, 1, 402, 33.1, 21.1, 1.3, 0.5, 0, 0, 25, 1.6),
(11, 'Chicken', NULL, 1, 1, FALSE, 1, 165, 3.6, 1, 0, 0, 0, 0, 31, 0.1),
(12, 'Csirke', NULL, 2, 2, FALSE, 1, 165, 3.6, 1, 0, 0, 0, 0, 31, 0.1),
(13, 'Chocolate', NULL, 1, 1, FALSE, 1, 546, 31.3, 18.8, 59.4, 56.3, 0, 0, 5.4, 0.1),
(14, 'Csokoládé', NULL, 2, 2, FALSE, 1, 546, 31.3, 18.8, 59.4, 56.3, 0, 0, 5.4, 0.1),
(15, 'Coca Cola', NULL, 1, 1, TRUE, 4, 42, 0, 0, 10.6, 10.6, 0, 0, 0, 0.01),
(16, 'Coca Cola', NULL, 2, 2, TRUE, 4, 42, 0, 0, 10.6, 10.6, 0, 0, 0, 0.01),
(17, 'Coffee', NULL, 1, 1, TRUE, 4, 2, 0.1, 0, 0.3, 0, 0.3, 0, 0.1, 0.01),
(18, 'Kávé', NULL, 2, 2, TRUE, 4, 2, 0.1, 0, 0.3, 0, 0.3, 0, 0.1, 0.01),
(19, 'Cucumber', NULL, 1, 1, FALSE, 1, 15, 0.1, 0, 3.6, 1.7, 0, 0, 0.6, 0.002),
(20, 'Uborka', NULL, 2, 2, FALSE, 1, 15, 0.1, 0, 3.6, 1.7, 0, 0, 0.6, 0.002),
(21, 'Egg', NULL, 1, 1, FALSE, 6, 155, 11, 3.3, 1.1, 0.2, 0, 0, 13, 0.5),
(22, 'Tojás', NULL, 2, 2, FALSE, 7, 155, 11, 3.3, 1.1, 0.2, 0, 0, 13, 0.5),
(23, 'Fish', NULL, 1, 1, FALSE, 1, 206, 13.4, 3.3, 0, 0, 0, 0, 20, 0.1),
(24, 'Hal', NULL, 2, 2, FALSE, 1, 206, 13.4, 3.3, 0, 0, 0, 0, 20, 0.1);

-- items_portions_joining_table (only portions which have an owner id should have entries in the joining table; the languages should be matching on the item and the portion)
INSERT INTO items_portions_joining_table(item_id, portion_id)
VALUES (1, 6),
(2, 7),
(3, 6),
(4, 7),
(21, 6),
(22, 7);

0 comments on commit 86dd679

Please sign in to comment.