Merge pull request #76 from MycroftAI/add-device

added geography schema and changed skill schema to put all the skill …
pull/77/head
Chris Veilleux 2019-03-13 17:29:08 -05:00 committed by GitHub
commit cdf46b9232
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 91 additions and 80 deletions

View File

@ -1 +1 @@
CREATE DATABASE mycroft WITH TEMPLATE mycroft_template OWNER mycroft;
CREATE DATABASE mycroft WITH TEMPLATE mycroft_template OWNER selene;

View File

@ -1 +1 @@
CREATE DATABASE mycroft_template WITH OWNER = mycroft;
CREATE DATABASE mycroft_template WITH OWNER = selene;

View File

@ -7,7 +7,7 @@ CREATE TABLE device.account_preferences (
measurement_system measurement_system_enum NOT NULL DEFAULT 'Imperial',
wake_word_id uuid NOT NULL REFERENCES device.wake_word,
text_to_speech_id uuid NOT NULL REFERENCES device.text_to_speech,
location_id uuid REFERENCES device.location,
geography_id uuid REFERENCES device.geography,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

View File

@ -8,7 +8,7 @@ CREATE TABLE device.device (
wake_word_id uuid NOT NULL REFERENCES device.wake_word,
text_to_speech_id uuid NOT NULL REFERENCES device.text_to_speech,
category_id uuid REFERENCES device.category,
location_id uuid REFERENCES device.location,
geography_id uuid REFERENCES device.geography,
placement text,
last_contact_ts timestamp,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

View File

@ -1,9 +1,12 @@
CREATE TABLE device.location (
CREATE TABLE device.geography (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
account_id uuid NOT NULL REFERENCES account.account ON DELETE CASCADE,
country text NOT NULL,
postal_code text NOT NULL,
state text NOT NULL,
city text NOT NULL,
time_zone text,
latitude NUMERIC NOT NULL,
longitude NUMERIC NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (account_id, country, postal_code)
UNIQUE (account_id, latitude, longitude)
);

View File

@ -0,0 +1,2 @@
-- create the schema that will be used to geographical reference data
CREATE SCHEMA geography;

View File

@ -0,0 +1,2 @@
GRANT USAGE ON SCHEMA geography TO selene;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA geography TO selene;

View File

@ -0,0 +1,14 @@
CREATE TABLE geography.city (
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
region_id uuid NOT NULL
REFERENCES geography.region,
timezone_id uuid NOT NULL
REFERENCES geography.timezone,
name text NOT NULL,
latitude NUMERIC NOT NULL,
longitude NUMERIC NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
)

View File

@ -0,0 +1,9 @@
CREATE TABLE geography.country (
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
iso_code CHAR(2) NOT NULL
UNIQUE,
name text NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
)

View File

@ -0,0 +1,10 @@
CREATE TABLE geography.region (
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
country_id uuid NOT NULL
REFERENCES geography.country,
region_code VARCHAR(20) NOT NULL UNIQUE,
name text NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
)

View File

@ -0,0 +1,11 @@
CREATE TABLE geography.timezone (
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
country_id uuid NOT NULL
REFERENCES geography.country,
name text NOT NULL UNIQUE,
gmt_offset NUMERIC NOT NULL,
dst_offset NUMERIC,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
)

View File

@ -1,7 +0,0 @@
CREATE TABLE skill.activation (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL REFERENCES skill.skill,
activation text NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_id, activation)
);

View File

@ -1,14 +0,0 @@
CREATE TABLE skill.branch (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL REFERENCES skill.skill,
repository_name text NOT NULL,
branch text NOT NULL,
display_name text,
short_description text,
long_description text,
icon_name text,
icon_color text,
icon_image_url text,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (repository_name, branch)
);

View File

@ -1,7 +0,0 @@
CREATE TABLE skill.category (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL REFERENCES skill.skill,
category category_enum NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_id, category)
);

View File

@ -1,8 +0,0 @@
CREATE TABLE skill.credit (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL REFERENCES skill.skill,
github_id text NOT NULL,
github_name text NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_id, github_id)
);

View File

@ -0,0 +1,10 @@
CREATE TABLE skill.display (
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
skill_name text NOT NULL,
core_version core_version_enum NOT NULL,
display_data json NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_name, core_version)
);

View File

@ -1,7 +0,0 @@
CREATE TABLE skill.platform (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL REFERENCES skill.skill,
platform text NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_id, platform)
);

View File

@ -1,12 +0,0 @@
CREATE TABLE skill.setting_meta (
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL
REFERENCES skill.skill
ON DELETE CASCADE,
version text NOT NULL,
settings_meta json NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_id, version)
);

View File

@ -1,7 +0,0 @@
CREATE TABLE skill.tag (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL REFERENCES skill.skill,
tag text NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_id, tag)
);

View File

@ -0,0 +1 @@
CREATE TYPE core_version_enum AS ENUM ('18.08', '19.02');

View File

@ -4,7 +4,7 @@ from os import path
from psycopg2 import connect
MYCROFT_DB_DIR = path.join(path.abspath('..'), 'mycroft')
SCHEMAS = ('account', 'skill', 'device')
SCHEMAS = ('account', 'skill', 'device', 'geography')
DB_DESTROY_FILES = (
'drop_mycroft_db.sql',
'drop_template_db.sql',
@ -22,18 +22,13 @@ ACCOUNT_TABLE_ORDER = (
SKILL_TABLE_ORDER = (
'skill',
'settings_display',
'branch',
'activation',
'category',
'credit',
'platform',
'tag',
'display',
'oauth_credential',
'oauth_token'
)
DEVICE_TABLE_ORDER = (
'category',
'location',
'geography',
'text_to_speech',
'wake_word',
'wake_word_settings',
@ -41,6 +36,12 @@ DEVICE_TABLE_ORDER = (
'device',
'device_skill',
)
GEOGRAPHY_TABLE_ORDER = (
'country',
'timezone',
'region',
'city'
)
schema_directory = '{}_schema'
@ -65,7 +66,7 @@ class PostgresDB(object):
cursor.execute(sql)
postgres_db = PostgresDB(dbname='postgres', user='postgres')
postgres_db = PostgresDB(dbname='postgres', user='chrisveilleux')
print('Destroying any objects we will be creating later.')
for db_destroy_file in DB_DESTROY_FILES:
@ -137,6 +138,17 @@ for table in DEVICE_TABLE_ORDER:
get_sql_from_file(create_table_file)
)
print('Creating the geography schema tables')
for table in GEOGRAPHY_TABLE_ORDER:
create_table_file = path.join(
'geography_schema',
'tables',
table + '.sql'
)
template_db.execute_sql(
get_sql_from_file(create_table_file)
)
print('Granting access to schemas and tables')
for schema in SCHEMAS:
template_db.execute_sql(
@ -146,8 +158,7 @@ for schema in SCHEMAS:
template_db.close_db()
print('Copying template to new database.')
# Copy template to new database.
postgres_db = PostgresDB(dbname='postgres', user='postgres')
postgres_db = PostgresDB(dbname='postgres', user='chrisveilleux')
postgres_db.execute_sql(get_sql_from_file('create_mycroft_db.sql'))
postgres_db.close_db()