Skip to content

Commit f330185

Browse files
Write SQL scripts to build DB schema and tables
1 parent cbc153a commit f330185

6 files changed

+148
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
-- AUTHOR: Eric MIlgram, PhD
2+
--
3+
-- DATE: 08 Dec 2021
4+
--
5+
-- PURPOSE
6+
-- The purpose of these SQL statements is to properly set up required
7+
-- schemas and roles for the PostgreSQL database used for the
8+
-- Paylocity Coding Challenge.
9+
--
10+
-- This code was adapted from the code posted by Mr. Yaser Raja
11+
-- in a blog post titled "Managing PostgreSQL users and roles," which
12+
-- was published on the "AWS Database Blog" on 04 MAR 2019. The post's
13+
-- permalink is
14+
-- https://aws.amazon.com/blogs/database/managing-postgresql-users-and-roles/
15+
16+
-- ############################################################################
17+
-- Create the Paylocity database
18+
-- NOTE: The steps in this section are not required when using PostgreSQL on
19+
-- AWS RDS.
20+
-- ############################################################################
21+
-- CREATE DATABASE paylocity
22+
-- WITH
23+
-- OWNER = postgres
24+
-- ENCODING = 'UTF8'
25+
-- LC_COLLATE = 'en_US.UTF-8'
26+
-- LC_CTYPE = 'en_US.UTF-8'
27+
-- TABLESPACE = pg_default
28+
-- CONNECTION LIMIT = -1;
29+
30+
-- SET default_tablespace = pg_default;
31+
32+
-- ############################################################################
33+
-- Create the 'dev' schema, which is for DB development only
34+
-- ############################################################################
35+
CREATE SCHEMA dev;
36+
37+
COMMENT ON SCHEMA dev IS 'Schema for developers';
38+
39+
-- ############################################################################
40+
-- REVOKE privileges from 'public' role from public
41+
-- ############################################################################
42+
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
43+
44+
REVOKE ALL ON DATABASE paylocity FROM PUBLIC;
45+
46+
-- ############################################################################
47+
-- CREATE Read-only role
48+
-- ############################################################################
49+
CREATE ROLE readonly;
50+
51+
GRANT CONNECT ON DATABASE paylocity TO readonly;
52+
53+
GRANT USAGE ON SCHEMA dev TO readonly;
54+
55+
GRANT SELECT ON ALL TABLES IN SCHEMA dev TO readonly;
56+
57+
ALTER DEFAULT PRIVILEGES IN SCHEMA dev
58+
GRANT SELECT ON TABLES TO readonly;
59+
60+
-- ############################################################################
61+
-- CREATE Read/write role
62+
-- ############################################################################
63+
CREATE ROLE readwrite;
64+
65+
GRANT CONNECT ON DATABASE paylocity TO readwrite;
66+
67+
GRANT USAGE, CREATE ON SCHEMA dev TO readwrite;
68+
69+
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA dev TO readwrite;
70+
71+
ALTER DEFAULT PRIVILEGES IN SCHEMA dev
72+
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO readwrite;
73+
74+
GRANT USAGE ON ALL SEQUENCES IN SCHEMA dev TO readwrite;
75+
76+
ALTER DEFAULT PRIVILEGES IN SCHEMA dev
77+
GRANT USAGE ON SEQUENCES TO readwrite;
78+
79+
-- ############################################################################
80+
-- Create users
81+
-- ############################################################################
82+
CREATE USER reporting_user1 WITH PASSWORD 'reporting_user1CHANGE_ON_FIRST_LOGIN';
83+
84+
CREATE USER reporting_user2 WITH PASSWORD 'reporting_user2CHANGE_ON_FIRST_LOGIN';
85+
86+
CREATE USER app_user1 WITH PASSWORD 'CHANGE_ON_FIRST_LOGIN';
87+
88+
CREATE USER app_user2 WITH PASSWORD 'CHANGE_ON_FIRST_LOGIN';
89+
90+
-- ############################################################################
91+
-- Grant privileges to users
92+
-- ############################################################################
93+
GRANT readonly TO reporting_user1;
94+
95+
GRANT readonly TO reporting_user2;
96+
97+
GRANT readwrite TO app_user1;
98+
99+
GRANT readwrite TO app_user2;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- ############################################################################
2+
-- CREATE Table: paylocity.dev.Company
3+
-- ############################################################################
4+
DROP TABLE IF EXISTS paylocity.dev.Company CASCADE;
5+
6+
CREATE TABLE IF NOT EXISTS paylocity.dev.Company
7+
(
8+
guid uuid PRIMARY KEY,
9+
name varchar(50) NOT NULL UNIQUE,
10+
status integer NOT NULL
11+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- ############################################################################
2+
-- Create the Position table
3+
-- ############################################################################
4+
DROP TABLE IF EXISTS paylocity.dev.Position CASCADE;
5+
6+
CREATE TABLE IF NOT EXISTS paylocity.dev.Position
7+
(
8+
guid uuid PRIMARY KEY,
9+
name varchar(50) NOT NULL UNIQUE,
10+
status integer NOT NULL
11+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- ############################################################################
2+
-- Create the Employee table
3+
-- ############################################################################
4+
DROP TABLE IF EXISTS paylocity.dev.Employee CASCADE;
5+
6+
CREATE TABLE IF NOT EXISTS paylocity.dev.Employee
7+
(
8+
guid uuid PRIMARY KEY,
9+
state varchar(50) NOT NULL,
10+
status integer NOT NULL
11+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- ############################################################################
2+
-- Create the paylocity.dev.Job table
3+
-- ############################################################################
4+
DROP TABLE IF EXISTS paylocity.dev.Job CASCADE;
5+
6+
CREATE TABLE IF NOT EXISTS paylocity.dev.Job
7+
(
8+
guid uuid PRIMARY KEY,
9+
company_guid uuid NOT NULL,
10+
position_guid uuid NOT NULL,
11+
employee_guid uuid NOT NULL,
12+
FOREIGN KEY (company_guid) REFERENCES paylocity.dev.Company (guid),
13+
FOREIGN KEY (position_guid) REFERENCES paylocity.dev.Position (guid),
14+
FOREIGN KEY (employee_guid) REFERENCES paylocity.dev.Employee (guid),
15+
UNIQUE (company_guid, employee_guid)
16+
);
55.7 KB
Loading

0 commit comments

Comments
 (0)