Skip to content

Commit

Permalink
all: seperate db schemas into multiple parts to avoid deadlocks on st…
Browse files Browse the repository at this point in the history
…artup
  • Loading branch information
jogramming committed Jul 20, 2019
1 parent 416740f commit f0749bf
Show file tree
Hide file tree
Showing 28 changed files with 120 additions and 79 deletions.
2 changes: 1 addition & 1 deletion automod/automod.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (p *Plugin) PluginInfo() *common.PluginInfo {
func RegisterPlugin() {
RegexCache = ccache.New(ccache.Configure())

common.InitSchema(DBSchema, "automod_v2")
common.InitSchemas("automod_v2", DBSchemas...)

p := &Plugin{}
common.RegisterPlugin(p)
Expand Down
20 changes: 17 additions & 3 deletions automod/db_schema.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package automod

const DBSchema = `
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS automod_rulesets (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
name TEXT NOT NULL,
enabled BOOLEAN NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS automod_rulesets_guild_idx ON automod_rulesets(guild_id);
`, `
CREATE TABLE IF NOT EXISTS automod_rules (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -20,8 +21,10 @@ CREATE TABLE IF NOT EXISTS automod_rules (
trigger_counter BIGINT NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS automod_rules_guild_idx ON automod_rules(guild_id);
`, `
CREATE TABLE IF NOT EXISTS automod_rule_data (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -32,8 +35,10 @@ CREATE TABLE IF NOT EXISTS automod_rule_data (
settings JSONB NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS automod_rule_data_guild_idx ON automod_rule_data(guild_id);
`, `
CREATE TABLE IF NOT EXISTS automod_ruleset_conditions (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -44,8 +49,10 @@ CREATE TABLE IF NOT EXISTS automod_ruleset_conditions (
settings JSONB NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS automod_ruleset_conditions_guild_idx ON automod_ruleset_conditions(guild_id);
`, `
CREATE TABLE IF NOT EXISTS automod_violations (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -57,9 +64,12 @@ CREATE TABLE IF NOT EXISTS automod_violations (
name TEXT NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS automod_violations_guild_idx ON automod_violations(guild_id);
`, `
CREATE INDEX IF NOT EXISTS automod_violations_user_idx ON automod_violations(user_id);
`, `
CREATE TABLE IF NOT EXISTS automod_lists (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -69,8 +79,10 @@ CREATE TABLE IF NOT EXISTS automod_lists (
content TEXT[] NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS automod_lists_guild_idx ON automod_lists(guild_id);
`, `
CREATE TABLE IF NOT EXISTS automod_triggered_rules (
id BIGSERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
Expand All @@ -90,7 +102,9 @@ CREATE TABLE IF NOT EXISTS automod_triggered_rules (
extradata JSONB NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS automod_triggered_rules_guild_idx ON automod_triggered_rules(guild_id);
`, `
CREATE INDEX IF NOT EXISTS automod_triggered_rules_trigger_idx ON automod_triggered_rules(trigger_id);
`
`}
2 changes: 1 addition & 1 deletion bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
)

func setup() {
common.InitSchema(DBSchema, "core_bot")
common.InitSchemas("core_bot", DBSchema)

discordgo.IdentifyRatelimiter = &identifyRatelimiter{}

Expand Down
2 changes: 1 addition & 1 deletion commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func RegisterPlugin() {
logger.WithError(err).Fatal("Failed migrating logged commands database")
}

common.InitSchema(DBSchema, "commands")
common.InitSchemas("commands", DBSchemas...)
}

type CommandProvider interface {
Expand Down
12 changes: 6 additions & 6 deletions commands/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package commands

const DBSchema = `
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS commands_channels_overrides (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -20,10 +20,11 @@ CREATE TABLE IF NOT EXISTS commands_channels_overrides (
require_roles BIGINT[] NOT NULL,
ignore_roles BIGINT[] NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS commands_channels_overrides_guild_idx ON commands_channels_overrides(guild_id);
`, `
CREATE UNIQUE INDEX IF NOT EXISTS commands_channels_overrides_global_uniquex ON commands_channels_overrides (guild_id) WHERE global;
`, `
CREATE TABLE IF NOT EXISTS commands_command_overrides (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -42,7 +43,6 @@ CREATE TABLE IF NOT EXISTS commands_command_overrides (
require_roles BIGINT[] NOT NULL,
ignore_roles BIGINT[] NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS commands_command_groups_channels_override_idx ON commands_command_overrides(commands_channels_overrides_id);
`
`}
6 changes: 3 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Init() error {
panic(err)
}

InitSchema(CoreServerConfDBSchema, "core_configs")
InitSchemas("core_configs", CoreServerConfDBSchema)

return err
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func connectDB(host, user, pass, dbName string) error {
return err
}

func InitSchema(schema string, name string) {
func initSchema(schema string, name string) {
_, err := PQ.Exec(schema)
if err != nil {
logger.WithError(err).Fatal("failed initializing postgres db schema for ", name)
Expand All @@ -248,7 +248,7 @@ func InitSchema(schema string, name string) {
func InitSchemas(name string, schemas ...string) {
for i, v := range schemas {
actualName := fmt.Sprintf("%s[%d]", name, i)
InitSchema(v, actualName)
initSchema(v, actualName)
}

return
Expand Down
2 changes: 1 addition & 1 deletion common/scheduledevents2/scheduledevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (p *ScheduledEvents) PluginInfo() *common.PluginInfo {
}

func RegisterPlugin() {
common.InitSchema(DBSchema, "scheduledevents2")
common.InitSchemas("scheduledevents2", DBSchemas...)

common.RegisterPlugin(newScheduledEventsPlugin())
}
Expand Down
6 changes: 3 additions & 3 deletions common/scheduledevents2/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scheduledevents2

const DBSchema = `
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS scheduled_events (
id BIGSERIAL PRIMARY KEY,
Expand All @@ -15,6 +15,6 @@ CREATE TABLE IF NOT EXISTS scheduled_events (
processed BOOL not null
);
`, `
CREATE INDEX IF NOT EXISTS scheduled_events_triggers_at_idx ON scheduled_events(triggers_at);
`
`}
2 changes: 1 addition & 1 deletion customcommands/customcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func KeyCommands(guildID int64) string { return "custom_commands:" + discordgo.S
type Plugin struct{}

func RegisterPlugin() {
common.InitSchema(DBSchema, "customcommands")
common.InitSchemas("customcommands", DBSchemas...)

plugin := &Plugin{}
common.RegisterPlugin(plugin)
Expand Down
12 changes: 9 additions & 3 deletions customcommands/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package customcommands

const DBSchema = `
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS custom_command_groups (
id BIGSERIAL PRIMARY KEY,
Expand All @@ -13,7 +13,7 @@ CREATE TABLE IF NOT EXISTS custom_command_groups (
whitelist_roles BIGINT[],
whitelist_channels BIGINT[]
);
`, `
CREATE TABLE IF NOT EXISTS custom_commands (
local_id BIGINT NOT NULL,
guild_id BIGINT NOT NULL,
Expand All @@ -40,12 +40,16 @@ CREATE TABLE IF NOT EXISTS custom_commands (
PRIMARY KEY(guild_id, local_id)
);
`, `
CREATE INDEX IF NOT EXISTS custom_commands_guild_idx ON custom_commands(guild_id);
`, `
CREATE INDEX IF NOT EXISTS custom_commands_next_run_idx ON custom_commands(next_run);
`, `
ALTER TABLE custom_commands ADD COLUMN IF NOT EXISTS context_channel BIGINT NOT NULL DEFAULT 0;
`, `
CREATE TABLE IF NOT EXISTS templates_user_database (
id BIGSERIAL PRIMARY KEY,
Expand All @@ -63,6 +67,8 @@ CREATE TABLE IF NOT EXISTS templates_user_database (
UNIQUE(guild_id, user_id, key)
);
`, `
CREATE INDEX IF NOT EXISTS templates_user_database_combined_idx ON templates_user_database (guild_id, user_id, key, value_num);
`, `
CREATE INDEX IF NOT EXISTS templates_user_database_expires_idx ON templates_user_database (expires_at);
`
`}
2 changes: 1 addition & 1 deletion premium/premium.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (p *Plugin) PluginInfo() *common.PluginInfo {
}

func RegisterPlugin() {
common.InitSchema(DBSchema, "premium")
common.InitSchemas("premium", DBSchemas...)

common.RegisterPlugin(&Plugin{})

Expand Down
8 changes: 4 additions & 4 deletions premium/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package premium

const DBSchema = `
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS premium_slots (
id BIGSERIAL PRIMARY KEY,
Expand All @@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS premium_slots (
permanent BOOLEAN NOT NULL,
duration_remaining BIGINT NOT NULL
);
`, `
CREATE TABLE IF NOT EXISTS premium_codes (
id BIGSERIAL PRIMARY KEY,
Expand All @@ -36,6 +36,6 @@ CREATE TABLE IF NOT EXISTS premium_codes (
permanent BOOLEAN NOT NULL,
duration BIGINT NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS premium_codes_code_idx ON premium_codes(code);
`
`}
2 changes: 1 addition & 1 deletion reddit/reddit.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (p *Plugin) WebhookAvatar() string {
}

func RegisterPlugin() {
common.InitSchema(DBSchema, "reddit")
common.InitSchemas("reddit", DBSchemas...)

plugin := &Plugin{
stopFeedChan: make(chan *sync.WaitGroup),
Expand Down
9 changes: 5 additions & 4 deletions reddit/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package reddit

const DBSchema = `
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS reddit_feeds (
id BIGSERIAL PRIMARY KEY,
guild_id BIGINT NOT NULL,
Expand All @@ -14,10 +14,11 @@ CREATE TABLE IF NOT EXISTS reddit_feeds (
use_embeds BOOLEAN NOT NULL,
slow BOOLEAN NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS redidt_feeds_guild_idx ON reddit_feeds(guild_id);
`, `
CREATE INDEX IF NOT EXISTS redidt_feeds_subreddit_idx ON reddit_feeds(subreddit);
`, `
ALTER TABLE reddit_feeds ADD COLUMN IF NOT EXISTS disabled BOOLEAN NOT NULL DEFAULT FALSE;
`
`}
2 changes: 1 addition & 1 deletion reputation/reputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func RegisterPlugin() {

plugin := &Plugin{}

common.InitSchema(DBSchema, "reputation")
common.InitSchemas("reputation", DBSchemas...)

common.RegisterPlugin(plugin)
}
Expand Down
22 changes: 11 additions & 11 deletions reputation/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package reputation

const DBSchema = `
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS reputation_configs (
guild_id bigint PRIMARY KEY,
points_name varchar(50) NOT NULL,
Expand All @@ -14,9 +14,9 @@ CREATE TABLE IF NOT EXISTS reputation_configs (
blacklisted_receive_role varchar(30),
admin_role varchar(30)
);
`, `
ALTER TABLE reputation_configs ADD COLUMN IF NOT EXISTS disable_thanks_detection BOOLEAN NOT NULL DEFAULT false;
`, `
DO $$
BEGIN
Expand Down Expand Up @@ -54,11 +54,8 @@ IF (SELECT COUNT(*) FROM information_schema.columns WHERE table_name='reputation
UPDATE reputation_configs SET blacklisted_receive_roles=ARRAY[blacklisted_receive_role]::BIGINT[] WHERE blacklisted_receive_role IS NOT NULL AND blacklisted_receive_role != '';
END IF;
END $$;
`, `
CREATE TABLE IF NOT EXISTS reputation_users (
user_id bigint NOT NULL,
guild_id bigint NOT NULL,
Expand All @@ -68,7 +65,7 @@ CREATE TABLE IF NOT EXISTS reputation_users (
PRIMARY KEY(guild_id, user_id)
);
`, `
CREATE TABLE IF NOT EXISTS reputation_log (
id bigserial PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
Expand All @@ -79,11 +76,14 @@ CREATE TABLE IF NOT EXISTS reputation_log (
set_fixed_amount bool NOT NULL,
amount bigint NOT NULL
);
`, `
ALTER TABLE reputation_log ADD COLUMN IF NOT EXISTS receiver_username TEXT NOT NULL DEFAULT '';
`, `
ALTER TABLE reputation_log ADD COLUMN IF NOT EXISTS sender_username TEXT NOT NULL DEFAULT '';
`, `
CREATE INDEX IF NOT EXISTS reputation_log_guild_idx ON reputation_log (guild_id);
`, `
CREATE INDEX IF NOT EXISTS reputation_log_sender_idx ON reputation_log (sender_id);
`, `
CREATE INDEX IF NOT EXISTS reputation_log_receiver_idx ON reputation_log (receiver_id);
`
`}
2 changes: 1 addition & 1 deletion rolecommands/rolecommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func RegisterPlugin() {
p := &Plugin{}
common.RegisterPlugin(p)

common.InitSchema(DBSchema, "rolecommands")
common.InitSchemas("rolecommands", DBSchemas...)
}

func FindToggleRole(ctx context.Context, ms *dstate.MemberState, name string) (gaveRole bool, err error) {
Expand Down
Loading

0 comments on commit f0749bf

Please sign in to comment.