Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pkg/cloud/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ import (
type LocalSqlServer struct {
projectName string
containerId string
port int
sqlpb.UnimplementedSqlServer
}

var _ sqlpb.SqlServer = (*LocalSqlServer)(nil)

func ensureDatabaseExists(databaseName string) (string, error) {
port := 5432
func (l *LocalSqlServer) ensureDatabaseExists(databaseName string) (string, error) {
// Ensure the database exists
// Connect to the PostgreSQL instance
conn, err := pgx.Connect(context.Background(), fmt.Sprintf("user=postgres password=localsecret host=localhost port=%d dbname=postgres sslmode=disable", port))
conn, err := pgx.Connect(context.Background(), fmt.Sprintf("user=postgres password=localsecret host=localhost port=%d dbname=postgres sslmode=disable", l.port))
if err != nil {
log.Fatal(err)
}
Expand All @@ -74,7 +74,7 @@ func ensureDatabaseExists(databaseName string) (string, error) {
// TODO: Run migrations/seeds if necessary

// Return the connection string of the new database
return fmt.Sprintf("postgresql://postgres:localsecret@localhost:%d/%s?sslmode=disable", port, databaseName), nil
return fmt.Sprintf("postgresql://postgres:localsecret@localhost:%d/%s?sslmode=disable", l.port, databaseName), nil
}

func (l *LocalSqlServer) start() error {
Expand Down Expand Up @@ -115,6 +115,8 @@ func (l *LocalSqlServer) start() error {

freeport := newLis.Addr().(*net.TCPAddr).Port

l.port = freeport

_ = newLis.Close()

l.containerId, err = dockerClient.ContainerCreate(&container.Config{
Expand Down Expand Up @@ -164,7 +166,7 @@ func (l *LocalSqlServer) Stop() error {
}

func (l *LocalSqlServer) ConnectionString(ctx context.Context, req *sqlpb.SqlConnectionStringRequest) (*sqlpb.SqlConnectionStringResponse, error) {
connectionString, err := ensureDatabaseExists(req.DatabaseName)
connectionString, err := l.ensureDatabaseExists(req.DatabaseName)
if err != nil {
return nil, err
}
Expand Down