Skip to content

Commit

Permalink
chore: don't start mysql for every test (#3439)
Browse files Browse the repository at this point in the history
The provisioner should start it if it is needed.
  • Loading branch information
stuartwdouglas authored Nov 20, 2024
1 parent 91cc5b0 commit 2a5d45a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
10 changes: 8 additions & 2 deletions backend/controller/sql/testdata/go/mysql/types.ftl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 49 additions & 26 deletions backend/provisioner/dev_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provisioner
import (
"context"
"fmt"
"time"

"github.com/XSAM/otelsql"
_ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -38,39 +39,61 @@ func provisionMysql(mysqlPort int) InMemResourceProvisionerFn {
if err != nil {
return nil, fmt.Errorf("failed to wait for mysql to be ready: %w", err)
}
conn, err := otelsql.Open("mysql", mysqlDSN)
if err != nil {
return nil, fmt.Errorf("failed to connect to mysql: %w", err)
}
defer conn.Close()

res, err := conn.Query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?", dbName)
if err != nil {
return nil, fmt.Errorf("failed to query database: %w", err)
}
defer res.Close()
if res.Next() {
_, err = conn.ExecContext(ctx, "DROP DATABASE "+dbName)
if err != nil {
return nil, fmt.Errorf("failed to drop database: %w", err)
timeout := time.After(10 * time.Second)
retry := time.NewTicker(100 * time.Millisecond)
defer retry.Stop()
for {
select {
case <-timeout:
return nil, fmt.Errorf("failed to query database: %w", err)
case <-retry.C:
var ret *provisioner.Resource
ret, err = establishMySQLDB(ctx, rc, mysqlDSN, dbName, mysql, mysqlPort)
if err != nil {
logger.Debugf("failed to establish mysql database: %s", err.Error())
continue
}
return ret, nil
}

}

_, err = conn.ExecContext(ctx, "CREATE DATABASE "+dbName)
}
}

func establishMySQLDB(ctx context.Context, rc *provisioner.ResourceContext, mysqlDSN string, dbName string, mysql *provisioner.Resource_Mysql, mysqlPort int) (*provisioner.Resource, error) {
conn, err := otelsql.Open("mysql", mysqlDSN)
if err != nil {
return nil, fmt.Errorf("failed to connect to mysql: %w", err)
}
defer conn.Close()

res, err := conn.Query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?", dbName)
if err != nil {
return nil, fmt.Errorf("failed to query database: %w", err)
}
defer res.Close()
if res.Next() {
_, err = conn.ExecContext(ctx, "DROP DATABASE "+dbName)
if err != nil {
return nil, fmt.Errorf("failed to create database: %w", err)
return nil, fmt.Errorf("failed to drop database: %w", err)
}
}

if mysql.Mysql == nil {
mysql.Mysql = &provisioner.MysqlResource{}
}
dsn := dsn.MySQLDSN(dbName, dsn.Port(mysqlPort))
mysql.Mysql.Output = &provisioner.MysqlResource_MysqlResourceOutput{
WriteDsn: dsn,
ReadDsn: dsn,
}
return rc.Resource, nil
_, err = conn.ExecContext(ctx, "CREATE DATABASE "+dbName)
if err != nil {
return nil, fmt.Errorf("failed to create database: %w", err)
}

if mysql.Mysql == nil {
mysql.Mysql = &provisioner.MysqlResource{}
}
dsn := dsn.MySQLDSN(dbName, dsn.Port(mysqlPort))
mysql.Mysql.Output = &provisioner.MysqlResource_MysqlResourceOutput{
WriteDsn: dsn,
ReadDsn: dsn,
}
return rc.Resource, nil
}

func provisionPostgres(postgresPort int) func(ctx context.Context, rc *provisioner.ResourceContext, module string, id string) (*provisioner.Resource, error) {
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ services:
retries: 60
start_period: 80s
mysql:
profiles:
- mysql
image: mysql:8.4.3
environment:
MYSQL_ROOT_PASSWORD: secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ public byte[] getConfig(String secretName) {
}

public Datasource getDatasource(String name) {
log.errorf("GET DATASOURCE");
List<ModuleContextResponse.DSN> databasesList = getModuleContext().getDatabasesList();
for (var i : databasesList) {
log.errorf("DSN %s %s", i.getName(), i.getDsn());
if (i.getName().equals(name)) {
return Datasource.fromDSN(i.getDsn(), i.getType());
}
Expand Down

0 comments on commit 2a5d45a

Please sign in to comment.