forked from tlberglund/groovy-liquibase
-
Notifications
You must be signed in to change notification settings - Fork 34
/
liquibase.gradle
198 lines (155 loc) · 4.33 KB
/
liquibase.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
apply plugin: 'java'
apply plugin: 'groovy'
repositories {
mavenCentral()
}
dependencies {
runtime 'org.codehaus.groovy:groovy:3.0.15'
runtime 'org.codehaus.groovy:groovy-sql:3.0.15'
runtime 'org.liquibase:liquibase-core:4.16.1'
runtime 'mysql:mysql-connector-java:5.1.34'
}
database = [:]
def propertiesFilename = System.properties['liquibase.properties'] ?: 'database.properties'
def parentDirectory = (propertiesFilename[0] == '/') ? '/' : projectDir
def propertiesFile = new File(parentDirectory, propertiesFilename)
def properties = new Properties()
if(propertiesFile.exists()) {
properties.load(new FileInputStream(propertiesFile))
}
database.url = System.properties['database.url'] ?: properties.url
database.username = System.properties['database.username'] ?: properties.username
database.password = System.properties['database.password'] ?: properties.password
database.changeLogFile = System.properties['change.log.file'] ?: properties['change.log.file']
database.workingDir = System.properties['working.dir'] ?: properties['working.dir']
database.classpath = System.properties['database.classpath'] ?: properties['classpath']
database.driver = System.properties['database.driver'] ?: properties['driver']
task generateChangeLog << {
runLiquibase('generateChangeLog')
}
task changeLogSync << {
runLiquibase('changeLogSync')
}
task update << {
liquibaseUpdate(false)
}
task updateSQL << {
liquibaseUpdate(true)
}
task rollback << {
liquibaseRollback(false)
}
task rollbackSQL << {
liquibaseRollback(true)
}
task futureRollbackSQL << {
runLiquibase('futureRollbackSQL')
}
task updateTestingRollback << {
runLiquibase('updateTestingRollback')
}
task status << {
runLiquibase('status')
}
task validate << {
runLiquibase('validate')
}
task changelogSync << {
runLiquibase('changelogSync')
}
task changelogSyncSQL << {
runLiquibase('changelogSyncSQL')
}
task listLocks << {
runLiquibase('listLocks')
}
task releaseLocks << {
runLiquibase('releaseLocks')
}
task markNextChangesetRan << {
runLiquibase('markNextChangesetRan')
}
task markNextChangesetRanSQL << {
runLiquibase('markNextChangesetRanSQL')
}
task dropAll << {
runLiquibase('dropAll')
}
task clearChecksums << {
runLiquibase('clearChecksums')
}
task diff << {
runLiquibase(["--referenceUrl=${System.properties['liquibase.referenceUrl']}", "--referenceUsername=${System.properties['liquibase.referenceUsername']}", "--referencePassword=${System.properties['liquibase.referencePassword']}", 'diff'])
}
task tag << {
runLiquibase(['tag', "${System.properties('liquibase.tag')}"])
}
task dbDoc << {
runLiquibase([ 'dbDoc', System.properties['liquibase.doc.dir'] ?: "${buildDir.absolutePath}/database/docs"])
}
def liquibaseRollback(dryRun) {
def argList = []
def tag = System.properties['liquibase.tag']
def count = System.properties['liquibase.count']
def date = System.properties['liquibase.date']
def suffix = ''
def param = ''
if(count) {
suffix = 'rollbackCount'
param = count
}
else if(date) {
suffix = 'rollbackToDate'
param = date
}
else {
suffix = 'rollback'
param = tag
}
if(dryRun) {
suffix = suffix + 'SQL'
}
argList << suffix
argList << param
runLiquibase(argList)
}
def liquibaseUpdate(dryRun) {
def suffix = ''
def count = System.properties['liquibase.count']
if(count) {
suffix = 'Count'
}
if(dryRun) {
suffix = suffix + 'SQL'
}
def argList = [ "update${suffix}" ]
if(count) {
argList << count
}
runLiquibase(argList)
}
def runLiquibase(String command) {
runLiquibase([command])
}
def runLiquibase(List argList) {
def contexts = System.properties['liquibase.contexts']
def stdArgs = ["--url=${project.database.url}", "--username=${project.database.username}", "--password=${project.database.password}"]
if(project.database.changeLogFile) {
stdArgs << "--changeLogFile=${project.database.changeLogFile}"
}
if(project.database.driver) {
stdArgs << "--driver=${project.database.driver}"
}
if(project.database.classpath) {
stdArgs << "--classpath=${project.database.classpath}"
}
if(contexts) {
stdArgs << "--contexts=${contexts}"
}
javaexec {
workingDir = database.workingDir ?: projectDir
main = 'liquibase.integration.commandline.Main'
classpath = sourceSets.main.runtimeClasspath
args = stdArgs + argList
}
}