Skip to content

Commit f634474

Browse files
committed
db: Make baseline migration script
1 parent b674a60 commit f634474

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed

BotCommands-core/src/main/kotlin/io/github/freya022/botcommands/internal/core/db/DatabaseImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import java.sql.Connection
2323
import kotlin.time.toKotlinDuration
2424

2525
// If the build script has 3.0.0-alpha.5_DEV, use the next release version, in this case 3.0.0-alpha.6
26-
private const val latestVersion = "3.0.0-beta.9" // Change in the latest migration script too
26+
private const val latestVersion = "3.0.0" // Change in the latest migration script too
2727

2828
private val logger = KotlinLogging.loggerOf<Database>()
2929

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
SET search_path TO bc;
2+
3+
-- region Setup
4+
CREATE TABLE bc_version
5+
(
6+
one_row bool DEFAULT TRUE,
7+
version text NOT NULL,
8+
9+
PRIMARY KEY (one_row),
10+
CHECK (one_row)
11+
);
12+
13+
INSERT INTO bc_version
14+
VALUES (TRUE, '3.0.0');
15+
-- endregion
16+
17+
-- region Components
18+
CREATE TABLE bc_component
19+
(
20+
component_id serial NOT NULL,
21+
component_type smallint NOT NULL, -- Can also be a group ! (0)
22+
lifetime_type smallint NOT NULL,
23+
one_use bool NOT NULL,
24+
rate_limit_group text NULL,
25+
filters text array NOT NULL,
26+
expires_at timestamp with time zone NULL,
27+
reset_timeout_on_use_duration_ms int NULL,
28+
rate_limit_discriminator text NULL,
29+
30+
PRIMARY KEY (component_id),
31+
32+
CHECK (component_type BETWEEN 0 AND 2),
33+
CHECK (lifetime_type BETWEEN 0 AND 1)
34+
);
35+
36+
CREATE TABLE bc_component_constraints
37+
(
38+
component_id int NOT NULL,
39+
users bigint array NOT NULL,
40+
roles bigint array NOT NULL,
41+
permissions bigint NOT NULL,
42+
43+
PRIMARY KEY (component_id),
44+
FOREIGN KEY (component_id) REFERENCES bc_component ON DELETE CASCADE
45+
);
46+
47+
-- Component types
48+
49+
CREATE TABLE bc_ephemeral_handler
50+
(
51+
component_id int NOT NULL,
52+
handler_id int NOT NULL,
53+
54+
PRIMARY KEY (component_id),
55+
FOREIGN KEY (component_id) REFERENCES bc_component ON DELETE CASCADE
56+
);
57+
58+
CREATE TABLE bc_persistent_handler
59+
(
60+
component_id int NOT NULL,
61+
handler_name text NOT NULL,
62+
user_data bytea array NOT NULL,
63+
64+
PRIMARY KEY (component_id),
65+
FOREIGN KEY (component_id) REFERENCES bc_component ON DELETE CASCADE
66+
);
67+
68+
-- Component timeouts
69+
70+
CREATE TABLE bc_ephemeral_timeout
71+
(
72+
component_id int NOT NULL,
73+
handler_id int NOT NULL,
74+
75+
PRIMARY KEY (component_id),
76+
FOREIGN KEY (component_id) REFERENCES bc_component ON DELETE CASCADE
77+
);
78+
79+
CREATE TABLE bc_persistent_timeout
80+
(
81+
component_id int NOT NULL,
82+
handler_name text NOT NULL,
83+
user_data bytea array NOT NULL,
84+
85+
PRIMARY KEY (component_id),
86+
FOREIGN KEY (component_id) REFERENCES bc_component ON DELETE CASCADE
87+
);
88+
89+
-- Group, bc_component can already be a group
90+
-- Associative table
91+
CREATE TABLE bc_component_component_group
92+
(
93+
group_id int NOT NULL,
94+
component_id int NOT NULL,
95+
96+
FOREIGN KEY (group_id) REFERENCES bc_component ON DELETE CASCADE,
97+
FOREIGN KEY (component_id) REFERENCES bc_component ON DELETE CASCADE,
98+
PRIMARY KEY (group_id, component_id)
99+
);
100+
-- endregion
101+
102+
-- region Application commands cache
103+
CREATE TABLE application_commands_cache
104+
(
105+
application_id bigint NOT NULL,
106+
guild_id bigint NULL,
107+
data text NOT NULL,
108+
metadata text NOT NULL,
109+
110+
UNIQUE NULLS NOT DISTINCT (application_id, guild_id)
111+
);
112+
-- endregion
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
------------------------------------------------------ 10th migration script for BotCommands -----------------------------------------------------
2+
---------------------------------- Make sure to run the previous scripts (chronological order) before this one -----------------------------------
3+
4+
SET SCHEMA 'bc';
5+
6+
UPDATE bc_version
7+
SET version = '3.0.0'
8+
WHERE one_row = true;

0 commit comments

Comments
 (0)