]> git.xonotic.org Git - xonotic/xonstatdb.git/blob - scripts/gen_partitions.shl
47583ae09edd3d55ef2a7c92f8b4c412426c573e
[xonotic/xonstatdb.git] / scripts / gen_partitions.shl
1 #!/bin/bash
2
3 table=$1
4 start_year=$2
5 end_year=$3
6
7 for year in `seq $start_year $end_year`
8 do
9     for qtr in "q1" "q2" "q3" "q4"
10     do
11         printf "CREATE TABLE IF NOT EXISTS xonstat.%s_%s%s ( \n" $table $year $qtr
12         if [[ $qtr = "q1" ]]
13         then
14             printf "\tCHECK ( create_dt >= DATE '%s-01-01' AND create_dt < DATE '%s-04-01' ) \n" $year $year
15         elif [[ $qtr = "q2" ]]
16         then
17             printf "\tCHECK ( create_dt >= DATE '%s-04-01' AND create_dt < DATE '%s-07-01' ) \n" $year $year
18         elif [[ $qtr = "q3" ]]
19         then
20             printf "\tCHECK ( create_dt >= DATE '%s-07-01' AND create_dt < DATE '%s-10-01' ) \n" $year $year
21         elif [[ $qtr = "q4" ]]
22         then
23             next_year=$[year + 1]
24             printf "\tCHECK ( create_dt >= DATE '%s-10-01' AND create_dt < DATE '%s-01-01' ) \n" $year $next_year
25         fi
26
27         printf ") INHERITS (%s);\n\n" $table
28
29         # indexes
30         printf "CREATE INDEX %s_%s%s_ix001 on %s_%s%s(create_dt);\n" $table $year $qtr $table $year $qtr
31
32         # conditional indexes that depend on the table
33         if [[ $table = "games" ]]
34         then
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
38         fi
39         
40         if [[ $table = "player_game_stats" || $table = "player_weapon_stats" ]]
41         then
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
44         fi
45
46         if [[ $table = "team_game_stats" ]]
47         then
48             printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
49         fi
50
51         if [[ $table = "games" ]]
52         then
53             printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s using gin(players);\n" $table $year $qtr $table $year $qtr
54         fi
55
56         printf "\n"
57     done
58 done
59 printf "\n"
60
61 printf "CREATE OR REPLACE FUNCTION %s_ins()\n" $table
62 printf "RETURNS TRIGGER AS \$\$\n"
63 printf "BEGIN\n"
64
65 for i in `seq $start_year $end_year`
66 do
67
68     if [[ start_year -eq i ]]
69     then
70         printf "\tIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
71     else
72         printf "\tELSIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
73     fi
74     printf "\t\tINSERT INTO %s_%sQ1 VALUES (NEW.*);\n" $table $i
75
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
78
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
81
82     next_year=$[i + 1]
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
85
86 done
87
88 printf "\tELSE\n"
89 printf "\t\tRAISE EXCEPTION 'Date out of range. Fix the %s_ins() trigger!';\n" $table
90 printf "\tEND IF;\n"
91 printf "\tRETURN NULL;\n"
92
93 printf "END\n"
94 printf "\$\$\n"
95 printf "LANGUAGE plpgsql;\n\n"
96
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