From: gamesguru Date: Sat, 26 Sep 2020 19:20:37 +0000 (-0400) Subject: wip X-Git-Tag: 0.0.0~19 X-Git-Url: https://git.nutra.tk/v2?a=commitdiff_plain;h=48973ebc4cbcb20503a9f4a24f18ae0077b109c9;p=nutratech%2Fnt-sqlite.git wip --- diff --git a/docs/nt.svg b/docs/nt.svg index d286a4b..c398c92 100644 --- a/docs/nt.svg +++ b/docs/nt.svg @@ -4,352 +4,254 @@ - - + + undefined - -nt.sqlite - + +nt.sqlite + -table_versions - -table_versions - -tablename* -text -version -int - - - -goals - -goals - -id* -integer -goal_desc -text +version + +version + +id* +integer +version +text +created +date +notes +text - + bmr_eqs - -bmr_eqs - -id* -integer -bmr_eq -text + +bmr_eqs + +id* +integer +bmr_eq +text - + bf_eqs - -bf_eqs - -id* -integer -bf_eq -text - - - -lbm_eqs - -lbm_eqs - -id* -integer -lbm_eq -text + +bf_eqs + +id* +integer +bf_eq +text - + users - -users - -id* -integer -name -text -eula -int -email -text -gender -text -dob -date -act_lvl -int -goal_id -int -goal_wt -real -goal_bf -real -bmr_id -int -bf_id -int -lbm_id -int -created -int - - - -users->goals - - + +users + +id* +integer +name +text +eula +int +gender +text +dob +date +act_lvl +int +goal_wt +real +goal_bf +real +bmr_id +int +bf_id +int +created +int - + users->bmr_eqs - - + + - -users->bf_eqs - - - - -users->lbm_eqs - - - - - -measurements - -measurements - -id* -integer -user_id -int -weight -real -height -int -wrist -real -ankle -real -chest -real -arm -real -thigh -real -calf -real -shoulders -real -waist -real -hips -real -neck -real -forearm -real -pectoral -int -abdominal -int -quadricep -int -midaxillary -int -subscapular -int -tricep -int -suprailiac -int -date -date -created -int -updated -int - - - -measurements->users - - +users->bf_eqs + + - + biometrics - -biometrics - -id* -integer -user_id -int -name -text -units -text -created -int - - - -biometrics->users - - + +biometrics + +id* +integer +name +text +unit +text +created +int - + biometric_log - -biometric_log - -id* -integer -user_id -int -biometric_id -int -value -real -created -int -updated -int + +biometric_log + +id* +integer +guid +text +user_id +int +date +date +biometric_id +int +value +real - + biometric_log->users - - + + - + biometric_log->biometrics - - + + - + recipes - -recipes - -id* -integer -name -text -user_id -int -shared -int -created -int + +recipes + +id* +integer +guid +text +user_id +int +name +text +created +int - + recipes->users - - + + - + recipe_dat - -recipe_dat - -id* -integer -recipe_id -int -food_id -int -msre_id -int -amount -real -created -int + +recipe_dat + +recipe_id +int +food_id +int +grams +real +notes +text - + recipe_dat->recipes - - - - - -recipe_serv - -recipe_serv - -id* -integer -recipe_id -int -msre_desc -text -grams -real -created -int + + - - -recipe_serv->recipes - - + + +meal_names + +meal_names + +id* +integer +name +text - + food_log - -food_log - -id* -integer -user_id -int -date -date -meal_name -text -amount -real -recipe_id -int -msre_id -int -food_id -int -created -int -updated -int + +food_log + +id* +integer +guid +text +user_id +int +date +date +meal_id +int +amount +real +recipe_id +int +food_id +int +food_msre_id +int - + food_log->users - - + + - + food_log->recipes - - + + + + + +food_log->meal_names + + + + + +rda + +rda + +user_id +int +nutr_id +int +rda +real +synced +int + + + +rda->users + + diff --git a/sql/data/biometrics.csv b/sql/data/biometrics.csv index bea9ae1..db27a05 100644 --- a/sql/data/biometrics.csv +++ b/sql/data/biometrics.csv @@ -1,30 +1,30 @@ -id,tag,name,unit,created -1,ht,Height,cm,1580336088 -2,wt,Weight,kg,1580336088 -3,wrist,Wrist,cm,1580336088 -4,ankle,Ankle,cm,1580336088 -5,chest,Chest,cm,1580336088 -6,arm,Upper Arm,cm,1580336088 -7,thigh,Thigh,cm,1580336088 -8,calf,Calf,cm,1580336088 -9,shoulders,Shoulders,cm,1580336088 -10,waist,Waist,cm,1580336088 -11,hips,Hips,cm,1580336088 -12,neck,Neck,cm,1580336088 -13,forearm,Forearm,cm,1580336088 -14,pectoral,Pectoral,mm,1580336088 -15,abdominal,Abdominal,mm,1580336088 -16,quadricep,Quadricep,mm,1580336088 -17,midaxillary,Midaxillary,mm,1580336088 -18,subscapular,Subscapular,mm,1580336088 -19,tricep,Tricep,mm,1580336088 -20,suprailiac,Suprailiac,mm,1580336088 -21,custom_bf,Body Fat,,1580336088 -22,pulse,Pulse,bpm,1580336088 -23,systolic,Systolic Blood Pressure,mmHg,1580336088 -24,diastolic,Diastolic Blood Pressure,mmHg,1580336088 -25,glucose,Blood Glucose,mg/dL,1580336088 -26,cholest_tot,Total Cholesterol,mg/dL,1580336088 -27,cholest_ldl,LDL Cholesterol,mg/dL,1580336088 -28,cholest_hdl,HDL Cholesterol,mg/dL,1580336088 -29,triglycerides,Triglycerides,mg/dL,1580336088 +id,name,unit,created +1,Height,cm,1580336088 +2,Weight,kg,1580336088 +3,Wrist,cm,1580336088 +4,Ankle,cm,1580336088 +5,Chest,cm,1580336088 +6,Upper Arm,cm,1580336088 +7,Thigh,cm,1580336088 +8,Calf,cm,1580336088 +9,Shoulders,cm,1580336088 +10,Waist,cm,1580336088 +11,Hips,cm,1580336088 +12,Neck,cm,1580336088 +13,Forearm,cm,1580336088 +14,Pectoral,mm,1580336088 +15,Abdominal,mm,1580336088 +16,Quadricep,mm,1580336088 +17,Midaxillary,mm,1580336088 +18,Subscapular,mm,1580336088 +19,Tricep,mm,1580336088 +20,Suprailiac,mm,1580336088 +21,Body Fat (Custom),,1580336088 +22,Pulse,bpm,1580336088 +23,Systolic Blood Pressure,mmHg,1580336088 +24,Diastolic Blood Pressure,mmHg,1580336088 +25,Blood Glucose,mg/dL,1580336088 +26,Total Cholesterol,mg/dL,1580336088 +27,LDL Cholesterol,mg/dL,1580336088 +28,HDL Cholesterol,mg/dL,1580336088 +29,Triglycerides,mg/dL,1580336088 diff --git a/sql/data/food_log.csv b/sql/data/food_log.csv new file mode 100644 index 0000000..8c69c02 --- /dev/null +++ b/sql/data/food_log.csv @@ -0,0 +1,23 @@ +id,guid,user_id,date,meal_id,amount,food_id,food_msre_id +1,365e2f316ea39c578a63ec0f59c0d0f0,1,2020-09-20,1,100,13047, +2,71d5d8cbbc591d22cb210ae6323249a1,1,2020-09-20,1,28,1270, +3,295f1cd568fc03b760ff9a3a985c4658,1,2020-09-21,1,55,9038, +4,30bb19b8d57492fee6cb1ebbdc9196a,1,2020-09-21,1,20,11251, +5,d2e64b1da42c2e12bd3dc9f029fb1bb5,1,2020-09-22,1,35,11529, +6,b169e76aa7ccd833a3f7d0e1a5f6935b,1,2020-09-22,1,15,11282, +7,2c53dca9604ea742c39184201d41eed4,1,2020-09-23,1,210,11828, +8,edad78872ae5d7e2a444756a336dce40,1,2020-09-23,1,40,28313, +9,c78bb27a7c756763d10d4bb7f860ba23,1,2020-09-24,1,100,9112, +10,490ffdd0063fcfc79ce5f825a7f2d370,1,2020-09-24,2,140,20137, +11,7fe9a33f24010f1738ddf73010041ced,1,2020-09-25,2,100,5062, +12,a54ddeafde280fd76a0cbbd63b9539e7,1,2020-09-25,2,45,12136, +13,6dfcfbbd5d0cb563343260d287cbdff5,1,2020-09-26,2,50,11821, +14,4704b0288bf626799dc2c09cbb75f28,1,2020-09-26,2,15,44005, +15,7053ec55502f3dccccf16d59c2a38873,1,2020-09-27,3,150,20545, +16,8f7d5caef9dac0d7102a4d90ae9f36c8,1,2020-09-27,3,85,16146, +17,79949cb0b9914d63892b89d91a4a4ad0,1,2020-09-28,3,40,1270, +18,85a4ab5a6da9fa84285dece0f2c7fa2a,1,2020-09-28,3,60,9037, +19,16f98e0382dece4799dd41325b292c70,1,2020-09-29,3,100,15076, +20,bcab83a1676a3ac8f6aadd63dd032e06,1,2020-09-29,3,60,11090, +21,44aa12a8695bfd84a513e24bb65896d9,1,2020-09-30,3,35,11938, +22,252045bc8ba3f133d4006d82a467b053,1,2020-09-30,3,25,11282, diff --git a/sql/data/meal_names.csv b/sql/data/meal_names.csv new file mode 100644 index 0000000..94770b5 --- /dev/null +++ b/sql/data/meal_names.csv @@ -0,0 +1,11 @@ +id,name +1,BREAKFAST +2,LUNCH +3,DINNER +4,SNACK +5,MEAL 2 +6,MEAL 4 +7,MEAL 6 +8,SNACK 2 +9,SNACK 3 +10,BRUNCH diff --git a/sql/data/rda.csv b/sql/data/rda.csv new file mode 100644 index 0000000..117fe94 --- /dev/null +++ b/sql/data/rda.csv @@ -0,0 +1,80 @@ +user_id,nutr_id,rda,synced +3,203,18,1 +3,204,25,1 +3,205,60,1 +3,208,501,1 +3,269,15,1 +3,291,9,1 +3,301,300,1 +3,303,4,1 +3,304,100,1 +3,305,250,1 +3,306,1300,1 +3,307,350,1 +3,309,3,1 +3,312,0.2,1 +3,315,0.6,1 +3,317,15,1 +3,318,1000,1 +3,320,250,1 +3,324,100,1 +3,328,5,1 +3,337,900,1 +3,338,2000,1 +3,401,30,1 +3,404,0.3,1 +3,405,0.35,1 +3,406,4,1 +3,410,1.25,1 +3,415,0.4,1 +3,417,100,1 +3,418,0.6,1 +3,421,110,1 +3,430,30,1 +3,501,0.1,1 +3,502,0.3,1 +3,503,0.5,1 +3,504,1,1 +3,505,0.8,1 +3,506,0.3,1 +3,508,0.3,1 +3,509,0.4,1 +3,510,0.5,1 +3,511,0.3,1 +3,512,0.2,1 +3,513,0.3,1 +3,514,0.3,1 +3,515,0.5,1 +3,516,0.2,1 +3,517,0.2,1 +3,518,0.4,1 +3,601,50,1 +3,605,0,1 +3,606,7,1 +3,621,0.1,1 +3,629,0.05,1 +3,645,10,1 +3,646,6,1 +3,710,8,1 +3,711,4,1 +3,713,11,1 +3,731,5,1 +3,734,10,1 +3,735,5,1 +3,736,5,1 +3,737,2,1 +3,738,1,1 +3,749,10,1 +3,750,10,1 +3,751,10,1 +3,752,10,1 +3,753,15,1 +3,759,20,1 +3,762,15,1 +3,770,8,1 +3,773,5,1 +3,785,5,1 +3,786,1.5,1 +3,788,0.5,1 +3,789,5,1 +3,851,0.3,1 diff --git a/sql/data/recipe_dat.csv b/sql/data/recipe_dat.csv new file mode 100644 index 0000000..2f62aa2 --- /dev/null +++ b/sql/data/recipe_dat.csv @@ -0,0 +1,39 @@ +recipe_id,food_id,amount,notes +1,20045,180,white rice +1,16042,25,pinto beans +1,11282,45,onions +1,11260,45,mushrooms +1,11821,35,bell peppers +1,11233,25,kale +1,23293,85,"beef (grass-fed, 85/15)" +1,11529,40,tomatoes +1,9037,40,avocados +1,1009,20,cheese (cheddar) +2,18351,55,roll (mixed-grain) +2,23293,85,"beef (grass-fed, 85/15)" +2,1009,20,cheddar cheese +2,11251,25,lettuce (romaine) +2,11529,40,tomatoes +2,11282,20,onions +2,9037,40,avocados +3,11355,300,potatoes (red) +3,4053,30,olive oil +3,11297,30,parsley (fresh) +4,20011,60,flour (buckwheat) +4,20140,30,flour (spelt) +4,20080,30,flour (whole wheat) +4,1123,56,egg +4,1079,244,milk (2%) +4,19911,25,syrup (maple) +4,16122,20,protein (soy or whey) +4,2047,1.5,salt +4,18372,1.5,baking soda +4,18370,0.75,baking powder +5,12061,50,almonds +5,12220,30,flaxseed +5,12012,20,hemp (seed/protein) +5,16122,28,protein (soy or whey) +5,9050,50,blueberries +5,9040,80,bananas +5,9176,40,mangos +5,1289,140,kefir diff --git a/sql/data/recipes.csv b/sql/data/recipes.csv new file mode 100644 index 0000000..1cabfd0 --- /dev/null +++ b/sql/data/recipes.csv @@ -0,0 +1,6 @@ +id,guid,created,name +1,72217324e92840852242c165aa778a67,1601235568,Burrito bowl (Everyday) +2,21613decf46873f07027886907d2a4ad,1601235568,"Burger (Grass fed, Beef)" +3,b4d0016ca8d8d0c2cbc9aa715944637f,1601235568,Baked potato wedges +4,fe653c350987bffbca2a738889009e4f,1601235568,Buckwheat pancake (w/ syrup) +5,8359446f074f8ee8ed3f26b4b3c66b3f,1601235568,Blueberry-hemp Smoothie diff --git a/sql/data/users.csv b/sql/data/users.csv index 7fe04d8..d0356e2 100644 --- a/sql/data/users.csv +++ b/sql/data/users.csv @@ -1,4 +1,4 @@ -id,name,eula,email,gender,dob,act_lvl,goal_wt,goal_bf,bmr_id,bf_id,created -1,Shane,1,nutratracker@gmail.com,MALE,1993-01-01,2,78,0.12,1,1,1601067725 -2,Mark,1,kinomark6@gmail.com,MALE,1957-01-01,2,72,0.14,1,1,1601068574 -3,Rocky,1,,DOG,2007-08-01,2,7.3,,1,1,1601068718 +id,name,guid,created,eula,gender,dob,act_lvl,goal_wt,goal_bf,bmr_id,bf_id +1,Shane,a0fdac7ab369de43f029a460879c854f,1601067725,1,MALE,1993-01-01,2,78,0.12,1,1 +2,Mark,7abf05c84beb849628a44336acfe7a5b,1601068574,1,MALE,1957-01-01,2,72,0.14,1,1 +3,Rocky,2454c8ab7c575cd2610f9977ed3b2b61,1601068718,1,DOG,2007-08-01,2,7.3,,1,1 diff --git a/sql/import.sql b/sql/import.sql index 5f402c9..ea61145 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -18,9 +18,16 @@ .import '| tail -n +2 ./data/bmr_eqs.csv' bmr_eqs .import '| tail -n +2 ./data/bf_eqs.csv' bf_eqs +.import '| tail -n +2 ./data/meal_names.csv' meal_names .import '| tail -n +2 ./data/biometrics.csv' biometrics .import '| tail -n +2 ./data/users.csv' users +.import '| tail -n +2 ./data/rda.csv' rda + +.import '| tail -n +2 ./data/recipes.csv' recipes +.import '| tail -n +2 ./data/recipe_dat.csv' recipe_dat + +.import '| tail -n +2 ./data/food_log.csv' food_log .header on .mode column diff --git a/sql/tables.sql b/sql/tables.sql index 24095be..1e21bb8 100644 --- a/sql/tables.sql +++ b/sql/tables.sql @@ -16,10 +16,6 @@ PRAGMA foreign_keys = 1; -CREATE TABLE test ( - id blob text UNIQUE -); - CREATE TABLE version( id integer PRIMARY KEY AUTOINCREMENT, version text NOT NULL, created date NOT NULL, notes text ); @@ -28,7 +24,7 @@ INSERT INTO version(version, created, notes) -- --------------------------------- --- Goals, equations, & statics +-- Equations --------------------------------- CREATE TABLE bmr_eqs ( @@ -38,7 +34,7 @@ CREATE TABLE bmr_eqs ( CREATE TABLE bf_eqs ( id integer PRIMARY KEY AUTOINCREMENT, - bf_eq text DEFAULT 'NAVY' + bf_eq text ); -- @@ -49,8 +45,9 @@ CREATE TABLE bf_eqs ( CREATE TABLE users ( id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL UNIQUE, + guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, + created int DEFAULT (strftime ('%s', 'now')), eula int DEFAULT 0, - email text UNIQUE, gender text, dob date, act_lvl int DEFAULT 2, -- [1, 2, 3, 4, 5] @@ -58,7 +55,6 @@ CREATE TABLE users ( goal_bf real, bmr_id int DEFAULT 1, bf_id int DEFAULT 1, - created int DEFAULT (strftime ('%s', 'now')), FOREIGN KEY (bmr_id) REFERENCES bmr_eqs (id) ON UPDATE CASCADE, FOREIGN KEY (bf_id) REFERENCES bf_eqs (id) ON UPDATE CASCADE ); @@ -69,8 +65,8 @@ CREATE TABLE users ( -------------------------------- CREATE TABLE biometrics ( + -- TODO: support custom biometrics and sync? id integer PRIMARY KEY AUTOINCREMENT, - tag text NOT NULL UNIQUE, name text NOT NULL UNIQUE, unit text, created int DEFAULT (strftime ('%s', 'now')) @@ -78,11 +74,11 @@ CREATE TABLE biometrics ( CREATE TABLE biometric_log ( id integer PRIMARY KEY AUTOINCREMENT, + guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, user_id int NOT NULL, date date DEFAULT CURRENT_DATE, biometric_id int NOT NULL, value real NOT NULL, - UNIQUE (user_id, date, biometric_id), FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE, FOREIGN KEY (biometric_id) REFERENCES biometrics (id) ON UPDATE CASCADE ); @@ -94,33 +90,24 @@ CREATE TABLE biometric_log ( CREATE TABLE recipes ( id integer PRIMARY KEY AUTOINCREMENT, - name text NOT NULL, - shared int DEFAULT 1, - created int DEFAULT (strftime ('%s', 'now')) -); - -CREATE TABLE recipe_serv ( - id integer PRIMARY KEY AUTOINCREMENT, - recipe_id int NOT NULL, - msre_desc text NOT NULL, - grams real NOT NULL, - FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE + guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, + created int DEFAULT (strftime ('%s', 'now')), + name text NOT NULL ); CREATE TABLE recipe_dat ( recipe_id int NOT NULL, -- TODO: enforce FK constraint across two DBs? food_id int NOT NULL, - msre_id int NOT NULL, - amount real NOT NULL, + grams real NOT NULL, + notes text, UNIQUE (recipe_id, food_id), - FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE, - FOREIGN KEY (msre_id) REFERENCES recipe_serv (id) ON UPDATE CASCADE + FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE ); -- -------------------------------- --- Food logs +-- Food (and recipe) logs -------------------------------- CREATE TABLE meal_names ( @@ -128,26 +115,32 @@ CREATE TABLE meal_names ( name text NOT NULL ); -INSERT INTO meal_names (name) - VALUES ('BREAKFAST'), ('LUNCH'), ('DINNER'), ('SNACK'); - CREATE TABLE food_log ( id integer PRIMARY KEY AUTOINCREMENT, + guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, user_id int, date date DEFAULT CURRENT_DATE, meal_id int, - amount real NOT NULL, - recipe_id int, - rec_msre_id int, + amount real NOT NULL, -- grams, if `food_msre_id` IS NULL -- TODO: enforce FK constraint across two DBs? + food_id int, food_msre_id int, - UNIQUE (user_id, date, meal_id, recipe_id), - UNIQUE (user_id, date, meal_id, food_id), + FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE, + FOREIGN KEY (meal_id) REFERENCES meal_names (id) ON UPDATE CASCADE +); + +CREATE TABLE recipe_log ( + id integer PRIMARY KEY AUTOINCREMENT, + guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE, + user_id int, + date date DEFAULT CURRENT_DATE, + meal_id int, + grams real NOT NULL, + recipe_id int, FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE, FOREIGN KEY (meal_id) REFERENCES meal_names (id) ON UPDATE CASCADE, - FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE, - FOREIGN KEY (rec_msre_id) REFERENCES recipe_serv (id) ON UPDATE CASCADE + FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE ); -- @@ -156,11 +149,34 @@ CREATE TABLE food_log ( -------------------------------- CREATE TABLE rda ( - id integer PRIMARY KEY, - user_id integer NOT NULL, + user_id int NOT NULL, -- TODO: enforce FK constraint across two DBs? - nutr_id integer NOT NULL, + nutr_id int NOT NULL, rda real NOT NULL, + synced int DEFAULT 0, + UNIQUE (user_id, nutr_id), FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE ); +CREATE TRIGGER rda_sync + AFTER UPDATE OF rda ON rda +BEGIN + UPDATE rda SET synced = 0 +WHERE + NEW.user_id = user_id AND NEW.nutr_id = nutr_id; + +END; + +-- +-------------------------------- +-- Custom RDAs +-------------------------------- + +CREATE TABLE sync_data ( + id integer PRIMARY KEY AUTOINCREMENT, + tablename text NOT NULL, + guid text, + `constraint` text, -- e.g. "(a, b)" in "UNIQUE (a, b)" or "ON CONFLICT (a, b) DO ..." + action text NOT NULL -- insert, delete, update +); +