From: Shane Jaroch Date: Mon, 11 Jul 2022 23:18:27 +0000 (-0400) Subject: wip tables X-Git-Tag: 0.0.5^2 X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=b8f312a20f1b0ba4eda2b0878bf6143a9f35e975;p=nutratech%2Fnt-sqlite.git wip tables use rst for changelog, no plural tables, wip other fix test, in Makefile tweak changelog --- diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index b77c00a..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,95 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -## [Unreleased] - -### Changed - -- 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 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-populated `meals` table to `meal_name` - -### Fixed - -- Slight bash inconvenience when using `export.sh` - -### Removed - -- `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 - -### Changed - -- `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 allotted) -- `guid` parameter from `functions.sql` - -## [0.0.2] - 2021-05-24 - -### Added - -`SCRIPT_DIR` in `sql/__init__.py` to help track `cwd` - -### Removed - -- `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) - -### Changed - -- 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 - (e.g. `food_log.csv`, `biometric_log.csv`, `profiles.csv`) -- Database diagram generated via `docs/sqleton.sh` diff --git a/CHANGELOG.rst b/CHANGELOG.rst new file mode 100644 index 0000000..36478bb --- /dev/null +++ b/CHANGELOG.rst @@ -0,0 +1,138 @@ +*********** + Changelog +*********** + +All notable changes to this project will be documented in this file. + +The format is based on `Keep a Changelog `_, +and this project adheres to `Semantic Versioning `_. + + + +`[Unreleased] `_ +############################################################################## + + + +`[0.0.5] - 2022-07-11 `_ +###################################################################################### + +Changed +~~~~~~~ + +- 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 packageable with ``cli`` repo + (as a ``git submodule``) +- SQL table ``custom_foods`` and respective ``cf_dat`` + +Changed +~~~~~~~ + +- Build with ``python sql/__init__.py`` (removed top-level script ``build.py``) +- Rename pre-populated ``meals`` table to ``meal_name`` + +Fixed +~~~~~ + +- Slight bash inconvenience when using ``export.sh`` + +Removed +~~~~~~~ + +- ``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 + +Changed +~~~~~~~ + +- ``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 allotted) +- ``guid`` parameter from ``functions.sql`` + + + +`[0.0.2] - 2021-05-24 `_ +###################################################################################### + +Added +~~~~~ + +``SCRIPT_DIR`` in ``sql/__init__.py`` to help track ``cwd`` + +Removed +~~~~~~~ + +- ``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) + +Changed +~~~~~~~ + +- 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 + (e.g. ``food_log.csv``, ``biometric_log.csv``, ``profiles.csv``) +- Database diagram generated via ``docs/sqleton.sh`` diff --git a/Makefile b/Makefile index c52a3af..14b0c7d 100644 --- a/Makefile +++ b/Makefile @@ -15,15 +15,16 @@ clean: ## Clean up build intermediates .PHONY: build build: ## Build sqlite image - python -m sql + python3 -m sql .PHONY: test test: ## Cursory sanity check sqlite3 -csv -header \ sql/nt.sqlite3 \ - 'SELECT * FROM bf_eqs;' \ - 'SELECT * FROM bmr_eqs;' \ + 'SELECT * FROM bf_eq;' \ + 'SELECT * FROM bmr_eq;' \ 'SELECT * FROM meal_name;' \ + 'SELECT * FROM rda;' \ 'SELECT * FROM version;' \ .PHONY: install diff --git a/sql/data/bf_eqs.csv b/sql/data/bf_eq.csv similarity index 100% rename from sql/data/bf_eqs.csv rename to sql/data/bf_eq.csv diff --git a/sql/data/bmr_eqs.csv b/sql/data/bmr_eq.csv similarity index 100% rename from sql/data/bmr_eqs.csv rename to sql/data/bmr_eq.csv diff --git a/sql/format.sh b/sql/format.sh index ac6058e..d7126ee 100755 --- a/sql/format.sh +++ b/sql/format.sh @@ -3,9 +3,8 @@ cd "$(dirname "$0")" # Format -pg_format -L -s 2 -w 80 tables.sql >tables.fmt.sql +pg_format -L -s 2 -w 100 tables.sql >tables.fmt.sql mv tables.fmt.sql tables.sql -pg_format -L -s 2 -w 80 functions.sql >functions.fmt.sql +pg_format -L -s 2 -w 100 functions.sql >functions.fmt.sql mv functions.fmt.sql functions.sql - diff --git a/sql/tables.sql b/sql/tables.sql index 3a95eda..ef7e773 100644 --- a/sql/tables.sql +++ b/sql/tables.sql @@ -20,27 +20,28 @@ CREATE TABLE `version` ( notes text ); +-- NOTE: INSERT INTO statements for version, bmr_eq, bf_eq? Don't maintain as CSV? -- TODO: enforce FK constraint across two DBs? -- --------------------------------- -- Equations --------------------------------- -CREATE TABLE bmr_eqs ( +CREATE TABLE bmr_eq ( id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL UNIQUE ); -CREATE TABLE bf_eqs ( +CREATE TABLE bf_eq ( id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL UNIQUE ); -- -------------------------------- --- Profiles table +-- Profile table -------------------------------- -- TODO: active profile? Decide what belongs here, vs. in prefs.json (if at all) -CREATE TABLE profiles ( +CREATE TABLE profile ( id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL UNIQUE, gender text, @@ -51,15 +52,15 @@ CREATE TABLE profiles ( bmr_eq_id int DEFAULT 1, bf_eq_id int DEFAULT 1, created int DEFAULT (strftime ('%s', 'now')), - FOREIGN KEY (bmr_eq_id) REFERENCES bmr_eqs (id) ON UPDATE CASCADE, - FOREIGN KEY (bf_eq_id) REFERENCES bf_eqs (id) ON UPDATE CASCADE + FOREIGN KEY (bmr_eq_id) REFERENCES bmr_eq (id) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY (bf_eq_id) REFERENCES bf_eq (id) ON UPDATE CASCADE ON DELETE CASCADE ); -- -------------------------------- --- Recipes +-- Recipe -------------------------------- -CREATE TABLE recipes ( +CREATE TABLE recipe ( id integer PRIMARY KEY AUTOINCREMENT, tagname text NOT NULL UNIQUE, name text NOT NULL UNIQUE, @@ -73,14 +74,14 @@ CREATE TABLE recipe_dat ( notes text, created int DEFAULT (strftime ('%s', 'now')), PRIMARY KEY (recipe_id, food_id), - FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE + FOREIGN KEY (recipe_id) REFERENCES recipe (id) ON UPDATE CASCADE ON DELETE CASCADE ); -- -------------------------------- -- Custom foods -------------------------------- -CREATE TABLE custom_foods ( +CREATE TABLE custom_food ( id integer PRIMARY KEY AUTOINCREMENT, tagname text NOT NULL UNIQUE, name text NOT NULL UNIQUE, @@ -94,7 +95,7 @@ CREATE TABLE cf_dat ( notes text, created int DEFAULT (strftime ('%s', 'now')), PRIMARY KEY (cf_id, nutr_id), - FOREIGN KEY (cf_id) REFERENCES custom_foods (id) ON UPDATE CASCADE + FOREIGN KEY (cf_id) REFERENCES custom_food (id) ON UPDATE CASCADE ON DELETE CASCADE ); -- @@ -112,15 +113,18 @@ CREATE TABLE food_log ( profile_id int NOT NULL, date int DEFAULT (strftime ('%s', 'now')), meal_id int NOT NULL, - food_id int NOT NULL, + -- NOTE: do we want separate tables for logging `food_id` vs. `custom_food_id`? + food_id int, + custom_food_id int, msre_id int NOT NULL, amt real NOT NULL, created int DEFAULT (strftime ('%s', 'now')), - FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE, - FOREIGN KEY (meal_id) REFERENCES meal_name (id) ON UPDATE CASCADE + FOREIGN KEY (profile_id) REFERENCES profile (id) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY (meal_id) REFERENCES meal_name (id) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY (custom_food_id) REFERENCES custom_food (id) ON UPDATE CASCADE ON DELETE CASCADE ); --- TODO: support msre_id for recipes +-- TODO: support msre_id for recipe CREATE TABLE recipe_log ( id integer PRIMARY KEY AUTOINCREMENT, profile_id int NOT NULL, @@ -129,9 +133,9 @@ CREATE TABLE recipe_log ( recipe_id int NOT NULL, grams real NOT NULL, created int DEFAULT (strftime ('%s', 'now')), - FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE, - FOREIGN KEY (meal_id) REFERENCES meal_name (id) ON UPDATE CASCADE, - FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE + FOREIGN KEY (profile_id) REFERENCES profile (id) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY (meal_id) REFERENCES meal_name (id) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY (recipe_id) REFERENCES recipe (id) ON UPDATE CASCADE ON DELETE CASCADE ); -- TODO: CREATE TABLE custom_food_log ( ... ); @@ -144,7 +148,7 @@ CREATE TABLE rda ( nutr_id int NOT NULL, rda real NOT NULL, PRIMARY KEY (profile_id, nutr_id), - FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE + FOREIGN KEY (profile_id) REFERENCES profile (id) ON UPDATE CASCADE ON DELETE CASCADE ); -- @@ -155,11 +159,13 @@ CREATE TABLE rda ( -- Leave edge cases potentially dangling (should never happen) -- Does this simplify imports with a potential `guid` column? CREATE TABLE food_cost ( - food_id int NOT NULL, + food_id int, + custom_food_id int, profile_id int NOT NULL, cost real NOT NULL, - PRIMARY KEY (food_id, profile_id), - FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ON DELETE CASCADE + PRIMARY KEY (food_id, custom_food_id, profile_id), + FOREIGN KEY (custom_food_id) REFERENCES custom_food (id) ON UPDATE CASCADE ON DELETE CASCADE, + FOREIGN KEY (profile_id) REFERENCES profile (id) ON UPDATE CASCADE ON DELETE CASCADE ); -- @@ -172,13 +178,16 @@ CREATE TABLE bug ( profile_id int, created int DEFAULT (strftime ('%s', 'now')), arguments text, + activity text, stack text, - os text, - py_ver text, - user_details text, + -- e.g. OS, Python / Android version + client_info json, + -- e.g. app version + app_info json, + user_details json, submitted tinyint DEFAULT 0, UNIQUE (arguments, stack), - FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ON DELETE CASCADE + FOREIGN KEY (profile_id) REFERENCES profile (id) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE msg ( @@ -190,5 +199,5 @@ CREATE TABLE msg ( header text, body text, UNIQUE (profile_id, msg_id), - FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE ON DELETE CASCADE + FOREIGN KEY (profile_id) REFERENCES profile (id) ON UPDATE CASCADE ON DELETE CASCADE );