Skip to content

Commit

Permalink
✨ Add support for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontaylordev committed Nov 30, 2024
1 parent cfc89a4 commit 1f5c60c
Show file tree
Hide file tree
Showing 29 changed files with 1,623 additions and 91 deletions.
74 changes: 57 additions & 17 deletions .template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@
"type": "parameter",
"datatype": "choice",
"choices": [
{
"choice": "postgresql",
"description": "PostgreSQL"
},
{
"choice": "sqlite",
"description": "SQLite. This option only applies if --aspire is not specified."
"description": "SQLite"
},
{
"choice": "sqlserver",
"description": "SQL Server."
"description": "SQL Server"
}
],
"defaultValue": "sqlserver",
Expand Down Expand Up @@ -97,6 +101,10 @@
"type": "computed",
"value": "(PipelineProvider == \"github\")"
},
"UsePostgreSQL": {
"type": "computed",
"value": "(Database == \"postgresql\")"
},
"UseSqlite": {
"type": "computed",
"value": "(Database == \"sqlite\")"
Expand Down Expand Up @@ -133,7 +141,22 @@
"**/*.filelist",
"**/*.user",
"**/*.lock.json",
"*.nuspec"
"*.nuspec",
"src/Infrastructure/Data/Migrations/**",
"src/Infrastructure/Data/SQLite/**",
"src/Infrastructure/Data/PostgreSQL/**",
"src/Web/appsettings.Development.json",
"src/Web/appsettings.PostgreSQL.json",
"src/Web/appsettings.SQLite.json",
"tests/Application.FunctionalTests/PostgreSQLTestcontainersTestDatabase.cs",
"tests/Application.FunctionalTests/PostgreSQLTestDatabase.cs",
"tests/Application.FunctionalTests/SqliteTestDatabase.cs",
"tests/Application.FunctionalTests/SqlTestcontainersTestDatabase.cs",
"tests/Application.FunctionalTests/SqlTestDatabase.cs",
"tests/Application.FunctionalTests/appsettings.json",
"tests/Application.FunctionalTests/appsettings.PostgreSQL.json",
".azdo/**/*",
".github/**/*"
],
"rename": {
"README-template.md": "README.md"
Expand Down Expand Up @@ -186,37 +209,54 @@
},
{
"condition": "(UseAzurePipelines)",
"exclude": [
".github/**/*"
"include": [
".azdo/**/*"
]
},
{
"condition": "(UseGithubActions)",
"exclude": [
".azdo/**/*"
"include": [
".github/**/*"
]
},
{
"condition": "(UsePostgreSQL)",
"include": [
"src/Infrastructure/Data/PostgreSQL/**",
"src/Web/appsettings.PostgreSQL.json",
"tests/Application.FunctionalTests/PostgreSQLTestcontainersTestDatabase.cs",
"tests/Application.FunctionalTests/PostgreSQLTestDatabase.cs",
"tests/Application.FunctionalTests/appsettings.PostgreSQL.json"
],
"rename": {
"src/Infrastructure/Data/PostgreSQL/" : "src/Infrastructure/Data/Migrations/",
"src/Web/appsettings.PostgreSQL.json": "src/Web/appsettings.Development.json",
"tests/Application.FunctionalTests/appsettings.PostgreSQL.json": "tests/Application.FunctionalTests/appsettings.json"
}
},
{
"condition": "(UseSqlServer)",
"exclude": [
"src/Infrastructure/Data/SQLite/**",
"src/Web/appsettings.SQLite.json",
"tests/Application.FunctionalTests/SqliteTestDatabase.cs"
"include": [
"src/Infrastructure/Data/Migrations/**",
"src/Web/appsettings.Development.json",
"tests/Application.FunctionalTests/SqlTestcontainersTestDatabase.cs",
"tests/Application.FunctionalTests/SqlTestDatabase.cs",
"tests/Application.FunctionalTests/appsettings.json"
]
},
{
"condition": "(UseSqlite)",
"include": [
"src/Infrastructure/Data/SQLite/**",
"src/Web/appsettings.SQLite.json",
"tests/Application.FunctionalTests/SqliteTestDatabase.cs"
],
"exclude": [
"src/Infrastructure/Data/Migrations/**",
"src/Web/appsettings.json",
"tests/Application.FunctionalTests/SqlServerTestDatabase.cs",
"tests/Application.FunctionalTests/TestcontainersTestDatabase.cs",
"tests/Application.FunctionalTests/appsettings.json"
],
"rename": {
"src/Infrastructure/Data/SQLite/" : "src/Infrastructure/Data/Migrations/",
"appsettings.SQLite.json": "appsettings.json",
"TestDatabase.Sqlite.cs": "TestDatabase.cs"
"src/Web/appsettings.SQLite.json": "src/Web/appsettings.Development.json"
}
},
{
Expand Down
17 changes: 13 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<AspireVersion>9.0.0</AspireVersion>
<AspnetVersion>9.0.0</AspnetVersion>
<EfcoreVersion>9.0.0</EfcoreVersion>
<MicrosoftExtensionsVersion>9.0.0</MicrosoftExtensionsVersion>
</PropertyGroup>
<ItemGroup>
<!--#if (UseAspire)-->
<!--#if (UsePostgreSQL)-->
<PackageVersion Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Hosting.PostgreSQL" Version="$(AspireVersion)" />
<!--#endif-->
<!--#if (UseSqlServer)-->
<PackageVersion Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.SqlServer" Version="9.0.0" />
<PackageVersion Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Hosting.SqlServer" Version="$(AspireVersion)" />
<!--#endif-->
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageVersion Include="Aspire.Hosting.AppHost" Version="$(AspireVersion)" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
Expand Down Expand Up @@ -42,11 +47,16 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="$(EfcoreVersion)" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="$(EfcoreVersion)" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EfcoreVersion)" />
<!--#if (UsePostgreSQL)-->
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.1" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0" />
<!--#endif-->
<!--#if (UseSqlite)-->
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="$(EfcoreVersion)" />
<!--#endif-->
<!--#if (UseSqlServer)-->
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EfcoreVersion)" />
<PackageVersion Include="Testcontainers.MsSql" Version="4.0.0" />
<!--#endif-->
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="$(EfcoreVersion)" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsVersion)" />
Expand All @@ -62,7 +72,6 @@
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageVersion Include="Respawn" Version="6.2.1" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="9.0.0" />
<PackageVersion Include="Testcontainers.MsSql" Version="4.0.0" />
<!--#if(!UseApiOnly)-->
<PackageVersion Include="Microsoft.Playwright" Version="1.48.0" />
<PackageVersion Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.57" />
Expand Down
3 changes: 2 additions & 1 deletion infra/abbreviations.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"dataLakeStoreAccounts": "dls",
"dataMigrationServices": "dms-",
"dBforMySQLServers": "mysql-",
"dBforPostgreSQLServers": "psql-",
"devicesIotHubs": "iot-",
"devicesProvisioningServices": "provs-",
"devicesProvisioningServicesCertificates": "pcert-",
Expand Down Expand Up @@ -104,6 +103,8 @@
"notificationHubsNamespacesNotificationHubs": "ntf-",
"operationalInsightsWorkspaces": "log-",
"portalDashboards": "dash-",
"postgreSQLServers": "psql-",
"postgreSQLServersDatabases": "psqldb-",
"powerBIDedicatedCapacities": "pbi-",
"purviewAccounts": "pview-",
"recoveryServicesVaults": "rsv-",
Expand Down
153 changes: 153 additions & 0 deletions infra/core/database/postgresql/flexibleserver.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
metadata description = 'Creates an Azure Database for PostgreSQL - Flexible Server.'
param name string
param location string = resourceGroup().location
param tags object = {}

param sku object
param storage object
param appUserLogin string
@secure()
param appUserLoginPassword string
param administratorLogin string
@secure()
param administratorLoginPassword string
param databaseName string
param allowAzureIPsFirewall bool = false
param allowAllIPsFirewall bool = false
param allowedSingleIPs array = []
param keyVaultName string
param connectionStringKey string

// PostgreSQL version
param version string

param utcNowString string = utcNow('yyyyMMddHHmm')

// Latest official version 2022-12-01 does not have Bicep types available
resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2022-12-01' = {
location: location
tags: tags
name: name
sku: sku
properties: {
version: version
administratorLogin: administratorLogin
administratorLoginPassword: administratorLoginPassword
storage: storage
highAvailability: {
mode: 'Disabled'
}
}

resource database 'databases' = {
name: databaseName
}

resource firewall_all 'firewallRules' = if (allowAllIPsFirewall) {
name: 'allow-all-IPs'
properties: {
startIpAddress: '0.0.0.0'
endIpAddress: '255.255.255.255'
}
}

resource firewall_azure 'firewallRules' = if (allowAzureIPsFirewall) {
name: 'allow-all-azure-internal-IPs'
properties: {
startIpAddress: '0.0.0.0'
endIpAddress: '0.0.0.0'
}
}

resource firewall_single 'firewallRules' = [for ip in allowedSingleIPs: {
name: 'allow-single-${replace(ip, '.', '')}'
properties: {
startIpAddress: ip
endIpAddress: ip
}
}]
}

resource psqlDeploymentScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: '${name}-deployment-script'
location: location
kind: 'AzureCLI'
properties: {
azCliVersion: '2.37.0'
retentionInterval: 'PT1H' // Retain the script resource for 1 hour after it ends running
timeout: 'PT5M' // Five minutes
cleanupPreference: 'OnSuccess'
forceUpdateTag: utcNowString
environmentVariables: [
{
name: 'APPUSERLOGIN'
value: appUserLogin
}
{
name: 'APPUSERPASSWORD'
secureValue: appUserLoginPassword
}
{
name: 'DBNAME'
value: databaseName
}
{
name: 'DBSERVER'
value: name
}
{
name: 'ADMINLOGIN'
value: administratorLogin
}
{
name: 'ADMINLOGINPASSWORD'
secureValue: administratorLoginPassword
}
]

scriptContent: '''
apk add postgresql-client
cat << EOF > create_user.sql
CREATE ROLE "$APPUSERLOGIN" WITH LOGIN PASSWORD '$APPUSERPASSWORD';
GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO "$APPUSERLOGIN";
EOF
psql "host=$DBSERVER.postgres.database.azure.com user=$ADMINLOGIN dbname=$DBNAME port=5432 password=$ADMINLOGINPASSWORD sslmode=require" < create_user.sql
'''
}
dependsOn: [
postgresServer
]
}

resource administratorLoginPasswordSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
parent: keyVault
name: 'administratorLoginPassword'
properties: {
value: administratorLoginPassword
}
}

resource appUserLoginPasswordSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
parent: keyVault
name: 'appUserLoginPassword'
properties: {
value: appUserLoginPassword
}
}

resource sqlAzureConnectionStringSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
parent: keyVault
name: connectionStringKey
properties: {
value: '${connectionString}; Password=${appUserLoginPassword}'
}
}

resource keyVault 'Microsoft.KeyVault/vaults@2022-07-01' existing = {
name: keyVaultName
}

var connectionString = 'Host=${postgresServer.properties.fullyQualifiedDomainName};Port=5432;Database=${databaseName};Username=${appUserLogin}'
output connectionStringKey string = connectionStringKey
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ resource appUserPasswordSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' =
}
}

resource sqlAzureConnectionStringSercret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
resource sqlAzureConnectionStringSecret 'Microsoft.KeyVault/vaults/secrets@2022-07-01' = {
parent: keyVault
name: connectionStringKey
properties: {
Expand Down
Loading

0 comments on commit 1f5c60c

Please sign in to comment.