nm
authorgamesguru <mathmuncher11@gmail.com>
Mon, 5 Oct 2020 19:13:24 +0000 (15:13 -0400)
committergamesguru <mathmuncher11@gmail.com>
Mon, 5 Oct 2020 19:13:24 +0000 (15:13 -0400)
sql/data/profiles.csv [moved from sql/data/users.csv with 100% similarity]
sql/functions.sql
sql/import.sql
sql/tables.sql

similarity index 100%
rename from sql/data/users.csv
rename to sql/data/profiles.csv
index d47e9ab713a56981c817fa55afb2331d0f77d159..8a6f3744cb62205e4a77257d5d18000131293245 100644 (file)
@@ -3,18 +3,16 @@
 --------------------------------
 
 SELECT
-  users.name,
+  profiles.name,
   date,
-  ROUND(bio_log_entry.value * 2.204, 1) AS weight,
-  tags,
-  notes
+  ROUND(bio_log_entry.value * 2.204, 1) AS weight
 FROM
   biometric_log
-  INNER JOIN users ON user_id = users.id
+  INNER JOIN profiles ON profile_id = profiles.id
   INNER JOIN bio_log_entry ON biometric_id = 2
     AND log_id = biometric_log.id
 WHERE
-  users.name = 'Mark';
+  profiles.name = 'Mark';
 
 --------------------------------
 -- Pulse and blood pressure
@@ -22,14 +20,14 @@ WHERE
 
 SELECT DISTINCT
   date,
-  users.name,
+  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 users ON user_id = users.id
+  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
@@ -37,7 +35,7 @@ FROM
   INNER JOIN bio_log_entry dia ON dia.biometric_id = 24
     AND dia.log_id = biometric_log.id
 WHERE
-  users.name = 'Mark';
+  profiles.name = 'Mark';
 
 --------------------------------
 -- Height, wrist, ankle
@@ -45,13 +43,13 @@ WHERE
 
 SELECT
   date,
-  users.name,
+  profiles.name,
   height.value AS height,
   wrist.value AS wrist,
   ankle.value AS ankle
 FROM
   biometric_log
-  INNER JOIN users ON user_id = users.id
+  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
@@ -69,7 +67,7 @@ WHERE
 
 SELECT
   date,
-  users.name,
+  profiles.name,
   chest.value / 2.54 AS chest,
   arm.value / 2.54 AS arm,
   thigh.value / 2.54 AS thigh,
@@ -81,7 +79,7 @@ SELECT
   forearm.value / 2.54 AS forearm
 FROM
   biometric_log
-  INNER JOIN users ON user_id = users.id
+  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
@@ -117,7 +115,7 @@ WHERE
 
 SELECT
   date,
-  users.name,
+  profiles.name,
   CAST(pectoral.value AS int) AS pec,
   CAST(abdominal.value AS int) AS ab,
   CAST(quadricep.value AS int) AS quad,
@@ -127,7 +125,7 @@ SELECT
   CAST(suprailiac.value AS int) AS supra
 FROM
   biometric_log
-  INNER JOIN users ON user_id = users.id
+  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
index 465650de77f528e83202bf59db62808b182bf148..19c7a18203d654674ea9d8f42dfe2111d59ee433 100644 (file)
 -- You should have received a copy of the GNU General Public License
 -- along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
+PRAGMA foreign_keys = 1;
+-- PRAGMA integrity_check = 1;
+
+
 .mode csv
-.nullvalue NULL
+-- .nullvalue NULL
 
 .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/meals.csv' meals
 
 .import '| tail -n +2 ./data/biometrics.csv' biometrics
-.import '| tail -n +2 ./data/users.csv' users
+.import '| tail -n +2 ./data/profiles.csv' profiles
 .import '| tail -n +2 ./data/rda.csv' rda
 
 .import '| tail -n +2 ./data/recipes.csv' recipes
index fdc63e58e4a93a9f74f1fc47b49e4819b5077056..1e6036921dd72669b82163fa0bbcbcd516a53500 100644 (file)
@@ -14,8 +14,6 @@
 -- You should have received a copy of the GNU General Public License
 -- along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-PRAGMA foreign_keys = 1;
-
 CREATE TABLE version( id integer PRIMARY KEY AUTOINCREMENT, version text NOT NULL, created date NOT NULL, notes text
 );
 
@@ -39,10 +37,10 @@ CREATE TABLE bf_eqs (
 
 --
 --------------------------------
--- Users table
+-- Profiles table
 --------------------------------
 
-CREATE TABLE users (
+CREATE TABLE profiles (
   id integer PRIMARY KEY AUTOINCREMENT,
   name text NOT NULL UNIQUE,
   guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE,
@@ -75,14 +73,14 @@ 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,
+  profile_id int NOT NULL,
   created int DEFAULT (strftime ('%s', 'now')),
   updated int DEFAULT (strftime ('%s', 'now')),
   synced int DEFAULT -1,
   date int DEFAULT (strftime ('%s', 'now')),
   tags text,
   notes text,
-  FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE
+  FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE
 );
 
 CREATE TABLE bio_log_entry (
@@ -130,24 +128,24 @@ CREATE TABLE meals (
 CREATE TABLE food_log (
   id integer PRIMARY KEY AUTOINCREMENT,
   guid text NOT NULL DEFAULT (lower(hex (randomblob (16)))) UNIQUE,
-  user_id int NOT NULL,
+  profile_id int NOT NULL,
   date date DEFAULT CURRENT_DATE,
   meal_id int NOT NULL,
   food_id int NOT NULL, -- TODO: enforce FK constraint across two DBs?
   grams real NOT NULL,
-  FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE,
+  FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE,
   FOREIGN KEY (meal_id) REFERENCES meals (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 NOT NULL,
+  profile_id int NOT NULL,
   date date DEFAULT CURRENT_DATE,
   meal_id int NOT NULL,
   recipe_id int NOT NULL,
   grams real NOT NULL,
-  FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE,
+  FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE,
   FOREIGN KEY (meal_id) REFERENCES meals (id) ON UPDATE CASCADE,
   FOREIGN KEY (recipe_id) REFERENCES recipes (id) ON UPDATE CASCADE
 );
@@ -158,13 +156,13 @@ CREATE TABLE recipe_log (
 --------------------------------
 
 CREATE TABLE rda (
-  user_id int NOT NULL,
+  profile_id int NOT NULL,
   -- TODO: enforce FK constraint across two DBs?
   nutr_id int NOT NULL,
   rda real NOT NULL,
   synced int DEFAULT 0,
-  PRIMARY KEY (user_id, nutr_id),
-  FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE CASCADE
+  PRIMARY KEY (profile_id, nutr_id),
+  FOREIGN KEY (profile_id) REFERENCES profiles (id) ON UPDATE CASCADE
 );
 
 CREATE TRIGGER rda_sync
@@ -172,7 +170,7 @@ CREATE TRIGGER rda_sync
 BEGIN
   UPDATE rda SET synced = 0
 WHERE
-  NEW.user_id = user_id AND NEW.nutr_id = nutr_id;
+  NEW.profile_id = profile_id AND NEW.nutr_id = nutr_id;
 
 END;