Open opens a MySQL driver.
(ctx context.Context, dbType storepb.Engine, connCfg db.ConnectionConfig)
| 77 | |
| 78 | // Open opens a MySQL driver. |
| 79 | func (d *Driver) Open(ctx context.Context, dbType storepb.Engine, connCfg db.ConnectionConfig) (db.Driver, error) { |
| 80 | defer func() { |
| 81 | for _, f := range d.openCleanUp { |
| 82 | f() |
| 83 | } |
| 84 | }() |
| 85 | |
| 86 | var dsn string |
| 87 | var err error |
| 88 | switch connCfg.DataSource.GetAuthenticationType() { |
| 89 | case storepb.DataSource_GOOGLE_CLOUD_SQL_IAM: |
| 90 | dsn, err = getCloudSQLConnection(ctx, connCfg) |
| 91 | case storepb.DataSource_AWS_RDS_IAM: |
| 92 | dsn, err = d.getRDSConnection(ctx, connCfg) |
| 93 | default: |
| 94 | dsn, err = d.getMySQLConnection(connCfg) |
| 95 | } |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | |
| 100 | db, err := sql.Open("mysql", dsn) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | d.dbType = dbType |
| 105 | d.db = db |
| 106 | // TODO(d): remove the work-around once we have clean-up the migration connection hack. |
| 107 | db.SetConnMaxLifetime(2 * time.Hour) |
| 108 | db.SetMaxOpenConns(50) |
| 109 | db.SetMaxIdleConns(15) |
| 110 | d.databaseName = connCfg.ConnectionContext.DatabaseName |
| 111 | |
| 112 | return d, nil |
| 113 | } |
| 114 | |
| 115 | func (d *Driver) getMySQLConnection(connCfg db.ConnectionConfig) (string, error) { |
| 116 | protocol := "tcp" |
nothing calls this directly
no test coverage detected