From: Shane Jaroch Date: Mon, 11 Jul 2022 16:04:03 +0000 (-0400) Subject: rename sqlite3, remove some CSV, drop biometrics (#9) X-Git-Tag: 0.0.5~1 X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=bd445909c0e6165e82d6f077b3160cf2c07f4aa4;p=nutratech%2Fnt-sqlite.git rename sqlite3, remove some CSV, drop biometrics (#9) --- diff --git a/.editorconfig b/.editorconfig index 30b02fd..ba6a1e8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,15 +6,19 @@ indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true -insert_final_newline = true max_line_length = 100 -[*.{py}] +[*.py] max_line_length = 88 +[*.sh] +indent_style = tab +max_line_length = 79 + + [*.sql] indent_size = 2 max_line_length = 80 diff --git a/.gitattributes b/.gitattributes index 0242374..7533c02 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ *.sql linguist-detectable - diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b2cb26..b77c00a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,44 +9,55 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Replace grams with `msre_id` and `amt` in `food_log` table +- Replace grams with `msre_id` and `amt` in `food_log` table +- Rename to use `.sqlite3` extension, instead of `nt.sqlite` + +### Development + +- Enhance lint tools +- Use `__main__.py` & `python -m sql` to package and build sqlite3 file + +### Added + +- SQL tables `bug` and `msg`, for bug reports & pushed message queue ## [0.0.4] - 2021-06-17 ### Added -- Empty `__init__.py` file to make packagable with `cli` repo (as a `git submodule`) -- Tables `custom_foods` and respective `cf_dat` +- Empty `__init__.py` file to make packageable with `cli` repo (as a `git submodule`) +- SQL table `custom_foods` and respective `cf_dat` ### Changed -- Build with `python sql/__init__.py` (removed root-level script `build.py`) -- Rename pre-populted `meals` table to `meal_name` +- Build with `python sql/__init__.py` (removed root-level script `build.py`) +- Rename pre-populated `meals` table to `meal_name` ### Fixed -- Slight bash inconvenience when using `export.sh` +- Slight bash inconvenience when using `export.sh` ### Removed -- `food_costs` table +- `food_costs` table +- `biometrics` ad related tables ## [0.0.3] - 2021-05-24 ### Added -- More dummy CSV data (not production ready) -- `tagname` column to `recipes` table +- More dummy CSV data (not production ready) +- `tagname` column to `recipes` table ### Changed -- `date` column uses `INT` type now, instead of `date` -- Drop `created` and `updated` fields off of `rda` table, add them to `biometrics` +- `date` column uses `INT` type now, instead of `date` +- Drop `created` and `updated` fields off of `rda` table, add them to `biometrics` ### Removed -- `last_sync` column (future release? Feature is planned, but not alloted) -- `guid` parameter from `functions.sql` +- `last_sync` column (future release? Feature is planned, but not allotted) +- `guid` parameter from `functions.sql` ## [0.0.2] - 2021-05-24 @@ -56,29 +67,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed -- `guid` columns (may return in a future release) +- `guid` columns (may return in a future release) ## [0.0.1] - 2021-05-21 ### Added -- Version table -- Travis CI configuration file `.travis.yml` -- `functions.sql` for use in Python client -- [TODO] Placeholder for initial upgrade script (`sql/upgrade_scripts/0.0.1.sql`) -- `food_costs` table (lone table, functionality not yet implemented) +- Version table +- Travis CI configuration file `.travis.yml` +- `functions.sql` for use in Python client +- [TODO] Placeholder for initial upgrade script (`sql/upgrade_scripts/0.0.1.sql`) +- `food_costs` table (lone table, functionality not yet implemented) ### Changed -- Use Python for import script (`build.py` replaced `import.sql`) +- Use Python for import script (`build.py` replaced `import.sql`) ## [0.0.0] - 2020-09-22 ### Added -- Initial release of table schema design -- Import script `import.sql` (SQL) -- Export script `export.sh` (Shell) -- Rudimentary dummy data in CSV files +- Initial release of table schema design +- Import script `import.sql` (SQL) +- Export script `export.sh` (Shell) +- Rudimentary dummy data in CSV files (e.g. `food_log.csv`, `biometric_log.csv`, `profiles.csv`) -- Database diagram generated via `docs/sqleton.sh` +- Database diagram generated via `docs/sqleton.sh` diff --git a/Makefile b/Makefile index 5bb2110..c52a3af 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ _help: .PHONY: clean clean: ## Clean up build intermediates - rm -f sql/nt.sqlite + rm -f sql/nt.sqlite3 rm -rf .mypy_cache/ .pytest_cache/ find sql/ -name __pycache__ -o -name .pytest_cache | xargs rm -rf @@ -20,14 +20,14 @@ build: ## Build sqlite image .PHONY: test test: ## Cursory sanity check sqlite3 -csv -header \ - sql/nt.sqlite \ + sql/nt.sqlite3 \ 'SELECT * FROM bf_eqs;' \ - 'SELECT * FROM biometrics;' \ 'SELECT * FROM bmr_eqs;' \ 'SELECT * FROM meal_name;' \ 'SELECT * FROM version;' \ .PHONY: install install: ## Copy sqlite file into ~/.nutra + # TODO: does this respect if the file exists already? mkdir -p ~/.nutra - cp sql/nt.sqlite ~/.nutra + cp sql/nt.sqlite3 ~/.nutra diff --git a/README.rst b/README.rst index 3dd58c8..5d3a2b7 100644 --- a/README.rst +++ b/README.rst @@ -11,47 +11,44 @@ See CLI: https://github.com/nutratech/cli Pypi page: https://pypi.org/project/nutra - Building the database ######################### -1. If you are committing database changes, add a line to ``data/version.csv`` - (e.g. ``id=4`` is the latest in this case), - -+-----+----------+-------------+------------------+ -| id | version | created | notes | -+=====+==========+=============+==================+ -| 1 | 0.0.0 | 2020-09-22 | initial release | -+-----+----------+-------------+------------------+ -| 2 | 0.0.1 | 2021-05-21 | bump version | -+-----+----------+-------------+------------------+ -| 3 | 0.0.2 | 2021-05-24 | remove guids | -+-----+----------+-------------+------------------+ -| 4 | 0.0.3 | 2021-05-24 | general cleanup | -+-----+----------+-------------+------------------+ +Create the database with. -2. Create the database with, +:: -.. code-block:: bash + make build - make +Verify the tables were populated and exist. -3. Verify the tables were populated and exist, - -.. code-block:: bash +:: make test -4. If everything looks good, commit and update submodules in the - ``cli`` (python) and ``nt-android`` (java) repos. +If everything looks good: commit, and update submodules in the ``cli`` repo. + +.. important:: If you are committing database changes, add a line to + ``sql/data/version.csv`` (``id=4`` is the latest in this case). + +-----+----------+-------------+------------------+ + | id | version | created | notes | + +=====+==========+=============+==================+ + | 1 | 0.0.0 | 2020-09-22 | initial release | + +-----+----------+-------------+------------------+ + | 2 | 0.0.1 | 2021-05-21 | bump version | + +-----+----------+-------------+------------------+ + | 3 | 0.0.2 | 2021-05-24 | remove guids | + +-----+----------+-------------+------------------+ + | 4 | 0.0.3 | 2021-05-24 | general cleanup | + +-----+----------+-------------+------------------+ Tables (Relational Design) ########################## -Note: functions are kept in ``sql/functions.sql``. +**Note:** functions are kept in ``sql/functions.sql``. -See ``sql/tables.sql`` for details. +See ``sql/tables.sql`` for details on design. This is frequently updated, see ``docs/`` for more info. diff --git a/__init__.py b/__init__.py index e69de29..c86693f 100644 --- a/__init__.py +++ b/__init__.py @@ -0,0 +1,4 @@ +""" +NOTE: this is used as a submodule in the cli repo (ntclient) + so we need this to all be packagable, e.g. have __init__.py files +""" diff --git a/docs/nt.svg b/docs/nt.svg index 4f1af30..98c75f1 100644 --- a/docs/nt.svg +++ b/docs/nt.svg @@ -4,351 +4,344 @@ - - + + undefined - -sql/nt.sqlite + +sql/nt.sqlite3 version - -version - -id* -integer -version -text -created -date -notes -text + +version + +id* +integer +version +text +created +date +notes +text bmr_eqs - -bmr_eqs - -id* -integer -name -text + +bmr_eqs + +id* +integer +name +text bf_eqs - -bf_eqs - -id* -integer -name -text + +bf_eqs + +id* +integer +name +text profiles - -profiles - -id* -integer -name -text -gender -text -dob -date -act_lvl -int -goal_wt -real -goal_bf -real -bmr_eq_id -int -bf_eq_id -int -created -int + +profiles + +id* +integer +name +text +gender +text +dob +date +act_lvl +int +goal_wt +real +goal_bf +real +bmr_eq_id +int +bf_eq_id +int +created +int profiles->bmr_eqs - - + + profiles->bf_eqs - - - - - -biometrics - -biometrics - -id* -integer -name -text -unit -text -created -int - - - -biometric_log - -biometric_log - -id* -integer -profile_id -int -date -int -tags -text -notes -text -created -int - - - -biometric_log->profiles - - - - - -bio_log_entry - -bio_log_entry - -log_id* -int -biometric_id* -int -value -real - - - -bio_log_entry->biometrics - - - - - -bio_log_entry->biometric_log - - + + - + recipes - -recipes - -id* -integer -tagname -text -name -text -created -int + +recipes + +id* +integer +tagname +text +name +text +created +int - + recipe_dat - -recipe_dat - -recipe_id* -int -food_id* -int -grams -real -notes -text -created -int + +recipe_dat + +recipe_id* +int +food_id* +int +grams +real +notes +text +created +int - + recipe_dat->recipes - - + + - + custom_foods - -custom_foods - -id* -integer -tagname -text -name -text -created -int + +custom_foods + +id* +integer +tagname +text +name +text +created +int - + cf_dat - -cf_dat - -cf_id* -int -nutr_id* -int -nutr_val -real -notes -text -created -int + +cf_dat + +cf_id* +int +nutr_id* +int +nutr_val +real +notes +text +created +int - + cf_dat->custom_foods - - + + - + meal_name - -meal_name - -id* -integer -name -text + +meal_name + +id* +integer +name +text - + food_log - -food_log - -id* -integer -profile_id -int -date -int -meal_id -int -food_id -int -msre_id -int -amt -real -created -int + +food_log + +id* +integer +profile_id +int +date +int +meal_id +int +food_id +int +msre_id +int +amt +real +created +int - + food_log->profiles - - + + - + food_log->meal_name - - + + - + recipe_log - -recipe_log - -id* -integer -profile_id -int -date -int -meal_id -int -recipe_id -int -grams -real -created -int + +recipe_log + +id* +integer +profile_id +int +date +int +meal_id +int +recipe_id +int +grams +real +created +int - + recipe_log->profiles - - + + - + recipe_log->recipes - - + + - + recipe_log->meal_name - - + + - + rda - -rda - -profile_id* -int -nutr_id* -int -rda -real + +rda + +profile_id* +int +nutr_id* +int +rda +real - + rda->profiles - - + + - - -food_costs - -food_costs - -food_id* -int -profile_id* -int -cost -real + + +food_cost + +food_cost + +food_id* +int +profile_id* +int +cost +real - - -food_costs->profiles - - + + +food_cost->profiles + + + + + +bug + +bug + +id* +integer +profile_id +int +created +int +arguments +text +stack +text +os +text +py_ver +text +user_details +text +submitted +tinyint + + + +bug->profiles + + + + + +msg + +msg + +id* +integer +profile_id +int +msg_id +int +created +int +received +int +header +text +body +text + + + +msg->profiles + + diff --git a/docs/sqleton.sh b/docs/sqleton.sh index cecf1b6..64e7d21 100755 --- a/docs/sqleton.sh +++ b/docs/sqleton.sh @@ -1,6 +1,12 @@ #!/bin/bash -e +set -x + cd "$(dirname "$0")" cd .. -sqleton -o docs/nt.svg sql/nt.sqlite +SQLITE3_FILE=sql/nt.sqlite3 + +test -f $SQLITE3_FILE + +sqleton -o docs/nt.svg $SQLITE3_FILE diff --git a/sql/__init__.py b/sql/__init__.py index 29f89fe..dfbe034 100755 --- a/sql/__init__.py +++ b/sql/__init__.py @@ -1,16 +1,17 @@ #!/usr/bin/env python3 -"""Main module for building nt.sqlite""" +"""Main module for building nt.sqlite3""" import csv import os import sqlite3 -NT_DB_NAME = "nt.sqlite" +NT_DB_NAME = "nt.sqlite3" SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) +CSV_DATA_DIR = os.path.join(SCRIPT_DIR, "data") def build_ntsqlite(verbose=False) -> bool: - """Builds and inserts stock data into nt.sqlite""" + """Builds and inserts stock data into nt.sqlite3""" # cd into this script's directory os.chdir(SCRIPT_DIR) @@ -32,7 +33,7 @@ def build_ntsqlite(verbose=False) -> bool: if verbose: print("-> Populate data") - for file_path in os.listdir("data"): + for file_path in os.listdir(CSV_DATA_DIR): if not file_path.endswith(".csv"): continue table_name = os.path.splitext(os.path.basename(file_path))[0] diff --git a/sql/data/bio_log_entry.csv b/sql/data/bio_log_entry.csv deleted file mode 100644 index 0490aa4..0000000 --- a/sql/data/bio_log_entry.csv +++ /dev/null @@ -1,132 +0,0 @@ -log_id,biometric_id,value -1,22,61 -1,23,100 -1,24,54 -2,22,68 -2,23,93 -2,24,54 -3,22,62 -3,23,103 -3,24,50 -4,22,66 -4,23,99 -4,24,58 -5,22,82 -5,23,115 -5,24,63 -6,22,0 -6,23,104 -6,24,59 -7,22,70 -7,23,108 -7,24,63 -8,22,59 -8,23,112 -8,24,61 -9,22,62 -9,23,108 -9,24,65 -10,1,177 -10,2,67 -10,3,17.2 -10,4,21.6 -10,5,93.345 -10,6,28.575 -10,7,50.8 -10,8,35.56 -10,9,109.22 -10,10,78.74 -10,11,91.44 -10,12,38.1 -10,13,26.67 -10,14,10 -10,15,15 -10,16,14 -10,17,13 -10,18,10 -10,19,12 -10,20,9 -11,22,53 -11,23,91 -11,24,60 -12,22,56 -12,23,101 -12,24,63 -13,22,64 -13,23,105 -13,24,62 -14,1,33 -14,2,8.71 -14,5,24.13 -14,6,10.795 -14,9,50.8 -14,10,46.355 -14,12,31.75 -15,1,179 -15,2,74 -15,3,17.2 -15,4,21.5 -15,5,100.965 -15,6,28.575 -15,7,55.245 -15,8,37.465 -15,9,115.57 -15,10,86.995 -15,11,95.25 -15,12,38.1 -15,13,26.67 -15,14,19 -15,15,34 -15,16,14 -15,17,18 -15,18,12 -15,19,13 -15,20,15 -16,22,59 -16,23,103 -16,24,63 -17,22,53 -17,23,107 -17,24,66 -18,2,73 -18,14,18 -18,15,26 -18,16,15 -19,2,7.26 -20,22,61 -20,23,103 -20,24,65 -21,22,97 -21,23,110 -21,24,71 -22,22,62 -22,23,106 -22,24,68 -23,22,59 -23,23,110 -23,24,66 -24,22,59 -24,23,92 -24,24,58 -25,2,70 -26,2,65.4 -27,2,71 -28,2,67.6 -29,2,66 -30,2,67.2 -31,22,58 -31,23,102 -31,24,61 -32,22,83 -32,23,113 -32,24,64 -33,22,78 -33,23,105 -33,24,70 -34,2,69.5 -35,2,66.2 -36,2,68.4 -37,22,66 -37,23,103 -37,24,63 -38,2,65.2 diff --git a/sql/data/biometric_log.csv b/sql/data/biometric_log.csv deleted file mode 100644 index 4b6564f..0000000 --- a/sql/data/biometric_log.csv +++ /dev/null @@ -1,39 +0,0 @@ -id,profile_id,date,tags,notes,created -1,1,Sun 28 Jun 2020 02:07:01 PM EDT,exercise,,1601977400 -2,2,Sun 28 Jun 2020 02:07:01 PM EDT,exercise,,1601977400 -3,1,Sun 28 Jun 2020 02:38:20 PM EDT,,,1601977400 -4,2,Sun 28 Jun 2020 02:38:20 PM EDT,,,1601977400 -5,1,Sun 28 Jun 2020 10:05:26 PM EDT,exercise,,1601977400 -6,2,Thu 02 Jul 2020 12:12:54 PM EDT,,doctor's office,1601977400 -7,1,Sat 04 Jul 2020 03:50:13 PM EDT,,,1601977400 -8,2,Sat 04 Jul 2020 03:54:27 PM EDT,,,1601977400 -9,2,Sun 05 Jul 2020 01:30:44 PM EDT,,,1601977400 -10,1,Mon 06 Jul 2020 06:54:49 AM EDT,,,1601977400 -11,2,Sun 12 Jul 2020 12:29:11 PM EDT,,,1601977400 -12,2,Sun 12 Jul 2020 02:44:25 PM EDT,,,1601977400 -13,1,Sun 12 Jul 2020 02:44:25 PM EDT,,,1601977400 -14,3,Mon 13 Jul 2020 03:30:46 PM EDT,,,1601977400 -15,2,Fri 17 Jul 2020 02:02:12 PM EDT,,,1601977400 -16,2,Fri 17 Jul 2020 03:14:37 PM EDT,,,1601977400 -17,1,Fri 17 Jul 2020 03:14:37 PM EDT,,,1601977400 -18,2,Sat 08 Aug 2020 01:53:25 PM EDT,,,1601977400 -19,3,Thu 27 Aug 2020 12:44:37 PM EDT,,,1601977400 -20,2,Fri 28 Aug 2020 03:40:56 PM EDT,,,1601977400 -21,1,Fri 28 Aug 2020 03:40:56 PM EDT,,,1601977400 -22,2,Sun 06 Sep 2020 11:49:47 AM EDT,,,1601977400 -23,1,Sun 06 Sep 2020 11:53:21 AM EDT,,,1601977400 -24,2,Sat 12 Sep 2020 12:15:53 PM EDT,,,1601977400 -25,2,Sun 06 Sep 2020,,,1601977400 -26,1,Sun 06 Sep 2020,,,1601977400 -27,2,Sat 12 Sep 2020,,,1601977400 -28,1,Sat 12 Sep 2020,,,1601977400 -29,1,Sun 13 Sep 2020,,,1601977400 -30,1,Mon 14 Sep 2020,,,1601977400 -31,1,Fri 18 Sep 2020 09:04:38 PM EDT,exercise,,1601977400 -32,2,Thu 24 Sep 2020 05:05:51 PM EDT,illness,,1601977400 -33,1,Thu 24 Sep 2020 05:05:51 PM EDT,,,1601977400 -34,2,Thu 24 Sep 2020,,,1601977400 -35,1,Fri 25 Sep 2020,gym scale,,1601977400 -36,2,Sat 26 Sep 2020,,,1601977400 -37,2,Sat 26 Sep 2020 11:54:17 AM EDT,,,1601977400 -38,1,Tue 29 Sep 2020 03:40:37 PM EDT,nude,,1601977400 diff --git a/sql/data/biometrics.csv b/sql/data/biometrics.csv deleted file mode 100644 index db27a05..0000000 --- a/sql/data/biometrics.csv +++ /dev/null @@ -1,30 +0,0 @@ -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 deleted file mode 100644 index e056c1e..0000000 --- a/sql/data/food_log.csv +++ /dev/null @@ -1,23 +0,0 @@ -id,profile_id,date,meal_id,food_id,msre_id,amt,created -1,1,2020-09-20,1,13047,-1,100,1601977400 -2,1,2020-09-20,1,1270,-1,28,1601977400 -3,1,2020-09-20,1,9038,-1,55,1601977400 -4,1,2020-09-20,1,11251,-1,20,1601977400 -5,1,2020-09-20,1,11529,-1,35,1601977400 -6,1,2020-09-20,1,11282,-1,15,1601977400 -7,1,2020-09-20,1,11828,-1,210,1601977400 -8,1,2020-09-20,1,28313,-1,40,1601977400 -9,1,2020-09-20,1,9112,-1,100,1601977400 -10,1,2020-09-20,2,20137,-1,140,1601977400 -11,1,2020-09-20,2,5062,-1,100,1601977400 -12,1,2020-09-20,2,12136,-1,45,1601977400 -13,1,2020-09-20,2,11821,-1,50,1601977400 -14,1,2020-09-20,2,44005,-1,15,1601977400 -15,1,2020-09-20,3,20545,-1,150,1601977400 -16,1,2020-09-20,3,16146,-1,85,1601977400 -17,1,2020-09-20,3,1270,-1,40,1601977400 -18,1,2020-09-20,3,9037,-1,60,1601977400 -19,1,2020-09-20,3,15076,-1,100,1601977400 -20,1,2020-09-20,3,11090,-1,60,1601977400 -21,1,2020-09-20,3,11938,-1,35,1601977400 -22,1,2020-09-20,3,11282,-1,25,1601977400 diff --git a/sql/data/profiles.csv b/sql/data/profiles.csv deleted file mode 100644 index 9a8cd3b..0000000 --- a/sql/data/profiles.csv +++ /dev/null @@ -1,4 +0,0 @@ -id,name,gender,dob,act_lvl,goal_wt,goal_bf,bmr_id,bf_id,created -1,Shane,MALE,1993-01-01,2,78,0.12,1,1,1601067725 -2,Mark,MALE,1957-01-01,2,72,0.14,1,1,1601068574 -3,Rocky,DOG,2007-08-01,2,7.3,,1,1,1601068718 diff --git a/sql/data/recipe_dat.csv b/sql/data/recipe_dat.csv deleted file mode 100644 index 3d36355..0000000 --- a/sql/data/recipe_dat.csv +++ /dev/null @@ -1,39 +0,0 @@ -recipe_id,food_id,grams,notes,created -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/recipe_log.csv b/sql/data/recipe_log.csv deleted file mode 100644 index fcad553..0000000 --- a/sql/data/recipe_log.csv +++ /dev/null @@ -1,2 +0,0 @@ -id,profile_id,date,meal_id,recipe_id,grams,created -1,1,2020-09-26,1,1,315,1601977400 diff --git a/sql/data/recipes.csv b/sql/data/recipes.csv deleted file mode 100644 index feb12ac..0000000 --- a/sql/data/recipes.csv +++ /dev/null @@ -1,6 +0,0 @@ -id,tagname,name,created -1,burrito-bowl,Burrito bowl (Everyday),1601235568 -2,burger,"Burger (Grass fed, Beef)",1601235568 -3,potato-wedges,Baked potato wedges,1601235568 -4,buckwheat-pancake,Buckwheat pancake (w/ syrup),1601235568 -5,blueberry-hemp-smoothie,Blueberry-hemp Smoothie,1601235568 diff --git a/sql/data/version.csv b/sql/data/version.csv index 308de8a..f5f5eb3 100644 --- a/sql/data/version.csv +++ b/sql/data/version.csv @@ -4,3 +4,4 @@ id,version,created,notes 3,0.0.2,2021-05-24,remove guids 4,0.0.3,2021-05-24,general cleanup 5,0.0.4,2021-06-17,"add custom foods tables (custom_foods, cf_dat)" +6,0.0.5,2022-07-11,remove biometrics diff --git a/sql/export.sh b/sql/export.sh index ddf5f30..c0ea144 100755 --- a/sql/export.sh +++ b/sql/export.sh @@ -2,6 +2,6 @@ cd "$(dirname "$0")" -for t in $(sqlite3 nt.sqlite '.tables'); do - sqlite3 -csv nt.sqlite "SELECT * FROM $t" > "data/$t.csv" -done +for t in $(sqlite3 nt.sqlite3 '.tables'); do + sqlite3 -csv nt.sqlite3 "SELECT * FROM $t" > "data/$t.csv" +done \ No newline at end of file diff --git a/sql/format.sh b/sql/format.sh index 47de684..ac6058e 100755 --- a/sql/format.sh +++ b/sql/format.sh @@ -1,6 +1,11 @@ -#!/bin/bash -e +#!/bin/bash -ex cd "$(dirname "$0")" -pg_format -s 2 tables.sql -o tables.sql -pg_format -s 2 functions.sql -o functions.sql +# Format +pg_format -L -s 2 -w 80 tables.sql >tables.fmt.sql +mv tables.fmt.sql tables.sql + +pg_format -L -s 2 -w 80 functions.sql >functions.fmt.sql +mv functions.fmt.sql functions.sql + diff --git a/sql/functions.sql b/sql/functions.sql index 1746314..d669b63 100644 --- a/sql/functions.sql +++ b/sql/functions.sql @@ -1,158 +1,6 @@ --------------------------------- --- Weight --------------------------------- - -SELECT - profiles.name, - date, - ROUND(bio_log_entry.value * 2.204, 1) AS weight -FROM - biometric_log - INNER JOIN profiles ON profile_id = profiles.id - INNER JOIN bio_log_entry ON biometric_id = 2 - AND log_id = biometric_log.id -WHERE - profiles.name = 'Mark'; - --------------------------------- --- Pulse and blood pressure --------------------------------- - -SELECT DISTINCT - date, - profiles.name, - tags, - notes, - CAST(sys.value AS int) || '/' || CAST(dia.value AS int) AS pressure, - CAST(pulse.value AS int) AS pulse -FROM - biometric_log - INNER JOIN profiles ON profile_id = profiles.id - INNER JOIN bio_log_entry pulse ON pulse.biometric_id = 22 - AND pulse.log_id = biometric_log.id - INNER JOIN bio_log_entry sys ON sys.biometric_id = 23 - AND sys.log_id = biometric_log.id - INNER JOIN bio_log_entry dia ON dia.biometric_id = 24 - AND dia.log_id = biometric_log.id -WHERE - profiles.name = 'Mark'; - --------------------------------- --- Height, wrist, ankle --------------------------------- - -SELECT - date, - profiles.name, - height.value AS height, - wrist.value AS wrist, - ankle.value AS ankle -FROM - biometric_log - INNER JOIN profiles ON profile_id = profiles.id - LEFT JOIN bio_log_entry height ON height.biometric_id = 1 - AND height.log_id = biometric_log.id - LEFT JOIN bio_log_entry wrist ON wrist.biometric_id = 3 - AND wrist.log_id = biometric_log.id - LEFT JOIN bio_log_entry ankle ON ankle.biometric_id = 4 - AND ankle.log_id = biometric_log.id -WHERE - height.value - OR wrist.value - OR ankle.value; - --------------------------------- --- Measurements (cm) --------------------------------- - -SELECT - date, - profiles.name, - chest.value / 2.54 AS chest, - arm.value / 2.54 AS arm, - thigh.value / 2.54 AS thigh, - calf.value / 2.54 AS calf, - shoulders.value / 2.54 AS shoulders, - waist.value / 2.54 AS waist, - hips.value / 2.54 AS hips, - neck.value / 2.54 AS neck, - forearm.value / 2.54 AS forearm -FROM - biometric_log - INNER JOIN profiles ON profile_id = profiles.id - LEFT JOIN bio_log_entry chest ON chest.biometric_id = 5 - AND chest.log_id = biometric_log.id - LEFT JOIN bio_log_entry arm ON arm.biometric_id = 6 - AND arm.log_id = biometric_log.id - LEFT JOIN bio_log_entry thigh ON thigh.biometric_id = 7 - AND thigh.log_id = biometric_log.id - LEFT JOIN bio_log_entry calf ON calf.biometric_id = 8 - AND calf.log_id = biometric_log.id - LEFT JOIN bio_log_entry shoulders ON shoulders.biometric_id = 9 - AND shoulders.log_id = biometric_log.id - LEFT JOIN bio_log_entry waist ON waist.biometric_id = 10 - AND waist.log_id = biometric_log.id - LEFT JOIN bio_log_entry hips ON hips.biometric_id = 11 - AND hips.log_id = biometric_log.id - LEFT JOIN bio_log_entry neck ON neck.biometric_id = 12 - AND neck.log_id = biometric_log.id - LEFT JOIN bio_log_entry forearm ON forearm.biometric_id = 13 - AND forearm.log_id = biometric_log.id -WHERE - chest.value - OR arm.value - OR thigh.value - OR calf.value - OR shoulders.value - OR waist.value - OR hips.value - OR neck.value - OR forearm.value; - --------------------------------- --- Skinfolds (mm) --------------------------------- - -SELECT - date, - profiles.name, - CAST(pectoral.value AS int) AS pec, - CAST(abdominal.value AS int) AS ab, - CAST(quadricep.value AS int) AS quad, - CAST(midaxillar.value AS int) AS midax, - CAST(subscapular.value AS int) AS sub, - CAST(tricep.value AS int) AS tricep, - CAST(suprailiac.value AS int) AS supra -FROM - biometric_log - INNER JOIN profiles ON profile_id = profiles.id - LEFT JOIN bio_log_entry pectoral ON pectoral.biometric_id = 14 - AND pectoral.log_id = biometric_log.id - LEFT JOIN bio_log_entry abdominal ON abdominal.biometric_id = 15 - AND abdominal.log_id = biometric_log.id - LEFT JOIN bio_log_entry quadricep ON quadricep.biometric_id = 16 - AND quadricep.log_id = biometric_log.id - LEFT JOIN bio_log_entry midaxillar ON midaxillar.biometric_id = 17 - AND midaxillar.log_id = biometric_log.id - LEFT JOIN bio_log_entry subscapular ON subscapular.biometric_id = 18 - AND subscapular.log_id = biometric_log.id - LEFT JOIN bio_log_entry tricep ON tricep.biometric_id = 19 - AND tricep.log_id = biometric_log.id - LEFT JOIN bio_log_entry suprailiac ON suprailiac.biometric_id = 20 - AND suprailiac.log_id = biometric_log.id -WHERE - pectoral.value - OR abdominal.value - OR quadricep.value - OR midaxillar.value - OR subscapular.value - OR tricep.value - OR suprailiac.value; - -------------------------------- -- Recipes overview -------------------------------- - SELECT id, tagname, diff --git a/sql/tables.sql b/sql/tables.sql index 01cdf9f..3a95eda 100644 --- a/sql/tables.sql +++ b/sql/tables.sql @@ -1,5 +1,5 @@ --- nt-sqlite, an sqlite3 database for nutratracker clients --- Copyright (C) 2018-2022 Shane Jaroch +-- nt-sqlite, an sqlite3 database for embedded clients +-- Copyright (C) 2018-2022 Shane Jaroch -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -13,7 +13,6 @@ -- -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . - CREATE TABLE `version` ( id integer PRIMARY KEY AUTOINCREMENT, `version` text NOT NULL UNIQUE, @@ -26,7 +25,6 @@ CREATE TABLE `version` ( --------------------------------- -- Equations --------------------------------- - CREATE TABLE bmr_eqs ( id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL UNIQUE @@ -41,7 +39,7 @@ CREATE TABLE bf_eqs ( -------------------------------- -- Profiles table -------------------------------- - +-- TODO: active profile? Decide what belongs here, vs. in prefs.json (if at all) CREATE TABLE profiles ( id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL UNIQUE, @@ -57,43 +55,10 @@ CREATE TABLE profiles ( FOREIGN KEY (bf_eq_id) REFERENCES bf_eqs (id) ON UPDATE CASCADE ); --- --------------------------------- --- Biometrics --------------------------------- - -CREATE TABLE biometrics ( - -- TODO: support custom biometrics and sync? - id integer PRIMARY KEY AUTOINCREMENT, - name text NOT NULL UNIQUE, - unit text, - created int DEFAULT (strftime ('%s', 'now')) -); - -CREATE TABLE biometric_log ( - id integer PRIMARY KEY AUTOINCREMENT, - profile_id int NOT NULL, - date int DEFAULT (strftime ('%s', 'now')), - tags text, - notes text, - created int DEFAULT (strftime ('%s', 'now')), - FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE -); - -CREATE TABLE bio_log_entry ( - log_id int NOT NULL, - biometric_id int NOT NULL, - value real NOT NULL, - PRIMARY KEY (log_id, biometric_id), - FOREIGN KEY (log_id) REFERENCES biometric_log (id) ON UPDATE CASCADE, - FOREIGN KEY (biometric_id) REFERENCES biometrics (id) ON UPDATE CASCADE -); - -- -------------------------------- -- Recipes -------------------------------- - CREATE TABLE recipes ( id integer PRIMARY KEY AUTOINCREMENT, tagname text NOT NULL UNIQUE, @@ -115,7 +80,6 @@ CREATE TABLE recipe_dat ( -------------------------------- -- Custom foods -------------------------------- - CREATE TABLE custom_foods ( id integer PRIMARY KEY AUTOINCREMENT, tagname text NOT NULL UNIQUE, @@ -137,7 +101,6 @@ CREATE TABLE cf_dat ( -------------------------------- -- Food (and recipe) logs -------------------------------- - CREATE TABLE meal_name ( -- predefined, includes standard three, snacks, brunch, and 3 optional/extra meals id integer PRIMARY KEY AUTOINCREMENT, @@ -157,6 +120,7 @@ CREATE TABLE food_log ( FOREIGN KEY (meal_id) REFERENCES meal_name (id) ON UPDATE CASCADE ); +-- TODO: support msre_id for recipes CREATE TABLE recipe_log ( id integer PRIMARY KEY AUTOINCREMENT, profile_id int NOT NULL, @@ -175,7 +139,6 @@ CREATE TABLE recipe_log ( -------------------------------- -- Custom RDAs -------------------------------- - CREATE TABLE rda ( profile_id int NOT NULL, nutr_id int NOT NULL, @@ -191,8 +154,7 @@ CREATE TABLE rda ( -- Case for no FK? e.g. points to food OR custom_food? -- Leave edge cases potentially dangling (should never happen) -- Does this simplify imports with a potential `guid` column? - -CREATE TABLE food_costs ( +CREATE TABLE food_cost ( food_id int NOT NULL, profile_id int NOT NULL, cost real NOT NULL, @@ -200,3 +162,33 @@ CREATE TABLE food_costs ( FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ON DELETE CASCADE ); +-- +-------------------------------- +-- Bug report, message queues +-------------------------------- +-- NOTE: be sure to SELECT version (latest) to include on bug report too +CREATE TABLE bug ( + id integer PRIMARY KEY AUTOINCREMENT, + profile_id int, + created int DEFAULT (strftime ('%s', 'now')), + arguments text, + stack text, + os text, + py_ver text, + user_details text, + submitted tinyint DEFAULT 0, + UNIQUE (arguments, stack), + FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ON DELETE CASCADE +); + +CREATE TABLE msg ( + id integer PRIMARY KEY AUTOINCREMENT, + profile_id int, + msg_id int NOT NULL, + created int, + received int DEFAULT (strftime ('%s', 'now')), + header text, + body text, + UNIQUE (profile_id, msg_id), + FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ON DELETE CASCADE +);