Skip to content

Instantly share code, notes, and snippets.

@kjessup
Created March 17, 2018 16:18
Show Gist options
  • Save kjessup/ee8a46cd03978a184fec6ed0a5650dd1 to your computer and use it in GitHub Desktop.
Save kjessup/ee8a46cd03978a184fec6ed0a5650dd1 to your computer and use it in GitHub Desktop.
extension CloudFormation.RDSInstance {
func authDb() throws -> Database<PostgresDatabaseConfiguration> {
return Database(configuration: try databaseConfiguration(database: biqAuthDatabaseName))
}
func databaseConfiguration() throws -> PostgresDatabaseConfiguration {
return try databaseConfiguration(database: biqAuthDatabaseName)
}
func databaseConfiguration(database: String) throws -> PostgresDatabaseConfiguration {
return try .init(database: database, host: hostName, port: hostPort, username: userName, password: password)
}
func initBiqDatabase() throws {
do {
let postgresConfig = try databaseConfiguration(database: "postgres")
let db = Database(configuration: postgresConfig)
struct PGDatabase: Codable, TableNameProvider {
static var tableName = "pg_database"
let datname: String
}
let count = try db.table(PGDatabase.self).where(\PGDatabase.datname == biqAuthDatabaseName).count()
if count == 0 {
try db.sql("CREATE DATABASE \(biqAuthDatabaseName)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment