]> de.git.xonotic.org Git - xonotic/xonstatdb.git/blob - scripts/gen_partitions.shl
Store a list of player_ids in the games table.
[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 = "player_game_stats" || $table = "player_weapon_stats" ]]
34         then
35             printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
36             printf "CREATE INDEX %s_%s%s_ix003 on %s_%s%s(player_id);\n" $table $year $qtr $table $year $qtr
37         fi
38
39         if [[ $table = "team_game_stats" ]]
40         then
41             printf "CREATE INDEX %s_%s%s_ix002 on %s_%s%s(game_id);\n" $table $year $qtr $table $year $qtr
42         fi
43         printf "\n"
44     done
45 done
46 printf "\n"
47
48 printf "CREATE OR REPLACE FUNCTION %s_ins()\n" $table
49 printf "RETURNS TRIGGER AS \$\$\n"
50 printf "BEGIN\n"
51
52 for i in `seq $start_year $end_year`
53 do
54
55     if [[ start_year -eq i ]]
56     then
57         printf "\tIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
58     else
59         printf "\tELSIF (NEW.create_dt >= DATE '%s-01-01' AND NEW.create_dt < DATE '%s-04-01') THEN\n" $i $i
60     fi
61     printf "\t\tINSERT INTO %s_%sQ1 VALUES (NEW.*);\n" $table $i
62
63     printf "\tELSIF (NEW.create_dt >= DATE '%s-04-01' AND NEW.create_dt < DATE '%s-07-01') THEN\n" $i $i
64     printf "\t\tINSERT INTO %s_%sQ2 VALUES (NEW.*);\n" $table $i
65
66     printf "\tELSIF (NEW.create_dt >= DATE '%s-07-01' AND NEW.create_dt < DATE '%s-10-01') THEN\n" $i $i
67     printf "\t\tINSERT INTO %s_%sQ3 VALUES (NEW.*);\n" $table $i
68
69     next_year=$[i + 1]
70     printf "\tELSIF (NEW.create_dt >= DATE '%s-10-01' AND NEW.create_dt < DATE '%s-01-01') THEN\n" $i $next_year
71     printf "\t\tINSERT INTO %s_%sQ4 VALUES (NEW.*);\n" $table $i
72
73 done
74
75 printf "\tELSE\n"
76 printf "\t\tRAISE EXCEPTION 'Date out of range. Fix the %s_ins() trigger!';\n" $table
77 printf "\tEND IF;\n"
78 printf "\tRETURN NULL;\n"
79
80 printf "END\n"
81 printf "\$\$\n"
82 printf "LANGUAGE plpgsql;\n\n"
83
84 printf "DROP TRIGGER IF EXISTS %s_ins_trg ON xonstat.%s;\n" $table $table
85 printf "CREATE TRIGGER %s_ins_trg\n" $table
86 printf "BEFORE INSERT on xonstat.%s\n" $table
87 printf "FOR EACH ROW EXECUTE PROCEDURE %s_ins();\n" $table