Archived
1
0
Fork 0

Add MySQL support (#24)

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-04-08 15:18:55 +02:00
parent d583d559d1
commit 9f51c62776
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7
9 changed files with 63 additions and 8 deletions

3
.gitignore vendored
View file

@ -1,5 +1,6 @@
bin/
docker/environment
docker/postgresql.env
docker/mysql.env
restockbot.db
restockbot.json
restockbot.log

View file

@ -73,13 +73,29 @@ Don't forget to prefix the channel name with an `@`.
### Database
#### SQLite (default)
Default database driver is SQLite using the `restockbot.db` file.
#### PostgreSQL
To configure a PostgreSQL database, you can use Docker:
```
cp -p docker/environment.example docker/environment
docker-compose -f docker-compose.yml up -d
cp -p docker/postgresql.env.example docker/postgresql.env
docker-compose -f docker-compose-postgresql.yml up -d
```
Then see the configuration section to define the database configuration.
#### MySQL
To configure a MySQL database, you can use Docker:
```
cp -p docker/mysql.env.example docker/mysql.env
docker-compose -f docker-compose-mysql.yml up -d
```
Then see the configuration section to define the database configuration.
@ -130,7 +146,7 @@ Default file is `restockbot.json` in the current directory. The file name can be
Options:
* `database` (optional)
* `type`: driver to use (`postgres`, `sqlite`)
* `type`: driver to use (`sqlite`, `postgres`, `mysql`)
* `dsn`: data source name (see [documentation](https://gorm.io/docs/connecting_to_the_database.html))
* `urls` (optional): list of retailers web pages
* `amazon` (optional)

3
db.go
View file

@ -1,6 +1,7 @@
package main
import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
@ -11,6 +12,8 @@ func NewDatabaseFromConfig(config DatabaseConfig) (*gorm.DB, error) {
switch config.Type {
case "postgres":
return gorm.Open(postgres.New(postgres.Config{DSN: config.DSN}), &gorm.Config{})
case "mysql":
return gorm.Open(mysql.New(mysql.Config{DSN: config.DSN}), &gorm.Config{})
default:
return gorm.Open(sqlite.Open(config.DSN), &gorm.Config{})
}

24
docker-compose-mysql.yml Normal file
View file

@ -0,0 +1,24 @@
---
version: "2"
services:
chromium:
container_name: restockbot-chromium
image: montferret/chromium
ports:
- "9222:9222"
restart: always
mysql:
container_name: restockbot-mysql
env_file:
- ./docker/mysql.env
image: mysql:8
ports:
- "3306:3306"
volumes:
- mysql-storage:/var/lib/mysql
restart: always
volumes:
mysql-storage:
driver: local

View file

@ -11,7 +11,7 @@ services:
postgres:
container_name: restockbot-postgres
env_file:
- ./docker/environment
- ./docker/postgresql.env
image: postgres:13
ports:
- "5432:5432"

5
docker/mysql.env.example Normal file
View file

@ -0,0 +1,5 @@
MYSQL_RANDOM_ROOT_PASSWORD=1
MYSQL_ONETIME_PASSWORD=1
MYSQL_DATABASE=restockbot
MYSQL_USER=restockbot
MYSQL_PASSWORD=

View file

@ -1,4 +1,4 @@
POSTGRES_PASSWORD=
POSTGRES_USER=restockbot
POSTGRES_DB=restockbot
POSTGRES_INITDB_ARGS="--data-checksums"
POSTGRES_INITDB_ARGS="--data-checksums"

3
go.mod
View file

@ -10,7 +10,8 @@ require (
github.com/gorilla/mux v1.8.0
github.com/sirupsen/logrus v1.8.0
github.com/spiegel-im-spiegel/pa-api v0.9.0
gorm.io/driver/mysql v1.0.5
gorm.io/driver/postgres v1.0.8
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.20.12
gorm.io/gorm v1.21.3
)

7
go.sum
View file

@ -40,6 +40,8 @@ github.com/dghubble/oauth1 v0.7.0 h1:AlpZdbRiJM4XGHIlQ8BuJ/wlpGwFEJNnB4Mc+78tA/w
github.com/dghubble/oauth1 v0.7.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk=
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.0.0-rc1 h1:Mr8jIV7wDfLw5Fw6BPupm0aduTFdLjhI3wFuIIZKvO4=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.0.0-rc1/go.mod h1:2s/IzRcxCszyNh760IjJiqoYHTnifk8ZeNYL33z8Pww=
@ -283,11 +285,14 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gorm.io/driver/mysql v1.0.5 h1:WAAmvLK2rG0tCOqrf5XcLi2QUwugd4rcVJ/W3aoon9o=
gorm.io/driver/mysql v1.0.5/go.mod h1:N1OIhHAIhx5SunkMGqWbGFVeh4yTNWKmMo1GOAsohLI=
gorm.io/driver/postgres v1.0.8 h1:PAgM+PaHOSAeroTjHkCHCBIHHoBIf9RgPWGo8dF2DA8=
gorm.io/driver/postgres v1.0.8/go.mod h1:4eOzrI1MUfm6ObJU/UcmbXyiHSs8jSwH95G5P5dxcAg=
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.12 h1:ebZ5KrSHzet+sqOCVdH9mTjW91L298nX3v5lVxAzSUY=
gorm.io/gorm v1.20.12/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.3 h1:qDFi55ZOsjZTwk5eN+uhAmHi8GysJ/qCTichM/yO7ME=
gorm.io/gorm v1.21.3/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=