forked from rabidgremlin/GraphQLReferenceData
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupdatabase.sql
More file actions
57 lines (49 loc) · 2.34 KB
/
setupdatabase.sql
File metadata and controls
57 lines (49 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- Create the database
CREATE DATABASE graphqlref;
CREATE USER graphqlref password 'graphqlref';
GRANT ALL PRIVILEGES ON DATABASE graphqlref TO graphqlref; -- in production we'd want to reduce these rights
-- switch to graphqlref database
\c graphqlref
-- create the countries table
CREATE TABLE countries (
country_code varchar(2) PRIMARY KEY,
name varchar(100) NOT NUll UNIQUE
);
GRANT ALL PRIVILEGES ON TABLE countries TO graphqlref; -- in production we'd want to reduce these rights
-- create the cities table
CREATE TABLE cities (
id int PRIMARY KEY,
country_code varchar(2) NOT NUll REFERENCES countries(country_code),
name varchar(100) NOT NUll,
timezone_id varchar(30) NOT NUll
);
CREATE INDEX country_code_idx_cities ON cities(country_code); -- postgres doesn't create indexes for foreign keys
GRANT ALL PRIVILEGES ON TABLE cities TO graphqlref; -- in production we'd want to reduce these rights
-- create the airports table
CREATE TABLE airports (
id int PRIMARY KEY,
name varchar(100) NOT NUll,
city_id int NOT NUll REFERENCES cities(id),
country_code varchar(2) NOT NUll REFERENCES countries(country_code),
iata_code varchar(3) UNIQUE,
icao_code varchar(4) UNIQUE,
latitude float NOT NUll,
longitude float NOT NUll,
elevation int NULL,
timezone_id varchar(30) NOT NUll
);
CREATE INDEX country_code_idx_airports ON airports(country_code); -- postgres doesn't create indexes for foreign keys
CREATE INDEX city_id_idx_airports ON airports(city_id); -- postgres doesn't create indexes for foreign keys
GRANT ALL PRIVILEGES ON TABLE airports TO graphqlref; -- in production we'd want to reduce these rights
-- because we are using country_code instead of country_id we need to give postgraphile how we want to name these relationships
COMMENT ON CONSTRAINT "cities_country_code_fkey" on "cities" is E'@foreignFieldName cities\n@fieldName country';
COMMENT ON CONSTRAINT "airports_country_code_fkey" on "airports" is E'@foreignFieldName airports\n@fieldName country';
-- load the countries table from .csv file
COPY countries
FROM '/tmp/countries.csv' DELIMITER ',' CSV NULL AS '\N';
-- load the cities table from .csv file
COPY cities
FROM '/tmp/cities.csv' DELIMITER ',' CSV NULL AS '\N';
-- load the airports table from .csv file
COPY airports
FROM '/tmp/airports.csv' DELIMITER ',' CSV NULL AS '\N';