Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is prime branch for all GORM TO SQLBOILER migrations. #1655

Merged
merged 9 commits into from
Jun 19, 2024
4 changes: 2 additions & 2 deletions automod/automod_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ func (p *Plugin) handlePostAutomodUpdateRule(w http.ResponseWriter, r *http.Requ
}
}
if anyMute {
conf, err := moderation.GetConfig(g.ID)
if err != nil || conf.MuteRole == "" {
conf, err := moderation.GetCachedConfigOrDefault(g.ID)
if err != nil || conf.MuteRole == 0 {
tx.Rollback()
tmpl.AddAlerts(web.ErrorAlert("No mute role set, please configure one."))
return tmpl, nil
Expand Down
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
13 changes: 6 additions & 7 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (

"github.com/botlabs-gg/yagpdb/v2/common/cacheset"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/jmoiron/sqlx"
"github.com/mediocregopher/radix/v3"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -30,7 +28,6 @@ import (
var (
VERSION = "unknown"

GORM *gorm.DB
PQ *sql.DB
SQLX *sqlx.DB

Expand Down Expand Up @@ -131,6 +128,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 Expand Up @@ -274,17 +275,15 @@ func connectDB(host, user, pass, dbName string, maxConns int) error {
passwordPart = " password='" + pass + "'"
}

db, err := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable%s", host, user, dbName, passwordPart))
GORM = db
PQ = db.DB()
db, err := sql.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable%s", host, user, dbName, passwordPart))
PQ = db
SQLX = sqlx.NewDb(PQ, "postgres")
boil.SetDB(PQ)
if err == nil {
PQ.SetMaxOpenConns(maxConns)
PQ.SetMaxIdleConns(maxConns)
logger.Infof("Set max PG connections to %d", maxConns)
}
GORM.SetLogger(&GORMLogger{})

return err
}
Expand Down
5 changes: 0 additions & 5 deletions common/configstore/README.md

This file was deleted.

157 changes: 0 additions & 157 deletions common/configstore/db.go

This file was deleted.

70 changes: 0 additions & 70 deletions common/configstore/sql.go

This file was deleted.

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;
`}
7 changes: 0 additions & 7 deletions common/loghooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,6 @@ func DiscordGatewayLogger(shardID int, connectionID int, msgL int, msgf string,
}
}

type GORMLogger struct {
}

func (g *GORMLogger) Print(v ...interface{}) {
logrus.WithField("stck", "...").Error(v...)
}

type LoggingTransport struct {
Inner http.RoundTripper
}
Expand Down
Loading