7 for year in `seq $start_year $end_year`
9 for qtr in "q1" "q2" "q3" "q4"
11 printf "CREATE TABLE IF NOT EXISTS xonstat.%s_%s%s ( \n" $table $year $qtr
14 printf "\tCHECK ( create_dt >= DATE '%s-01-01' AND create_dt < DATE '%s-04-01' ) \n" $year $year
15 elif [[ $qtr = "q2" ]]
17 printf "\tCHECK ( create_dt >= DATE '%s-04-01' AND create_dt < DATE '%s-07-01' ) \n" $year $year
18 elif [[ $qtr = "q3" ]]
20 printf "\tCHECK ( create_dt >= DATE '%s-07-01' AND create_dt < DATE '%s-10-01' ) \n" $year $year
21 elif [[ $qtr = "q4" ]]
24 printf "\tCHECK ( create_dt >= DATE '%s-10-01' AND create_dt < DATE '%s-01-01' ) \n" $year $next_year
27 printf ") INHERITS (%s);\n\n" $table
30 printf "CREATE INDEX %s_%s%s_ix001 on %s_%s%s(create_dt);\n" $table $year $qtr $table $year $qtr
32 # conditional indexes that depend on the table
33 if [[ $table = "games" ]]
35 printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s using gin(players);\n" $table $year $qtr $table $year $qtr
36 # TODO(divVerent): This index probably should be a primary key instead.
37 printf "CREATE INDEX %s_%s%s_ix003 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
40 if [[ $table = "player_game_stats" || $table = "player_weapon_stats" ]]
42 printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
43 printf "CREATE INDEX %s_%s%s_ix003 on %s_%s%s(player_id);\n" $table $year $qtr $table $year $qtr
46 if [[ $table = "team_game_stats" ]]
48 printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
51 if [[ $table = "games" ]]
53 printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s using gin(players);\n" $table $year $qtr $table $year $qtr
61 printf "CREATE OR REPLACE FUNCTION %s_ins()\n" $table
62 printf "RETURNS TRIGGER AS \$\$\n"
65 for i in `seq $start_year $end_year`
68 if [[ start_year -eq i ]]
70 printf "\tIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
72 printf "\tELSIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
74 printf "\t\tINSERT INTO %s_%sQ1 VALUES (NEW.*);\n" $table $i
76 printf "\tELSIF (NEW.create_dt >= DATE '%s-04-01' AND NEW.create_dt < DATE '%s-07-01') THEN\n" $i $i
77 printf "\t\tINSERT INTO %s_%sQ2 VALUES (NEW.*);\n" $table $i
79 printf "\tELSIF (NEW.create_dt >= DATE '%s-07-01' AND NEW.create_dt < DATE '%s-10-01') THEN\n" $i $i
80 printf "\t\tINSERT INTO %s_%sQ3 VALUES (NEW.*);\n" $table $i
83 printf "\tELSIF (NEW.create_dt >= DATE '%s-10-01' AND NEW.create_dt < DATE '%s-01-01') THEN\n" $i $next_year
84 printf "\t\tINSERT INTO %s_%sQ4 VALUES (NEW.*);\n" $table $i
89 printf "\t\tRAISE EXCEPTION 'Date out of range. Fix the %s_ins() trigger!';\n" $table
91 printf "\tRETURN NULL;\n"
95 printf "LANGUAGE plpgsql;\n\n"
97 printf "DROP TRIGGER IF EXISTS %s_ins_trg ON xonstat.%s;\n" $table $table
98 printf "CREATE TRIGGER %s_ins_trg\n" $table
99 printf "BEFORE INSERT on xonstat.%s\n" $table
100 printf "FOR EACH ROW EXECUTE PROCEDURE %s_ins();\n" $table