Commit d4dd80b2 authored by zauberstuhl's avatar zauberstuhl

Merge branch...

Merge branch '82-mysql-mysqlerror-error-1064-you-have-an-error-in-your-sql-syntax-check-the-manual-that-corresponds-to-your-mysql' into 'master'

Resolve "*mysql.MySQLError: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL..."

Closes #82

See merge request !77
parents 51ccc4db 77efe3f3
Pipeline #566 passed with stages
in 16 minutes and 7 seconds
......@@ -3,12 +3,14 @@
image: zauberstuhl/ganggo-ci:1.9-stretch
services:
- postgres:latest
- mysql:latest
variables:
POSTGRES_DB: ganggo
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ""
UPDATE_CHANNEL: alpha
stages:
- install
- test
- build
- deploy
......@@ -20,17 +22,32 @@ before_script:
- ln -s $(pwd) $PROJECT_PATH/ganggo
- ln -sf $(pwd)/bin $GOPATH/bin
- cd $PROJECT_PATH/ganggo
linux:test:
install:
artifacts:
expire_in: "1 hour"
paths:
- vendor/
- node_modules/
- bin/
stage: test
stage: install
script:
- make install
linux:test:psql:
stage: test
script:
- bash ci/scripts/change_database.sh postgres
- make test
dependencies:
- install
except:
- master
linux:test:mysql:
stage: test
script:
- bash ci/scripts/change_database.sh mysql
- make test
dependencies:
- install
except:
- master
linux:amd64:
......@@ -44,7 +61,7 @@ linux:amd64:
- export GOOS=linux
- make release
dependencies:
- linux:test
- install
except:
- master
windows:amd64:
......@@ -61,7 +78,7 @@ windows:amd64:
- export GOOS=windows
- make release
dependencies:
- linux:test
- install
only:
- /^v[\d\.]+-\w.*$/
windows:386:
......@@ -78,7 +95,7 @@ windows:386:
- export GOOS=windows
- make release
dependencies:
- linux:test
- install
only:
- /^v[\d\.]+-\w.*$/
linux:arm:7:
......@@ -95,7 +112,7 @@ linux:arm:7:
- export GOARM=7
- make release
dependencies:
- linux:test
- install
only:
- /^v[\d\.]+-\w.*$/
deploy artifacts to bintray:
......
......@@ -240,11 +240,28 @@ func advancedColumnModify(s *gorm.DB, column, dataType string) {
}
// modify column in scope
scope.Raw(fmt.Sprintf(
format, scope.QuotedTableName(),
scope.Quote(column), dataType,
format, scope.QuotedTableName(), scope.Quote(column), dataType,
)).Exec()
}
// advancedColumnRename can rename a database column name for mysql,
// postgresql and most common databases. It requires old, new name and
// the data type (mysql only)
func advancedColumnRename(s *gorm.DB, oldName, newName, dataType string) {
var scope = s.NewScope(s.Value)
switch DB.Driver {
case "mysql":
scope.Raw(fmt.Sprintf("ALTER TABLE %v CHANGE %v %v %v",
scope.QuotedTableName(), scope.Quote(oldName),
scope.Quote(newName), dataType,
)).Exec()
default:
scope.Raw(fmt.Sprintf("ALTER TABLE %v RENAME COLUMN %v TO %v",
scope.QuotedTableName(), scope.Quote(oldName), scope.Quote(newName),
)).Exec()
}
}
// returns different methods of searching
// with regular patterns in a database
func advancedColumnSearch(column, expr string) *gorm.ExprResult {
......
......@@ -47,6 +47,13 @@ func migrateSchema(db *gorm.DB) error {
//// Migrations Start ////
// related to https://git.feneas.org/ganggo/ganggo/merge_requests/77
commit = "https://git.feneas.org/ganggo/ganggo/merge_requests/77"
if _, ok := migrations[commit]; !ok {
advancedColumnRename(db.Model(UserSetting{}), "key", "setting_key", "int(11)")
structMigrations = append(structMigrations, SchemaMigration{Commit: commit})
}
// related to https://git.feneas.org/ganggo/ganggo/merge_requests/76
commit = "https://git.feneas.org/ganggo/ganggo/merge_requests/76"
if _, ok := migrations[commit]; !ok {
......@@ -290,11 +297,19 @@ func InitDB() {
}
defer db.Close()
loadSchema(db) // initial schema
if err = migrateSchema(db); err != nil {
revel.AppLog.Error("Something went wrong while migrating", "error", err.Error())
migrationErr := db.Find(&SchemaMigrations{}).Error
// if no migrations are found its very
// likely that no schema is loaded at all
if migrationErr != nil {
loadSchema(db)
} else {
// migrate and print errors to log without aborting
if err = migrateSchema(db); err != nil {
revel.AppLog.Error("Something went wrong while migrating", "error", err.Error())
}
// incase we had no previous issues we never ran loadSchema
// but this is still important cause its possible we added
// a new table or index to it
loadSchema(db)
}
// re-run loadSchema in case something
// broke cause of missing migrations
loadSchema(db)
}
......@@ -33,7 +33,8 @@ type UserSetting struct {
ID uint `gorm:"primary_key"`
UserID uint
Key UserSettingKey
// NOTE Key is a special word in MySQL
Key UserSettingKey `gorm:"column:setting_key"`
Value string
}
......@@ -63,7 +64,8 @@ func (setting *UserSetting) FindByKeyValue(key UserSettingKey, value string) err
}
defer db.Close()
return db.Where("key = ? and value = ?", key, value).First(setting).Error
return db.Where("setting_key = ? and value = ?",
key, value).First(setting).Error
}
func (setting *UserSetting) Update() error {
......@@ -75,8 +77,8 @@ func (setting *UserSetting) Update() error {
defer db.Close()
var dbSetting UserSetting
err = db.Where("user_id = ? and key = ?", setting.UserID, setting.Key).
First(&dbSetting).Error
err = db.Where("user_id = ? and setting_key = ?",
setting.UserID, setting.Key).First(&dbSetting).Error
if err == nil {
setting.ID = dbSetting.ID
return db.Save(setting).Error
......@@ -99,6 +101,6 @@ func (setting *UserSetting) Delete() error {
}
defer db.Close()
return db.Where("user_id = ? and key = ?", setting.UserID, setting.Key).
Delete(UserSetting{}).Error
return db.Where("user_id = ? and setting_key = ?",
setting.UserID, setting.Key).Delete(UserSetting{}).Error
}
#!/bin/bash
if [[ "$1" == "mysql" ]]; then
sed -i 's/db.driver.*/db.driver = "mysql"/' conf/app.conf
sed -i 's/db.user.*/db.user = "root"/' conf/app.conf
sed -i 's/db.password.*/db.password = ""/' conf/app.conf
sed -i 's/db.host.*/db.host = "mysql"/' conf/app.conf
sed -i 's/db.database.*/db.database = "ganggo"/' conf/app.conf
sed -i 's#db.dsn.*#db.dsn = "%s:%s@tcp(%s)/%s?parseTime=true"#g' conf/app.conf
exit 0;
fi
if [[ "$1" == "postgres" ]]; then
sed -i 's/db.driver.*/db.driver = "postgres"/' conf/app.conf
sed -i 's/db.user.*/db.user = "postgres"/' conf/app.conf
sed -i 's/db.password.*/db.password = ""/' conf/app.conf
sed -i 's/db.host.*/db.host = "postgres"/' conf/app.conf
sed -i 's/db.database.*/db.database = "ganggo"/' conf/app.conf
sed -i 's#db.dsn.*#db.dsn = "postgres://%s:%s@%s/%s?sslmode=disable"#g' conf/app.conf
exit 0;
fi
echo "./$0 [mysql|postgres]";
exit 1;
......@@ -108,7 +108,7 @@ func (t *JobsTest) TestTelegramReceiver() {
var setting models.UserSetting
id := t.UserID(t.AccessToken())
err = db.Where("user_id = ? and key = ?", id,
err = db.Where("user_id = ? and setting_key = ?", id,
models.UserSettingTelegramVerified).First(&setting).Error
t.Assertf(err == nil, "Expected nil, got '%+v'", err)
......@@ -122,7 +122,7 @@ func (t *JobsTest) TestTelegramReceiver() {
Body: []byte(fmt.Sprintf(tmpl, test.Text))}).Run()
var setting models.UserSetting
db.Where("key = ? and value = ?",
db.Where("setting_key = ? and value = ?",
models.UserSettingTelegramID, "4321").Find(&setting)
if test.Expected == "" {
......
......@@ -68,7 +68,7 @@ func (t *SettingTest) TestSettings() {
var ii int
for key, value := range test.Settings {
var setting models.UserSetting
err = db.Where("key = ? and user_id = ?", key, id).
err = db.Where("setting_key = ? and user_id = ?", key, id).
First(&setting).Error
var statement bool
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment