prepareDatasourceForDriver ensures that required features are enabled on the datasource connection string based on the driver.
(driver string, datasource string)
| 93 | // prepareDatasourceForDriver ensures that required features are enabled on the |
| 94 | // datasource connection string based on the driver. |
| 95 | func prepareDatasourceForDriver(driver string, datasource string) (string, error) { |
| 96 | switch driver { |
| 97 | case "sqlite3": |
| 98 | url, err := url.Parse(datasource) |
| 99 | if err != nil { |
| 100 | return "", fmt.Errorf("datasource is of invalid format for driver sqlite3") |
| 101 | } |
| 102 | |
| 103 | // get original query and update it with required settings |
| 104 | query := url.Query() |
| 105 | |
| 106 | // ensure foreign keys are always enabled (disabled by default) |
| 107 | // See https://github.com/mattn/go-sqlite3#connection-string |
| 108 | query.Set("_foreign_keys", "on") |
| 109 | |
| 110 | // update url with updated query |
| 111 | url.RawQuery = query.Encode() |
| 112 | |
| 113 | return url.String(), nil |
| 114 | default: |
| 115 | return datasource, nil |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // helper function to ping the database with backoff to ensure |
| 120 | // a connection can be established before we proceed with the |