Clean up Gradle Flyway tasks in :db (#990)

* Clean up Gradle Flyway tasks in :db

Simplified the command line by revising the semantics of some
properties.

Added examples of Flyway task invocations.

This script still uses the GCS file-based credential. We will migrate it
to the Secret Manager soon.
This commit is contained in:
Weimin Yu 2021-03-04 19:58:09 -05:00 committed by GitHub
parent f997f64169
commit db16a2c679
2 changed files with 15 additions and 7 deletions

View file

@ -111,8 +111,8 @@ PROPERTIES = [
# Cloud SQL properties # Cloud SQL properties
Property('dbServer', Property('dbServer',
'A registry environment name (e.g., "alpha") or a host[:port] ' 'The host[:port] of a database that accepts direct IP access. '
'string'), 'This is typically used with a testing database.'),
Property('dbName', Property('dbName',
'Database name to use in connection.', 'Database name to use in connection.',
'postgres'), 'postgres'),

View file

@ -20,8 +20,6 @@ plugins {
ext { ext {
Set restrictedDbEnv = Set restrictedDbEnv =
[ 'sandbox', 'production' ].asUnmodifiable() [ 'sandbox', 'production' ].asUnmodifiable()
Set allDbEnv =
[ 'alpha', 'crash' ].plus(restrictedDbEnv).asUnmodifiable()
def dbServerProperty = 'dbServer' def dbServerProperty = 'dbServer'
def dbNameProperty = 'dbName' def dbNameProperty = 'dbName'
@ -57,12 +55,12 @@ ext {
} }
getJdbcAccessInfo = { getJdbcAccessInfo = {
if (allDbEnv.contains(dbServer)) { if (rootProject.projects.keySet().contains(environment)) {
return getSocketFactoryAccessInfo(dbServer) return getSocketFactoryAccessInfo(environment)
} else if (!dbServer.isEmpty()) { } else if (!dbServer.isEmpty()) {
return getAccessInfoByHostPort(dbServer) return getAccessInfoByHostPort(dbServer)
} else { } else {
// Not connecting to a database. Return a dummy object for Flyway config. // Not running flyway tasks. Return a dummy object for Flyway config.
return [ url: '', user: '', password: '' ] return [ url: '', user: '', password: '' ]
} }
} }
@ -137,6 +135,16 @@ publishing {
} }
} }
// Adds flyway tasks such as: flywayInfo, flywayValidate, flywayMigrate (deploying the schema in
// local repository), and flywayClean (dropping all data in the database). The latter two commands
// are disallowed in environments listed in ext.restrictedDbEnv.
//
// Examples:
// Get info in alpha: nom_build :db:flywayInfo --environment=alpha
// Deploy schema to a local test instance and override the database name:
// nom_build :db:flywayMigrate --dbServer=localhost:5432 --dbName=not-default \
// --dbUser=... --dbPassword=...
flyway { flyway {
def accessInfo = project.ext.getJdbcAccessInfo() def accessInfo = project.ext.getJdbcAccessInfo()