Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathcreate.sql
More file actions
Latest commit
252 lines (218 loc) · 6.01 KB
/
Copy pathcreate.sql
File metadata and controls
252 lines (218 loc) · 6.01 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
-- This trigger updates the value in the updated_at column. It is used in the tables below to log
-- when a row was last updated.
CREATE OR REPLACEFUNCTIONtrigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at= NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- USERS
-- This table is used to store the users of our application. The view returns the same data as the
-- table, we're just creating it to follow the pattern used in other tables.
CREATETABLEusers_table
(
id SERIALPRIMARY KEY,
username text UNIQUE NOT NULL,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATETRIGGERusers_updated_at_timestamp
BEFORE UPDATEON users_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATEVIEWusers
AS
SELECT
id,
username,
created_at,
updated_at
FROM
users_table;
-- ITEMS
-- This table is used to store the items associated with each user. The view returns the same data
-- as the table, we're just using both to maintain consistency with our other tables. For more info
-- on the Plaid Item schema, see the docs page: https://plaid.com/docs/#item-schema
CREATETABLEitems_table
(
id SERIALPRIMARY KEY,
user_id integerREFERENCES users_table(id) ON DELETE CASCADE,
plaid_access_token text UNIQUE NOT NULL,
plaid_item_id text UNIQUE NOT NULL,
plaid_institution_id textNOT NULL,
status textNOT NULL,
created_at timestamptz default now(),
updated_at timestamptz default now(),
transactions_cursor text
);
CREATETRIGGERitems_updated_at_timestamp
BEFORE UPDATEON items_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATEVIEWitems
AS
SELECT
id,
plaid_item_id,
user_id,
plaid_access_token,
plaid_institution_id,
status,
created_at,
updated_at,
transactions_cursor
FROM
items_table;
-- -- ASSETS
-- -- This table is used to store the assets associated with each user. The view returns the same data
-- -- as the table, we're just using both to maintain consistency with our other tables.
CREATETABLEassets_table
(
id SERIALPRIMARY KEY,
user_id integerREFERENCES users_table(id) ON DELETE CASCADE,
value numeric(28,2),
description text,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATETRIGGERassets_updated_at_timestamp
BEFORE UPDATEON assets_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATEVIEWassets
AS
SELECT
id,
user_id,
value,
description,
created_at,
updated_at
FROM
assets_table;
-- ACCOUNTS
-- This table is used to store the accounts associated with each item. The view returns all the
-- data from the accounts table and some data from the items view. For more info on the Plaid
-- Accounts schema, see the docs page: https://plaid.com/docs/#account-schema
CREATETABLEaccounts_table
(
id SERIALPRIMARY KEY,
item_id integerREFERENCES items_table(id) ON DELETE CASCADE,
plaid_account_id text UNIQUE NOT NULL,
name textNOT NULL,
mask textNOT NULL,
official_name text,
current_balance numeric(28,10),
available_balance numeric(28,10),
iso_currency_code text,
unofficial_currency_code text,
type textNOT NULL,
subtype textNOT NULL,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATETRIGGERaccounts_updated_at_timestamp
BEFORE UPDATEON accounts_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATEVIEWaccounts
AS
SELECT
a.id,
a.plaid_account_id,
a.item_id,
i.plaid_item_id,
i.user_id,
a.name,
a.mask,
a.official_name,
a.current_balance,
a.available_balance,
a.iso_currency_code,
a.unofficial_currency_code,
a.type,
a.subtype,
a.created_at,
a.updated_at
FROM
accounts_table a
LEFT JOIN items i ONi.id=a.item_id;
-- TRANSACTIONS
-- This table is used to store the transactions associated with each account. The view returns all
-- the data from the transactions table and some data from the accounts view. For more info on the
-- Plaid Transactions schema, see the docs page: https://plaid.com/docs/#transaction-schema
CREATETABLEtransactions_table
(
id SERIALPRIMARY KEY,
account_id integerREFERENCES accounts_table(id) ON DELETE CASCADE,
plaid_transaction_id text UNIQUE NOT NULL,
plaid_category_id text,
category text,
payment_channel textNOT NULL,
name textNOT NULL,
amount numeric(28,10) NOT NULL,
iso_currency_code text,
unofficial_currency_code text,
datedateNOT NULL,
pending booleanNOT NULL,
account_owner text,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
CREATETRIGGERtransactions_updated_at_timestamp
BEFORE UPDATEON transactions_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
CREATEVIEWtransactions
AS
SELECT
t.id,
t.plaid_transaction_id,
t.account_id,
a.plaid_account_id,
a.item_id,
a.plaid_item_id,
a.user_id,
t.category,
t.payment_channel,
t.name,
t.amount,
t.iso_currency_code,
t.unofficial_currency_code,
t.date,
t.pending,
t.account_owner,
t.created_at,
t.updated_at
FROM
transactions_table t
LEFT JOIN accounts a ONt.account_id=a.id;
-- The link_events_table is used to log responses from the Plaid API for client requests to the
-- Plaid Link client. This information is useful for troubleshooting.
CREATETABLElink_events_table
(
id SERIALPRIMARY KEY,
type textNOT NULL,
user_id integer,
link_session_id text,
request_id text UNIQUE,
error_type text,
error_code text,
status text,
created_at timestamptz default now()
);
-- The plaid_api_events_table is used to log responses from the Plaid API for server requests to
-- the Plaid client. This information is useful for troubleshooting.
CREATETABLEplaid_api_events_table
(
id SERIALPRIMARY KEY,
item_id integer,
user_id integer,
plaid_method textNOT NULL,
arguments text,
request_id text UNIQUE,
error_type text,
error_code text,
created_at timestamptz default now()
);