Skip to content

Commit

Permalink
compose: Add support for mysql (#4783)
Browse files Browse the repository at this point in the history
Add support for MySQL databases (using flexible servers).

Contributes to #4671
  • Loading branch information
saragluna authored Feb 19, 2025
1 parent d280c2f commit ffd345d
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 31 deletions.
1 change: 1 addition & 0 deletions cli/azd/.vscode/cspell-azd-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ mongojs
mvnw
mysqlclient
mysqldb
mysqladmin
nobanner
nodeapp
nolint
Expand Down
1 change: 1 addition & 0 deletions cli/azd/internal/cmd/add/add_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Configure(
case project.ResourceTypeOpenAiModel:
return fillAiModelName(ctx, r, console, p)
case project.ResourceTypeDbPostgres,
project.ResourceTypeDbMySql,
project.ResourceTypeDbMongo:
return fillDatabaseName(ctx, r, console, p)
case project.ResourceTypeMessagingEventHubs:
Expand Down
10 changes: 10 additions & 0 deletions cli/azd/internal/cmd/add/add_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ func Metadata(r *project.ResourceConfig) resourceMeta {
"POSTGRES_PORT",
"POSTGRES_URL",
}
case project.ResourceTypeDbMySql:
res.AzureResourceType = "Microsoft.DBforMySQL/flexibleServers/databases"
res.UseEnvVars = []string{
"MYSQL_HOST",
"MYSQL_USERNAME",
"MYSQL_DATABASE",
"MYSQL_PASSWORD",
"MYSQL_PORT",
"MYSQL_URL",
}
case project.ResourceTypeDbMongo:
res.AzureResourceType = "Microsoft.DocumentDB/databaseAccounts/mongodbDatabases"
res.UseEnvVars = []string{
Expand Down
15 changes: 12 additions & 3 deletions cli/azd/internal/scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,21 @@ func executeToFS(targetFS *memfs.FS, tmpl *template.Template, name string, path
}

func preExecExpand(spec *InfraSpec) {
// postgres requires specific password seeding parameters
// postgres and mysql requires specific password seeding parameters
if spec.DbPostgres != nil {
spec.Parameters = append(spec.Parameters,
Parameter{
Name: "databasePassword",
Value: "$(secretOrRandomPassword ${AZURE_KEY_VAULT_NAME} databasePassword)",
Name: "postgresDatabasePassword",
Value: "$(secretOrRandomPassword ${AZURE_KEY_VAULT_NAME} postgres-password)",
Type: "string",
Secret: true,
})
}
if spec.DbMySql != nil {
spec.Parameters = append(spec.Parameters,
Parameter{
Name: "mysqlDatabasePassword",
Value: "$(secretOrRandomPassword ${AZURE_KEY_VAULT_NAME} mysql-password)",
Type: "string",
Secret: true,
})
Expand Down
24 changes: 23 additions & 1 deletion cli/azd/internal/scaffold/scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func TestExecInfra(t *testing.T) {
DbPostgres: &DatabasePostgres{
DatabaseName: "appdb",
},
DbMySql: &DatabaseMysql{
DatabaseName: "mysqldb",
},
DbCosmosMongo: &DatabaseCosmosMongo{
DatabaseName: "appdb",
},
Expand Down Expand Up @@ -113,6 +116,9 @@ func TestExecInfra(t *testing.T) {
DbPostgres: &DatabaseReference{
DatabaseName: "appdb",
},
DbMySql: &DatabaseReference{
DatabaseName: "mysqldb",
},
ServiceBus: &ServiceBus{},
EventHubs: &EventHubs{},
StorageAccount: &StorageReference{},
Expand All @@ -136,7 +142,6 @@ func TestExecInfra(t *testing.T) {
InfraSpec{
DbPostgres: &DatabasePostgres{
DatabaseName: "appdb",
DatabaseUser: "appuser",
},
Services: []ServiceSpec{
{
Expand All @@ -149,6 +154,23 @@ func TestExecInfra(t *testing.T) {
},
},
},
{
"API with MySQL",
InfraSpec{
DbMySql: &DatabaseMysql{
DatabaseName: "appdb",
},
Services: []ServiceSpec{
{
Name: "api",
Port: 3100,
DbMySql: &DatabaseReference{
DatabaseName: "appdb",
},
},
},
},
},
{
"API with MongoDB",
InfraSpec{
Expand Down
7 changes: 6 additions & 1 deletion cli/azd/internal/scaffold/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type InfraSpec struct {

// Databases to create
DbPostgres *DatabasePostgres
DbMySql *DatabaseMysql
DbCosmosMongo *DatabaseCosmosMongo
DbRedis *DatabaseRedis

Expand All @@ -36,7 +37,10 @@ type Parameter struct {
}

type DatabasePostgres struct {
DatabaseUser string
DatabaseName string
}

type DatabaseMysql struct {
DatabaseName string
}

Expand Down Expand Up @@ -88,6 +92,7 @@ type ServiceSpec struct {

// Connection to a database
DbPostgres *DatabaseReference
DbMySql *DatabaseReference
DbCosmosMongo *DatabaseReference
DbRedis *DatabaseReference

Expand Down
4 changes: 4 additions & 0 deletions cli/azd/pkg/project/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func AllResourceTypes() []ResourceType {
return []ResourceType{
ResourceTypeDbRedis,
ResourceTypeDbPostgres,
ResourceTypeDbMySql,
ResourceTypeDbMongo,
ResourceTypeHostContainerApp,
ResourceTypeOpenAiModel,
Expand All @@ -27,6 +28,7 @@ func AllResourceTypes() []ResourceType {
const (
ResourceTypeDbRedis ResourceType = "db.redis"
ResourceTypeDbPostgres ResourceType = "db.postgres"
ResourceTypeDbMySql ResourceType = "db.mysql"
ResourceTypeDbMongo ResourceType = "db.mongo"
ResourceTypeHostContainerApp ResourceType = "host.containerapp"
ResourceTypeOpenAiModel ResourceType = "ai.openai.model"
Expand All @@ -41,6 +43,8 @@ func (r ResourceType) String() string {
return "Redis"
case ResourceTypeDbPostgres:
return "PostgreSQL"
case ResourceTypeDbMySql:
return "MySQL"
case ResourceTypeDbMongo:
return "MongoDB"
case ResourceTypeHostContainerApp:
Expand Down
7 changes: 6 additions & 1 deletion cli/azd/pkg/project/scaffold_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ func infraSpec(projectConfig *ProjectConfig) (*scaffold.InfraSpec, error) {
case ResourceTypeDbPostgres:
infraSpec.DbPostgres = &scaffold.DatabasePostgres{
DatabaseName: res.Name,
DatabaseUser: "pgadmin",
}
case ResourceTypeDbMySql:
infraSpec.DbMySql = &scaffold.DatabaseMysql{
DatabaseName: res.Name,
}
case ResourceTypeHostContainerApp:
svcSpec := scaffold.ServiceSpec{
Expand Down Expand Up @@ -287,6 +290,8 @@ func mapHostUses(
svcSpec.DbCosmosMongo = &scaffold.DatabaseReference{DatabaseName: useRes.Name}
case ResourceTypeDbPostgres:
svcSpec.DbPostgres = &scaffold.DatabaseReference{DatabaseName: useRes.Name}
case ResourceTypeDbMySql:
svcSpec.DbMySql = &scaffold.DatabaseReference{DatabaseName: useRes.Name}
case ResourceTypeDbRedis:
svcSpec.DbRedis = &scaffold.DatabaseReference{DatabaseName: useRes.Name}
case ResourceTypeHostContainerApp:
Expand Down
3 changes: 3 additions & 0 deletions cli/azd/resources/scaffold/templates/main.bicept
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ output AZURE_RESOURCE_REDIS_ID string = resources.outputs.AZURE_RESOURCE_REDIS_I
{{- if .DbPostgres}}
output AZURE_RESOURCE_{{alphaSnakeUpper .DbPostgres.DatabaseName}}_ID string = resources.outputs.AZURE_RESOURCE_{{alphaSnakeUpper .DbPostgres.DatabaseName}}_ID
{{- end}}
{{- if .DbMySql}}
output AZURE_RESOURCE_{{alphaSnakeUpper .DbMySql.DatabaseName}}_ID string = resources.outputs.AZURE_RESOURCE_{{alphaSnakeUpper .DbMySql.DatabaseName}}_ID
{{- end}}
{{- if .StorageAccount }}
output AZURE_RESOURCE_STORAGE_ID string = resources.outputs.AZURE_RESOURCE_STORAGE_ID
{{- end}}
Expand Down
Loading

0 comments on commit ffd345d

Please sign in to comment.