Skip to content

Commit

Permalink
update database setup and removed vendor files
Browse files Browse the repository at this point in the history
  • Loading branch information
zekroTJA committed Jun 22, 2019
1 parent 4fcd97d commit 49b4716
Show file tree
Hide file tree
Showing 47 changed files with 262 additions and 4,732 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ private.*
bin/*
*.exe
*.db
guildBackups/*
guildBackups/*
vendor/*
24 changes: 16 additions & 8 deletions Gopkg.lock

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

106 changes: 86 additions & 20 deletions internal/core/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package core

import (
"database/sql"
"encoding/base64"
"errors"
"fmt"
"regexp"
"strings"
"time"

"github.com/zekroTJA/shinpuru/internal/util"
"github.com/zekroTJA/shinpuru/pkg/multierror"

"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/snowflake"
Expand All @@ -21,23 +19,91 @@ type MySQL struct {
}

func (m *MySQL) setup() {
if MySqlDbSchemeB64 == "" {
util.Log.Warning("MySql database scheme was not set on compiling. Database can not be checked for structure changes!")
return
}
scheme, err := base64.StdEncoding.DecodeString(MySqlDbSchemeB64)
if err != nil {
util.Log.Fatal("failed decoding base64 database scheme: ", err)
return
}
for _, query := range strings.Split(string(scheme), ";") {
if ok, _ := regexp.MatchString(`\w`, query); ok {
fmt.Println(query)
_, err = m.DB.Exec(query)
if err != nil {
util.Log.Errorf("Failed executing setup database query '%s': %s", query, err.Error())
}
}
mErr := multierror.New(nil)

_, err := m.DB.Exec("CREATE TABLE IF NOT EXISTS `guilds` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`guildID` text NOT NULL," +
"`prefix` text NOT NULL," +
"`autorole` text NOT NULL," +
"`modlogchanID` text NOT NULL," +
"`voicelogchanID` text NOT NULL," +
"`muteRoleID` text NOT NULL," +
"`ghostPingMsg` text NOT NULL," +
"`jdoodleToken` text NOT NULL," +
"`backup` text NOT NULL," +
"`inviteBlock` text NOT NULL," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `permissions` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`roleID` text NOT NULL," +
"`guildID` text NOT NULL," +
"`permission` int(11) NOT NULL," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `reports` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`id` text NOT NULL," +
"`type` int(11) NOT NULL," +
"`guildID` text NOT NULL," +
"`executorID` text NOT NULL," +
"`victimID` text NOT NULL," +
"`msg` text NOT NULL," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `settings` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`setting` text NOT NULL," +
"`value` text NOT NULL," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `starboard` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`guildID` text NOT NULL," +
"`chanID` text NOT NULL," +
"`enabled` tinyint(1) NOT NULL DEFAULT '1'," +
"`minimum` int(11) NOT NULL DEFAULT '5'," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `votes` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`id` text NOT NULL," +
"`data` mediumtext NOT NULL," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `twitchnotify` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`guildID` text NOT NULL," +
"`channelID` text NOT NULL," +
"`twitchUserID` text NOT NULL," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `backups` (" +
"`iid` int(11) NOT NULL AUTO_INCREMENT," +
"`guildID` text NOT NULL," +
"`timestamp` bigint(20) NOT NULL," +
"`fileID` text NOT NULL," +
"PRIMARY KEY (`iid`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;")
mErr.Append(err)

if mErr.Len() > 0 {
util.Log.Fatalf("Failed database setup: %s", mErr.Concat().Error())
}
}

Expand Down
105 changes: 86 additions & 19 deletions internal/core/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package core

import (
"database/sql"
"encoding/base64"
"errors"
"fmt"
"regexp"
"strings"
"time"

"github.com/zekroTJA/shinpuru/pkg/multierror"

"github.com/zekroTJA/shinpuru/internal/util"

"github.com/bwmarrin/discordgo"
Expand All @@ -21,22 +20,90 @@ type Sqlite struct {
}

func (m *Sqlite) setup() {
if SqliteDbSchemeB64 == "" {
util.Log.Warning("sqlite database scheme was not set on compiling. Database can not be checked for structure changes!")
return
}
scheme, err := base64.StdEncoding.DecodeString(SqliteDbSchemeB64)
if err != nil {
util.Log.Fatal("failed decoding base64 database scheme: ", err)
return
}
for _, query := range strings.Split(string(scheme), ";") {
if ok, _ := regexp.MatchString(`\w`, query); ok {
_, err = m.DB.Exec(query)
if err != nil {
util.Log.Error("Failed executing setup database query: ", err)
}
}
mErr := multierror.New(nil)

_, err := m.DB.Exec("CREATE TABLE IF NOT EXISTS `guilds` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`guildID` text NOT NULL DEFAULT ''," +
"`prefix` text NOT NULL DEFAULT ''," +
"`autorole` text NOT NULL DEFAULT ''," +
"`modlogchanID` text NOT NULL DEFAULT ''," +
"`voicelogchanID` text NOT NULL DEFAULT ''," +
"`muteRoleID` text NOT NULL DEFAULT ''," +
"`ghostPingMsg` text NOT NULL DEFAULT ''," +
"`jdoodleToken` text NOT NULL DEFAULT ''," +
"`backup` text NOT NULL DEFAULT ''," +
"`inviteBlock` text NOT NULL DEFAULT ''" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `permissions` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`roleID` text NOT NULL DEFAULT ''," +
"`guildID` text NOT NULL DEFAULT ''," +
"`permission` int(11) NOT NULL DEFAULT '0'" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `reports` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`id` text NOT NULL DEFAULT ''," +
"`type` int(11) NOT NULL DEFAULT '3'," +
"`guildID` text NOT NULL DEFAULT ''," +
"`executorID` text NOT NULL DEFAULT ''," +
"`victimID` text NOT NULL DEFAULT ''," +
"`msg` text NOT NULL DEFAULT ''" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `settings` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`setting` text NOT NULL DEFAULT ''," +
"`value` text NOT NULL DEFAULT ''" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `settings` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`setting` text NOT NULL DEFAULT ''," +
"`value` text NOT NULL DEFAULT ''" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `starboard` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`guildID` text NOT NULL DEFAULT ''," +
"`chanID` text NOT NULL DEFAULT ''," +
"`enabled` tinyint(1) NOT NULL DEFAULT '1'," +
"`minimum` int(11) NOT NULL DEFAULT '5'" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `votes` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`id` text NOT NULL DEFAULT ''," +
"`data` mediumtext NOT NULL DEFAULT ''" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `twitchnotify` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`guildID` text NOT NULL DEFAULT ''," +
"`channelID` text NOT NULL DEFAULT ''," +
"`twitchUserID` text NOT NULL DEFAULT ''" +
");")
mErr.Append(err)

_, err = m.DB.Exec("CREATE TABLE IF NOT EXISTS `backups` (" +
"`iid` INTEGER PRIMARY KEY AUTOINCREMENT," +
"`guildID` text NOT NULL DEFAULT ''," +
"`timestamp` bigint(20) NOT NULL DEFAULT 0," +
"`fileID` text NOT NULL DEFAULT ''" +
");")
mErr.Append(err)

if mErr.Len() > 0 {
util.Log.Fatalf("Failed database setup: %s", mErr.Concat().Error())
}
}

Expand Down
72 changes: 72 additions & 0 deletions pkg/multierror/multierror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Package multierror impements handling
// multiple errors as one error object.
// Authors: Ringo Hoffmann.
package multierror

import (
"errors"
"fmt"
"strings"
)

// FormatFunc is the function used to format an
// error element of a MulitError
type FormatFunc func(i int, e error) string

// MultiError contains multiple error objects
// and the frmatting function for concating into
// one handable error object
type MultiError struct {
errors []error
formatFunc FormatFunc
}

// New creates a new instance of MultiError
// using the passed format function. If this argument
// is nil, the default format function will be used.
func New(formatFunc FormatFunc) *MultiError {
if formatFunc == nil {
formatFunc = func(i int, e error) string {
return fmt.Sprintf("MultiError %02x: %s\n", i, e)
}
}

return &MultiError{
formatFunc: formatFunc,
}
}

// Append adds an error object to the
// MultiError cotainer if the error
// is != nil
func (m *MultiError) Append(err error) {
if err != nil {
m.errors = append(m.errors, err)
}
}

// Len returns the ammount of errors contained
// in the MultiError container
func (m *MultiError) Len() int {
return len(m.errors)
}

// Concat creates one handable error object
// from all errors in the MultiError container
// using the formatting function.
func (m *MultiError) Concat() error {
if m.Len() == 0 {
return nil
}

if m.Len() == 1 {
return m.errors[0]
}

strErrArr := make([]string, m.Len())
for i, e := range m.errors {
strErrArr[i] = m.formatFunc(i, e)
}

return errors.New(strings.Join(strErrArr, ""))
}
1 change: 0 additions & 1 deletion vendor/github.com/bwmarrin/discordgo
Submodule discordgo deleted from 8325a6
1 change: 0 additions & 1 deletion vendor/github.com/bwmarrin/snowflake
Submodule snowflake deleted from 68117e
1 change: 0 additions & 1 deletion vendor/github.com/generaltso/sadbox
Submodule sadbox deleted from 27893f
1 change: 0 additions & 1 deletion vendor/github.com/generaltso/vibrant
Submodule vibrant deleted from 563623
Loading

0 comments on commit 49b4716

Please sign in to comment.