Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql #1448

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Sql #1448

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions sql/common/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ GRANT ALL PRIVILEGES ON SEQUENCE oauth_tokens_id_seq TO ingress_user;


--
-- SCORES
-- SCORES -- not in common as of 01/13/2016
--

CREATE TABLE account_scores(
Expand Down Expand Up @@ -185,20 +185,6 @@ CREATE TABLE sleep_feedback(
GRANT ALL PRIVILEGES ON sleep_feedback TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sleep_feedback_id_seq TO ingress_user;

CREATE TABLE account_location (
id SERIAL PRIMARY KEY,
account_id BIGINT NOT NULL,
longitude DOUBLE PRECISION NOT NULL,
latitude DOUBLE PRECISION NOT NULL,
ip_address INET,
city varchar(100),
country varchar(2), -- see http://en.wikipedia.org/wiki/ISO_3166-1
created TIMESTAMP default current_timestamp
);

GRANT ALL PRIVILEGES ON account_location TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE account_location_id_seq TO ingress_user;

-- for data science-y stuff

CREATE TYPE USER_LABEL_TYPE AS ENUM ('make_bed', 'went_to_bed', 'fall_asleep',
Expand Down Expand Up @@ -255,11 +241,17 @@ GRANT ALL PRIVILEGES ON sense_colors TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sense_colors_id_seq TO ingress_user;


-- not in common as of 01/13/2016
CREATE TABLE tracking (id SERIAL PRIMARY KEY,
sense_id VARCHAR(255),
internal_sense_id BIGINT,
account_id BIGINT,
category SMALLINT,
created_at TIMESTAMP
);

CREATE TABLE tracking (id SERIAL PRIMARY KEY, sense_id VARCHAR(255), internal_sense_id BIGINT, account_id BIGINT, category SMALLINT, created_at TIMESTAMP);
CREATE UNIQUE index tracking_uniq_device_id_category on tracking(internal_sense_id, category);


GRANT ALL PRIVILEGES ON tracking TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE tracking_id_seq TO ingress_user;

Expand Down
2 changes: 1 addition & 1 deletion sql/insights/updates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ responses_ids = '{29, 30, 31, 32, 33}' WHERE id = 9;

-- grammar!!
update questions SET question_text = 'Have you ever woken up gasping for breath?' where id = 13;
update questions set question_text = 'Have you been diagnosed with any sleep disorders?' where id = 12;
update questions set question_text = 'Have you been diagnosed with any sleep disorders?' where id = 12;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Deprecated. Data is in DynamoDB sleep_stats table

-- For Trends Graphs

-- Sleep duration by day of week
CREATE TABLE sleep_duration_dow (
id BIGSERIAL PRIMARY KEY,
account_id BIGINT,
day_of_week INTEGER,
duration_sum BIGINT default 0, -- minutes
duration_count INTEGER default 0, -- number of nights
local_utc_updated TIMESTAMP
);

CREATE UNIQUE INDEX unique_account_id_duration_dow on sleep_duration_dow(account_id, day_of_week);
CREATE INDEX duration_dow_account_id on sleep_duration_dow(account_id);

GRANT ALL PRIVILEGES ON sleep_duration_dow TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sleep_duration_dow_id_seq TO ingress_user;

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Deprecated, no data in table

CREATE TABLE sleep_feedback(
id SERIAL PRIMARY KEY,
account_id BIGINT,
day VARCHAR (100),
hour VARCHAR (100),
correct BOOLEAN
);

GRANT ALL PRIVILEGES ON sleep_feedback TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sleep_feedback_id_seq TO ingress_user;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

CREATE TABLE sleep_label(
id SERIAL PRIMARY KEY,
account_id BIGINT,
date_utc TIMESTAMP,
rating INTEGER,
sleep_at_utc TIMESTAMP,
wakeup_at_utc TIMESTAMP,
offset_millis INTEGER
);

CREATE UNIQUE INDEX uniq_account_target_date on sleep_label(account_id, date_utc);

GRANT ALL PRIVILEGES ON sleep_label TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sleep_label_id_seq TO ingress_user;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Deprecated, data in DynamoDB now

CREATE TABLE sleep_score (
id BIGSERIAL PRIMARY KEY,
account_id BIGINT,
device_id BIGINT,
pill_id BIGINT, -- pill id
date_bucket_utc TIMESTAMP, -- Y-m-d H:00 in UTC
offset_millis INTEGER,
sleep_duration INTEGER, -- no. of minutes of data used to compute scores
custom BOOLEAN default false, -- true if the scores are user-customized
bucket_score INTEGER, -- score for this time period, updated as we get more data
agitation_num INTEGER, -- agitation score, number of times pill value > -1
agitation_tot INTEGER, -- agitation score, area under curve
updated TIMESTAMP default current_timestamp -- server time when record was last updated
);

CREATE UNIQUE INDEX unique_account_id_date_hour_utc on sleep_score(account_id, date_bucket_utc);

GRANT ALL PRIVILEGES ON sleep_score TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sleep_score_id_seq TO ingress_user;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Deprecated

-- Sleep score by day of week
CREATE TABLE sleep_score_dow (
id BIGSERIAL PRIMARY KEY,
account_id BIGINT,
day_of_week INTEGER,
score_sum BIGINT default 0, -- sum of scores
score_count INTEGER default 0, -- number of nights
local_utc_updated TIMESTAMP
);

CREATE UNIQUE INDEX unique_account_id_score_dow on sleep_score_dow(account_id, day_of_week);
CREATE INDEX scores_dow_account_id on sleep_score_dow(account_id);

GRANT ALL PRIVILEGES ON sleep_score_dow TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sleep_score_dow_id_seq TO ingress_user;

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Deprecated


-- sleep duration over time

CREATE TABLE sleep_stats_time(
id BIGSERIAL PRIMARY KEY,
account_id BIGINT,
duration INTEGER, -- minutes
sound_sleep INTEGER, -- mins
light_sleep INTEGER,
sleep_time_utc TIMESTAMP, -- fall asleep time in UTC
wake_time_utc TIMESTAMP, -- wake up time in UTC
fall_asleep_time INTEGER, -- mins taken to fall asleep after getting into bed
motion INTEGER, -- number of motion events
offset_millis INTEGER, -- timezone offset
local_utc_date TIMESTAMP -- night of yyyy-mm-dd
);

CREATE UNIQUE INDEX unique_account_date_duration on sleep_stats_time(account_id, local_utc_date);
GRANT ALL PRIVILEGES ON sleep_stats_time TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE sleep_stats_time_id_seq TO ingress_user;

-- to alter sleep_stats_time table (01/02/2015)
ALTER TABLE sleep_stats_time ADD COLUMN sleep_time_utc TIMESTAMP;
ALTER TABLE sleep_stats_time ADD COLUMN wake_time_utc TIMESTAMP;
ALTER TABLE sleep_stats_time ADD COLUMN fall_asleep_time INTEGER default 0;
14 changes: 14 additions & 0 deletions suripu-core/src/main/resources/sql/common/account_device_map.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE account_device_map(
id SERIAL PRIMARY KEY,
account_id BIGINT,
device_name VARCHAR(100),
created_at TIMESTAMP default current_timestamp
);

GRANT ALL PRIVILEGES ON account_device_map TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE account_device_map_id_seq TO ingress_user;

ALTER TABLE account_device_map ADD COLUMN active BOOLEAN DEFAULT TRUE;
ALTER TABLE account_device_map ADD COLUMN last_updated TIMESTAMP default current_timestamp;

CREATE UNIQUE INDEX uniq_account_device_name on account_device_map(account_id, device_name, active);
18 changes: 18 additions & 0 deletions suripu-core/src/main/resources/sql/common/account_location.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- User Location Data, added during onboarding for now. 2015-12-30

CREATE TABLE account_location(
id SERIAL PRIMARY KEY,
account_id BIGINT,
ip INET,
latitude DOUBLE PRECISION,
longitude DOUBLE PRECISION,
city VARCHAR(255),
state VARCHAR(255),
country_code CHAR(2), -- see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
created TIMESTAMP default current_timestamp
);

CREATE UNIQUE INDEX uniq_account_location_created_idx ON account_location(account_id, created);

GRANT ALL PRIVILEGES ON account_location TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE account_location_id_seq TO ingress_user;
21 changes: 21 additions & 0 deletions suripu-core/src/main/resources/sql/common/account_tracker_map.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Keeping track of trackers for each users
-- Normally should only be one / account
-- but might want to support multiple for debug/admin purposes

CREATE TABLE account_tracker_map(
id BIGSERIAL PRIMARY KEY,
account_id BIGINT,
device_id VARCHAR(100),
created_at TIMESTAMP default current_timestamp
);

-- CREATE UNIQUE INDEX uniq_account_tracker on account_tracker_map(account_id, device_id);
CREATE UNIQUE INDEX uniq_account_tracker_active on account_tracker_map(account_id, device_id, active);
CREATE INDEX pill_id_active ON account_tracker_map(device_id, active);


GRANT ALL PRIVILEGES ON account_tracker_map TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE account_tracker_map_id_seq TO ingress_user;

ALTER TABLE account_tracker_map ADD COLUMN active BOOLEAN DEFAULT TRUE;
ALTER TABLE account_tracker_map ADD COLUMN last_updated TIMESTAMP default current_timestamp;
44 changes: 44 additions & 0 deletions suripu-core/src/main/resources/sql/common/accounts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE TABLE accounts (
id SERIAL PRIMARY KEY,
firstname VARCHAR (100),
lastname VARCHAR (100),
username VARCHAR (100),
email VARCHAR (255),
password_hash CHAR (60),
created TIMESTAMP,
height SMALLINT,
weight SMALLINT,
age SMALLINT,
tz VARCHAR (100)
);

CREATE UNIQUE INDEX uniq_email on accounts(email);

GRANT ALL PRIVILEGES ON accounts TO ingress_user;
GRANT ALL PRIVILEGES ON SEQUENCE accounts_id_seq TO ingress_user;



--
-- UPDATES TO ACCOUNT TABLE 2014-07-16
--

ALTER TABLE accounts ADD COLUMN name VARCHAR (255);
UPDATE accounts SET name = firstname || ' ' || lastname;
ALTER TABLE accounts ALTER COLUMN name set NOT NULL;
ALTER TABLE accounts ALTER COLUMN weight SET DATA TYPE INTEGER;



--
-- UPDATES TO ACCOUNT TABLE 2014-07-17
--

ALTER TABLE accounts ADD COLUMN tz_offset INTEGER;
ALTER TABLE accounts ADD COLUMN last_modified BIGINT;
-- ALTER TABLE accounts DROP COLUMN tz;
ALTER TABLE accounts ADD COLUMN dob TIMESTAMP;



ALTER TABLE accounts ADD COLUMN gender VARCHAR(50);
Loading