|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -o pipefail |
| 4 | +set -o errtrace |
| 5 | +set -o nounset |
| 6 | +set -o errexit |
| 7 | + |
| 8 | +LIQUIBASE_OPTS="$LIQUIBASE_OPTS --defaultsFile=/liquibase.properties" |
| 9 | + |
| 10 | +echo -n > /liquibase.properties |
| 11 | + |
| 12 | +## Database driver |
| 13 | +if [[ -n "$LIQUIBASE_DRIVER" ]]; then |
| 14 | + echo "driver: ${LIQUIBASE_DRIVER}" >> /liquibase.properties |
| 15 | +fi |
| 16 | + |
| 17 | +## Classpath |
| 18 | +if [[ -n "$LIQUIBASE_CLASSPATH" ]]; then |
| 19 | + echo "classpath: ${LIQUIBASE_CLASSPATH}" >> /liquibase.properties |
| 20 | +fi |
| 21 | + |
| 22 | +## Database url |
| 23 | +if [[ -n "$LIQUIBASE_URL" ]]; then |
| 24 | + echo "url: ${LIQUIBASE_URL}" >> /liquibase.properties |
| 25 | +fi |
| 26 | + |
| 27 | +## Database username |
| 28 | +if [[ -n "$LIQUIBASE_USERNAME" ]]; then |
| 29 | + echo "username: ${LIQUIBASE_USERNAME}" >> /liquibase.properties |
| 30 | +fi |
| 31 | + |
| 32 | +## Database password |
| 33 | +if [[ -n "$LIQUIBASE_PASSWORD" ]]; then |
| 34 | + echo "password: ${LIQUIBASE_PASSWORD}" >> /liquibase.properties |
| 35 | +fi |
| 36 | + |
| 37 | +## Database contexts |
| 38 | +if [[ -n "$LIQUIBASE_CONTEXTS" ]]; then |
| 39 | + echo "contexts: ${LIQUIBASE_CONTEXTS}" >> /liquibase.properties |
| 40 | +fi |
| 41 | + |
| 42 | +## Database changelog file |
| 43 | +if [[ -n "$LIQUIBASE_CHANGELOG" ]]; then |
| 44 | + echo "changeLogFile: ${LIQUIBASE_CHANGELOG}" >> /liquibase.properties |
| 45 | +fi |
| 46 | + |
| 47 | +function executeLiquibase() { |
| 48 | + exec /opt/liquibase/liquibase $LIQUIBASE_OPTS "$@" |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +if [[ "$#" -ge 1 ]]; then |
| 53 | + TASK="$1" |
| 54 | + shift |
| 55 | + |
| 56 | + case "$TASK" in |
| 57 | + ## Custom liquibase command |
| 58 | + liquibase) |
| 59 | + executeLiquibase "$@" |
| 60 | + ;; |
| 61 | + |
| 62 | + ## Database Update Commands |
| 63 | + update|updateCount|updateSQL|updateCountSQL) ;& |
| 64 | + ## Database Rollback Commands |
| 65 | + rollback|rollbackToDate|rollbackCount|rollbackSQL|rollbackToDateSQL|rollbackCountSQL|updateTestingRollback) ;& |
| 66 | + ## Diff Commands |
| 67 | + diff|diffChangeLog) ;& |
| 68 | + ## Documentation Commands |
| 69 | + dbDoc) ;& |
| 70 | + ## Maintenance Commands |
| 71 | + status|validate|changelogSync|changelogSyncSQL|markNextChangeSetRan|listLocks|releaseLocks|dropAll|clearCheckSums) |
| 72 | + if [[ "$#" -eq 0 ]]; then |
| 73 | + executeLiquibase "$TASK" |
| 74 | + else |
| 75 | + executeLiquibase "$TASK" "$@" |
| 76 | + fi |
| 77 | + ;; |
| 78 | + |
| 79 | + ## show configuration |
| 80 | + showConf) |
| 81 | + cat /liquibase.properties |
| 82 | + ;; |
| 83 | + |
| 84 | + ## Help |
| 85 | + help) |
| 86 | + cat <<EOF |
| 87 | +Database Update Commands |
| 88 | +------------------------------------------------------------------------------- |
| 89 | + update Updates database to current version. |
| 90 | + updateCount <value> Applies the next <value> change sets. |
| 91 | + updateSQL Writes SQL to update database to current |
| 92 | + version to STDOUT. |
| 93 | + updateCountSQL <value> Writes SQL to apply the next <value> |
| 94 | + change sets to STDOUT. |
| 95 | +
|
| 96 | +Database Rollback Commands |
| 97 | +------------------------------------------------------------------------------- |
| 98 | + rollback <tag> Rolls back the database to the state it |
| 99 | + was in when the tag was applied. |
| 100 | + rollbackToDate <date/time> Rolls back the database to the state it |
| 101 | + was in at the given date/time. |
| 102 | + rollbackCount <value> Rolls back the last <value> change sets. |
| 103 | + rollbackSQL <tag> Writes SQL to roll back the database to |
| 104 | + the state it was in when the tag was |
| 105 | + applied to STDOUT. |
| 106 | + rollbackToDateSQL <date/time> Writes SQL to roll back the database to |
| 107 | + the state it was in at the given date/time |
| 108 | + version to STDOUT. |
| 109 | + rollbackCountSQL <value> Writes SQL to roll back the last <value> |
| 110 | + change sets to STDOUT. |
| 111 | + futureRollbackSQL Writes SQL to roll back the database to |
| 112 | + the current state after the changes in |
| 113 | + the changeslog have been applied. |
| 114 | + updateTestingRollback Updates the database, then rolls back |
| 115 | + changes before updating again. |
| 116 | + generateChangeLog generateChangeLog of the database to |
| 117 | + standard out. v1.8 requires the dataDir |
| 118 | + parameter currently. |
| 119 | +
|
| 120 | +Diff Commands |
| 121 | +------------------------------------------------------------------------------- |
| 122 | + diff [diff parameters] Writes description of differences to |
| 123 | + standard out. |
| 124 | + diffChangeLog [diff parameters] Writes Change Log XML to update the base |
| 125 | + database to the target database to |
| 126 | + standard out. |
| 127 | +
|
| 128 | +Documentation Commands |
| 129 | +------------------------------------------------------------------------------- |
| 130 | + dbDoc <outputDirectory> Generates Javadoc-like documentation based |
| 131 | + on current database and change log. |
| 132 | +
|
| 133 | +Maintenance Commands |
| 134 | +------------------------------------------------------------------------------- |
| 135 | + tag <tag> "Tags" the current database state for |
| 136 | + future rollback. |
| 137 | + tagExists <tag> Checks whether the given tag is already |
| 138 | + existing. |
| 139 | + status Outputs count (list if --verbose) of unrun |
| 140 | + change sets. |
| 141 | + validate Checks the changelog for errors. |
| 142 | + changelogSync Mark all changes as executed in the |
| 143 | + database. |
| 144 | + changelogSyncSQL Writes SQL to mark all changes as executed |
| 145 | + in the database to STDOUT. |
| 146 | + markNextChangeSetRan Mark the next change set as executed in |
| 147 | + the database. |
| 148 | + listLocks Lists who currently has locks on the |
| 149 | + database changelog. |
| 150 | + releaseLocks Releases all locks on the database |
| 151 | + changelog. |
| 152 | + dropAll Drops all database objects owned by the |
| 153 | + user. Note that functions, procedures |
| 154 | + and packages are not dropped |
| 155 | + (limitation in 1.8.1). |
| 156 | + clearCheckSums Removes current checksums from database. |
| 157 | + On next run checksums will be recomputed. |
| 158 | +EOF |
| 159 | + exit 1 |
| 160 | + ;; |
| 161 | + |
| 162 | + ## Default task (eg. sh, bash) |
| 163 | + *) |
| 164 | + exec "$TASK" "$@" |
| 165 | + ;; |
| 166 | + esac |
| 167 | +fi |
0 commit comments