-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
132 lines (123 loc) · 4.52 KB
/
schema.sql
File metadata and controls
132 lines (123 loc) · 4.52 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
-- Part 1: Schema for database initialization
create table
public.drafts (
draft_id bigint generated by default as identity,
created_at timestamp with time zone not null default (now() at time zone 'utc'::text),
draft_name text not null,
draft_type text not null,
roster_size integer not null,
draft_size integer not null,
draft_status text not null default 'pending'::text,
constraint drafts_pkey primary key (draft_id),
constraint drafts_draft_id_key unique (draft_id)
) tablespace pg_default;
create table
public.position_requirements (
draft_id bigint not null,
position text not null,
min integer not null,
max integer not null,
constraint position_requirements_pkey primary key (draft_id, "position"),
constraint position_requirements_draft_id_fkey foreign key (draft_id) references drafts (draft_id)
) tablespace pg_default;
create table
public.teams (
team_id bigint generated by default as identity,
created_at timestamp with time zone not null default now(),
team_name text not null,
user_name text not null,
draft_position integer null,
draft_id bigint not null,
constraint teams_pkey primary key (team_id),
constraint teams_id_key unique (team_id),
constraint teams_draft_id_fkey foreign key (draft_id) references drafts (draft_id)
) tablespace pg_default;
create table
public.players (
player_name text not null,
player_id text not null,
constraint players_pkey primary key (player_id),
constraint players_player_id2_key unique (player_id)
) tablespace pg_default;
create table
public.stats (
player_id text not null,
year integer not null,
age integer not null,
position text not null,
team text not null,
games_played integer not null,
games_started integer not null,
passing_yards integer not null,
passing_tds integer not null,
interceptions integer not null,
rushing_atts integer not null,
rushing_yards integer not null,
rushing_tds integer not null,
targets integer not null,
receptions integer not null,
receiving_yards integer not null,
receiving_tds integer not null,
fumbles integer not null,
fumbles_lost integer not null,
two_point_conversions integer not null,
two_point_conversions_passing integer not null,
fantasy_points_standard_10 integer not null,
fantasy_points_ppr_10 integer not null,
constraint stats_pkey primary key (player_id, year),
constraint stats_player_id_fkey foreign key (player_id) references players (player_id)
) tablespace pg_default;
create table
public.selections (
team_id bigint not null,
player_id text not null,
when_selected integer not null,
constraint selections_pkey primary key (team_id, player_id),
constraint selections_player_id_fkey foreign key (player_id) references players (player_id),
constraint selections_team_id_fkey foreign key (team_id) references teams (team_id)
) tablespace pg_default;
-- Part 2: Populating initial player data
-- 1) Navigate to your instance of Supabase
-- 2) Click on the "players" table
-- 3) Click "Insert" and "Import" data from CSV
-- 4) Use "players.csv" file from the data folder of the repository
-- 5) Click "Import Data"
-- 6) Click on the "stats" table
-- 7) Click "Insert" and "Import" data from CSV
-- 8) Use "stats.csv" file from the data folder of the repository
-- 9) Click "Import Data"
-- Now your local database will have all the necassary fantasy football player data for the 2019-2023 seasons
-- Part 3: Creating the materialized views.
-- Creating the materialized view for search players endpoint.
-- NOTE: Once the initial data is imported, the materialized view never needs to be updated.
create materialized view
public.player_points as
select
players.player_id,
players.player_name,
stats.year,
stats."position",
stats.team,
stats.age,
stats.fantasy_points_standard_10,
stats.fantasy_points_ppr_10
from
stats
join players on stats.player_id = players.player_id;
-- Creating the materialized view for draft players endpoint.
-- NOTE: Once the initial data is imported, the materialized view never needs to be updated.
create materialized view
public.player_positions as
with recent_stats as (
select player_id, position, ROW_NUMBER() over (partition by player_id order by year desc) as n
from stats
)
select distinct
recent_stats.player_id,
recent_stats."position",
players.player_name
from
players
join recent_stats on players.player_id = recent_stats.player_id
where
recent_stats.n = 1;