diff options
| -rw-r--r-- | common/config.go | 12 | ||||
| -rw-r--r-- | common/context.go | 9 | ||||
| -rw-r--r-- | go.mod | 1 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | migrations/000-leave-gorm.sql | 44 |
5 files changed, 55 insertions, 13 deletions
diff --git a/common/config.go b/common/config.go index 8cb74ab..5519690 100644 --- a/common/config.go +++ b/common/config.go @@ -1,12 +1,12 @@ package common import ( + "database/sql" "fmt" "os" + _ "github.com/lib/pq" "github.com/pelletier/go-toml/v2" - "gorm.io/driver/postgres" - "gorm.io/gorm" ) type Config struct { @@ -31,12 +31,8 @@ func (p *PostgresConfig) SetDefaultValues() { p.Port = 5432 } -func (p *PostgresConfig) Connect() (*gorm.DB, error) { - db, err := gorm.Open(postgres.Open(p.generateDsn()), &gorm.Config{}) - if err != nil { - return nil, err - } - return db, nil +func (p *PostgresConfig) Connect() (*sql.DB, error) { + return sql.Open("postgres", p.generateDsn()) } // generateDsn for the connection to postgres using the given config.SQLCredentials diff --git a/common/context.go b/common/context.go index 740e727..8bb02b2 100644 --- a/common/context.go +++ b/common/context.go @@ -2,8 +2,7 @@ package common import ( "context" - - "gorm.io/gorm" + "database/sql" ) type Key uint8 @@ -15,16 +14,16 @@ const ( KeyCopaingState Key = 3 ) -func SetDB(ctx context.Context, db *gorm.DB) context.Context { +func SetDB(ctx context.Context, db *sql.DB) context.Context { return context.WithValue(ctx, keyDB, db) } -func GetDB(ctx context.Context) *gorm.DB { +func GetDB(ctx context.Context) *sql.DB { raw := ctx.Value(keyDB) if raw == nil { return nil } - return raw.(*gorm.DB).WithContext(ctx) + return raw.(*sql.DB) } func SetDebug(ctx context.Context, b bool) context.Context { @@ -29,6 +29,7 @@ require ( github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/lib/pq v1.11.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/redis/go-redis/v9 v9.16.0 // indirect golang.org/x/crypto v0.47.0 // indirect @@ -49,6 +49,8 @@ github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/lib/pq v1.11.2 h1:x6gxUeu39V0BHZiugWe8LXZYZ+Utk7hSJGThs8sdzfs= +github.com/lib/pq v1.11.2/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA= github.com/nyttikord/gokord v0.33.1 h1:kBDqz/2dmENOW+zQ8Iv3p9Iv3MQ59XFT9wI+NrF3mtE= github.com/nyttikord/gokord v0.33.1/go.mod h1:PS/ntyzpniZyArWmFa5OvOn6qJ5VnzRlRdDlTlcK3+c= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= diff --git a/migrations/000-leave-gorm.sql b/migrations/000-leave-gorm.sql new file mode 100644 index 0000000..91addd8 --- /dev/null +++ b/migrations/000-leave-gorm.sql @@ -0,0 +1,44 @@ +CREATE DOMAIN snowflake AS NUMERIC(20,0) CHECK (VALUE < 36893488147419103232 and CHECK VALUE > 0); +CREATE DOMAIN uint AS INTEGER CHECK (VALUE >= 0); + +CREATE TABLE IF NOT EXISTS guilds ( + id snowflake PRIMARY KEY, + disabled_channels TEXT, + fallback_channel snowflake, + days_xp_remains INTEGER CHECK (VALUE > 0) DEFAULT 90 +); + +CREATE TABLE IF NOT EXISTS copaings ( + id snowflake PRIMARY KEY, + guild_id snowflake REFERENCES guilds(id) NOT NULL +); + +CREATE TABLE IF NOT EXISTS copaing_xps ( + copaing_id snowflake REFERENCES copaings(id) NOT NULL, + guild_id snowflake REFERENCES copaings(id) NOT NULL, + xp uint DEFAULT 0, + created_at TIMESTAMP DEFAULT NOW, + PRIMARY KEY(copaing_id, guild_id, created_at) +); + +CREATE TABLE IF NOT EXISTS xp_roles ( + xp uint, + role snowflake, + guild_id snowflake REFERENCES guilds(id), + PRIMARY KEY(xp, role, guild_id) +); + +CREATE TABLE IF NOT EXISTS role_react_messages ( + id SERIAL PRIMARY KEY, + message_id snowflake, + channel_id snowflake, + guild_id snowflake REFERENCES guilds(id), + note TEXT +); + +CREATE TABLE IF NOT EXISTS role_reacts ( + role snowflake, + message_id snowflake REFERENCES role_react_messages(id), + reaction TEXT, + PRIMARY KEY(role, message_id, reaction) +); |
