Skip to content

Commit

Permalink
all: migrate executed command logging away from gorm to sqlboiler (#1654
Browse files Browse the repository at this point in the history
)

* common: add schemas for executed command logs

* common: configure and run sqlboiler for executed command log model

* all: migrate executed command logging to sqlboiler
  • Loading branch information
jo3-l authored and ashishjh-bst committed Jun 19, 2024
1 parent 3615e45 commit e0960d9
Show file tree
Hide file tree
Showing 11 changed files with 1,064 additions and 58 deletions.
4 changes: 0 additions & 4 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ func (p *Plugin) PluginInfo() *common.PluginInfo {
func RegisterPlugin() {
plugin := &Plugin{}
common.RegisterPlugin(plugin)
err := common.GORM.AutoMigrate(&common.LoggedExecutedCommand{}).Error
if err != nil {
logger.WithError(err).Fatal("Failed migrating logged commands database")
}

common.InitSchemas("commands", DBSchemas...)
}
Expand Down
10 changes: 6 additions & 4 deletions commands/yagcommmand.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries/qm"

commonmodels "github.com/botlabs-gg/yagpdb/v2/common/models"
)

type ContextKey int
Expand Down Expand Up @@ -233,7 +236,7 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) {
}

// Set up log entry for later use
logEntry := &common.LoggedExecutedCommand{
logEntry := &commonmodels.ExecutedCommand{
UserID: discordgo.StrID(data.Author.ID),
ChannelID: discordgo.StrID(data.ChannelID),

Expand All @@ -243,8 +246,7 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) {
}

if data.GuildData != nil {
logEntry.GuildID = discordgo.StrID(data.GuildData.GS.ID)

logEntry.GuildID.SetValid(discordgo.StrID(data.GuildData.GS.ID))
}

metricsExcecutedCommands.With(prometheus.Labels{"name": "(other)", "trigger_type": triggerType}).Inc()
Expand Down Expand Up @@ -284,7 +286,7 @@ func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) {
}

// Create command log entry
err := common.GORM.Create(logEntry).Error
err := logEntry.InsertG(data.Context(), boil.Infer())
if err != nil {
logger.WithError(err).Error("Failed creating command execution log")
}
Expand Down
4 changes: 4 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func Init() error {

logger.Info("Initializing core schema")
InitSchemas("core_configs", CoreServerConfDBSchema, localIDsSchema)

logger.Info("Initializing executed command log schema")
InitSchemas("executed_commands", ExecutedCommandDBSchemas...)

initQueuedSchemas()

return err
Expand Down
39 changes: 39 additions & 0 deletions common/executedcommandschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package common

var ExecutedCommandDBSchemas = []string{`
CREATE TABLE IF NOT EXISTS executed_commands (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
user_id TEXT NOT NULL, -- text not bigint for legacy compatibility
channel_id TEXT NOT NULL,
guild_id TEXT,
command TEXT NOT NULL,
raw_command TEXT NOT NULL,
error TEXT,
time_stamp TIMESTAMP WITH TIME ZONE NOT NULL,
response_time BIGINT NOT NULL
);
`, `
-- Preexisting tables created prior to sqlboiler are missing non-null constraints,
-- so add them retraoctively.
ALTER TABLE executed_commands ALTER COLUMN created_at SET NOT NULL;
`, `
ALTER TABLE executed_commands ALTER COLUMN updated_at SET NOT NULL;
`, `
ALTER TABLE executed_commands ALTER COLUMN user_id SET NOT NULL;
`, `
ALTER TABLE executed_commands ALTER COLUMN channel_id SET NOT NULL;
`, `
ALTER TABLE executed_commands ALTER COLUMN command SET NOT NULL;
`, `
ALTER TABLE executed_commands ALTER COLUMN raw_command SET NOT NULL;
`, `
ALTER TABLE executed_commands ALTER COLUMN time_stamp SET NOT NULL;
`, `
ALTER TABLE executed_commands ALTER COLUMN response_time SET NOT NULL;
`}
6 changes: 4 additions & 2 deletions common/models/boil_table_names.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions common/models/boil_view_names.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e0960d9

Please sign in to comment.