Last active
October 12, 2021 16:14
-
-
Save mazrean/59ed5532abe2ae019fa1f396b11e4209 to your computer and use it in GitHub Desktop.
anke-to db fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module anke-to-fix | |
go 1.17 | |
require ( | |
gorm.io/driver/mysql v1.1.2 | |
gorm.io/gorm v1.21.16 | |
) | |
require ( | |
github.com/go-sql-driver/mysql v1.6.0 // indirect | |
github.com/jinzhu/inflection v1.0.0 // indirect | |
github.com/jinzhu/now v1.1.2 // indirect | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= | |
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | |
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | |
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | |
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= | |
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | |
gorm.io/driver/mysql v1.1.2 h1:OofcyE2lga734MxwcCW9uB4mWNXMr50uaGRVwQL2B0M= | |
gorm.io/driver/mysql v1.1.2/go.mod h1:4P/X9vSc3WTrhTLZ259cpFd6xKNYiSSdSZngkSBGIMM= | |
gorm.io/gorm v1.21.12/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= | |
gorm.io/gorm v1.21.16 h1:YBIQLtP5PLfZQz59qfrq7xbrK7KWQ+JsXXCH/THlMqs= | |
gorm.io/gorm v1.21.16/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"gorm.io/driver/mysql" | |
"gorm.io/gorm" | |
) | |
type Config struct { | |
User string | |
Password string | |
Host string | |
Port string | |
Database string | |
} | |
func main() { | |
log.Println("start") | |
conohaUser, ok := os.LookupEnv("CONOHA_USERNAME") | |
if !ok { | |
panic("CONOHA_USERNAME is not set") | |
} | |
conohaPass, ok := os.LookupEnv("CONOHA_PASSWORD") | |
if !ok { | |
panic("CONOHA_PASSWORD is not set") | |
} | |
conohaHost, ok := os.LookupEnv("CONOHA_HOSTNAME") | |
if !ok { | |
panic("CONOHA_HOSTNAME is not set") | |
} | |
conohaDBName, ok := os.LookupEnv("CONOHA_DATABASE") | |
if !ok { | |
panic("CONOHA_DATABASE is not set") | |
} | |
config := Config{ | |
User: conohaUser, | |
Password: conohaPass, | |
Host: conohaHost, | |
Database: conohaDBName, | |
} | |
conohaDB, err := EstablishConnection(&config) | |
if err != nil { | |
panic(err) | |
} | |
log.Println("start get options") | |
var options []Options | |
err = conohaDB. | |
Session(&gorm.Session{}). | |
Where("option_num = ?", 3). | |
Find(&options).Error | |
if err != nil { | |
panic(err) | |
} | |
log.Println("finish get options") | |
log.Printf(`options: | |
%+v | |
ok?`, options[:5]) | |
scanner := bufio.NewScanner(os.Stdin) | |
scanner.Scan() | |
kmbkUser, ok := os.LookupEnv("KMBK_USERNAME") | |
if !ok { | |
panic("KMBK_USERNAME is not set") | |
} | |
kmbkPass, ok := os.LookupEnv("KMBK_PASSWORD") | |
if !ok { | |
panic("KMBK_PASSWORD is not set") | |
} | |
kmbkHost, ok := os.LookupEnv("KMBK_HOSTNAME") | |
if !ok { | |
panic("KMBK_HOSTNAME is not set") | |
} | |
kmbkDBName, ok := os.LookupEnv("KMBK_DATABASE") | |
if !ok { | |
panic("KMBK_DATABASE is not set") | |
} | |
config = Config{ | |
User: kmbkUser, | |
Password: kmbkPass, | |
Host: kmbkHost, | |
Database: kmbkDBName, | |
} | |
kmbkDB, err := EstablishConnection(&config) | |
if err != nil { | |
panic(err) | |
} | |
log.Println("start update options") | |
for _, option := range options { | |
log.Printf("option id:%d\n", option.ID) | |
err = kmbkDB. | |
Session(&gorm.Session{}). | |
Model(&Options{}). | |
Where("id = ?", option.ID). | |
Update("body", option.Body).Error | |
if err != nil { | |
panic(err) | |
} | |
} | |
log.Println("finish update options") | |
log.Println("Finish") | |
} | |
type Options struct { | |
ID int `gorm:"type:int(11) AUTO_INCREMENT;not null;primaryKey"` | |
QuestionID int `gorm:"type:int(11);not null"` | |
OptionNum int `gorm:"type:int(11);not null"` | |
Body string `gorm:"type:text;default:NULL;"` | |
} | |
// EstablishConnection DBと接続 | |
func EstablishConnection(config *Config) (*gorm.DB, error) { | |
dsn := fmt.Sprintf( | |
"%s:%s@tcp(%s:3306)/%s", | |
config.User, | |
config.Password, | |
config.Host, | |
config.Database, | |
) + "?parseTime=true&loc=Asia%2FTokyo&charset=utf8mb4" | |
var err error | |
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | |
db = db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci") | |
return db, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment