Skip to content

Commit

Permalink
Merge pull request #100 from adamdecaf/support-mysql
Browse files Browse the repository at this point in the history
cmd/server: expand databases supported to include mysql
  • Loading branch information
adamdecaf authored Jul 18, 2019
2 parents 4b893cc + 0ea19dd commit 772dd0b
Show file tree
Hide file tree
Showing 22 changed files with 1,939 additions and 1,012 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,32 @@ Docs: [docs.moov.io](https://docs.moov.io/ofac/) | [api docs](https://api.moov.i
| `OFAC_DATA_REFRESH` | Interval for OFAC data redownload and reparse. | 12h |
| `OFAC_DOWNLOAD_TEMPLATE` | HTTP address for downloading raw OFAC files. | (OFAC website) |
| `DPL_DOWNLOAD_TEMPLATE` | HTTP address for downloading the DPL | (BIS website) |
| `SQLITE_DB_PATH`| Local filepath location for the paygate SQLite database. | `ofac.db` |
| `WEBHOOK_BATCH_SIZE` | How many watches to read from database per batch of async searches. | 100 |
| `LOG_FORMAT` | Format for logging lines to be written as. | Options: `json`, `plain` - Default: `plain` |
| `HTTP_BIND_ADDRESS` | Address for paygate to bind its HTTP server on. This overrides the command-line flag `-http.addr`. | Default: `:8080` |
| `HTTP_ADMIN_BIND_ADDRESS` | Address for paygate to bind its admin HTTP server on. This overrides the command-line flag `-admin.addr`. | Default: `:9090` |
| `HTTP_BIND_ADDRESS` | Address for OFAC to bind its HTTP server on. This overrides the command-line flag `-http.addr`. | Default: `:8080` |
| `HTTP_ADMIN_BIND_ADDRESS` | Address for OFAC to bind its admin HTTP server on. This overrides the command-line flag `-admin.addr`. | Default: `:9090` |
| `DATABASE_TYPE` | Which database option to use (Options: `sqlite`, `mysql`) | Default: `sqlite` |

#### Storage

Based on `DATABASE_TYPE` the following environment variables will be read to configure connections for a specific database.

##### MySQL

- `MYSQL_ADDRESS`: TCP address for connecting to the mysql server. (example: `tcp(hostname:3306)`)
- `MYSQL_DATABASE`: Name of database to connect into.
- `MYSQL_PASSWORD`: Password of user account for authentication.
- `MYSQL_USER`: Username used for authentication,

Refer to the mysql driver documentation for [connection parameters](https://github.com/go-sql-driver/mysql#dsn-data-source-name).

- `MYSQL_TIMEOUT`: Timeout parameter specified on (DSN) data source name. (Default: `30s`)

##### SQLite

- `SQLITE_DB_PATH`: Local filepath location for the paygate SQLite database. (Default: `ofac.db`)

Refer to the sqlite driver documentation for [connection parameters](https://github.com/mattn/go-sqlite3#connection-string).

### Features

Expand Down
34 changes: 28 additions & 6 deletions cmd/server/companies.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

moovhttp "github.com/moov-io/base/http"
"github.com/moov-io/ofac"
"github.com/moov-io/ofac/internal/database"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -112,7 +113,8 @@ type companyRepository interface {
}

type sqliteCompanyRepository struct {
db *sql.DB
db *sql.DB
logger log.Logger
}

func (r *sqliteCompanyRepository) close() error {
Expand Down Expand Up @@ -144,14 +146,34 @@ func (r *sqliteCompanyRepository) getCompanyStatus(companyID string) (*CompanySt
}

func (r *sqliteCompanyRepository) upsertCompanyStatus(companyID string, status *CompanyStatus) error {
query := `insert or replace into company_status (company_id, user_id, note, status, created_at) values (?, ?, ?, ?, ?);`
stmt, err := r.db.Prepare(query)
tx, err := r.db.Begin()
if err != nil {
return err
return fmt.Errorf("upsertCompanyStatus: begin: %v", err)
}

query := `insert into company_status (company_id, user_id, note, status, created_at) values (?, ?, ?, ?, ?);`
stmt, err := tx.Prepare(query)
if err != nil {
return fmt.Errorf("upsertCompanyStatus: prepare error=%v rollback=%v", err, tx.Rollback())
}
defer stmt.Close()
_, err = stmt.Exec(companyID, status.UserID, status.Note, status.Status, status.CreatedAt)
return err
stmt.Close()
if err == nil {
return tx.Commit()
}
if database.UniqueViolation(err) {
query = `update company_status set note = ?, status = ? where company_id = ? and user_id = ?;`
stmt, err = tx.Prepare(query)
if err != nil {
return fmt.Errorf("upsertCompanyStatus: inner prepare error=%v rollback=%v", err, tx.Rollback())
}
_, err := stmt.Exec(status.Note, status.Status, companyID, status.UserID)
stmt.Close()
if err != nil {
return fmt.Errorf("upsertCompanyStatus: unique error=%v rollback=%v", err, tx.Rollback())
}
}
return tx.Commit()
}

func getCompany(logger log.Logger, searcher *searcher, companyRepo companyRepository) http.HandlerFunc {
Expand Down
Loading

0 comments on commit 772dd0b

Please sign in to comment.