Created
December 25, 2011 07:29
-
-
Save skhatri/1518850 to your computer and use it in GitHub Desktop.
Gradle Reset Database with versioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.dbcp.BasicDataSource | |
apply plugin:'eclipse' | |
propFile="${rootProject.projectDir}/config/database.properties" | |
prod=false | |
buildscript { | |
buildDeps = ['commons-pool:commons-pool:1.5.4', 'commons-io:commons-io:2.0.1', 'commons-dbcp:commons-dbcp:1.3', | |
'mysql:mysql-connector-java:5.1.13'] | |
repositories { | |
if(System.getenv("M2_REPO")) { | |
maven {url file(System.getenv('M2_REPO')).absolutePath} | |
} | |
mavenCentral() | |
} | |
dependencies { | |
project.buildDeps.each { | |
classpath it | |
} | |
} | |
} | |
def loadProperties(String fileName) { | |
def props = new Properties() | |
new File(fileName).withInputStream { | |
props.load(it) | |
} | |
def tokenlist = [:] | |
props.each ({ | |
tokenlist[it.key] = it.value | |
}) | |
tokenlist | |
} | |
def runSql(runDir, fileName) { | |
props = loadProperties(rootProject.propFile) | |
ant.sql( | |
'driver':props['database.driverClassName'], | |
'url': props['database.url'], | |
'password': props['database.password'], | |
'userid':props['database.username'], | |
'classpath':buildscript.configurations.classpath.asPath | |
) { | |
fileset(dir:runDir) { | |
include (name: fileName) | |
} | |
} | |
} | |
task dropDatabase << { | |
runSql('database/init', '**/*.sql') | |
} | |
task loadDatabase << { | |
runSql('database/refresh', '**/*.sql') | |
} | |
task updateDatabase << { | |
props = loadProperties(propFile) | |
def ds = new BasicDataSource() | |
ds.with { | |
url = props['database.url'] | |
driverClassName = props['database.driverClassName'] | |
password = props['database.password'] | |
username = props['database.username'] | |
} | |
releaseDir = 'database/release' | |
def sql = new groovy.sql.Sql(ds) | |
def maxVersion = 0 | |
def row = sql.firstRow 'select max(version) as version from version_info where endtime is not null' | |
maxVersion = row.version | |
println "Current DB version: ${maxVersion?:0}" | |
def fileList = [] | |
def allFiles = file(releaseDir).listFiles().findAll({!it.directory}) | |
def filesToRun = [] | |
allFiles.each { | |
if (prod) { | |
if (!it.name.contains("mock")) { | |
filesToRun << it | |
} | |
} else { | |
filesToRun << it | |
} | |
} | |
filesToRun.each{theFile-> | |
def fileName = theFile.name | |
def matcher = fileName =~ /^([0-9]+).*/ | |
if (!matcher.matches()) { | |
throw new RuntimeException("file ${fileName} should start with number.") | |
} | |
def fNumber = matcher[0][1] | |
def versionNumber = (fNumber =~ /^0*/).replaceAll('') | |
if (!versionNumber?.empty) { | |
if (new Long(versionNumber) > maxVersion) { | |
def versionExpando = new groovy.util.Expando(versionNumber: versionNumber, fileName: fileName) | |
fileList.add(versionExpando) | |
} | |
} | |
} | |
fileList.sort(new Comparator() { | |
public int compare(Object thisObject, Object thatObject) { | |
return thisObject.versionNumber.compareTo(thatObject.versionNumber) | |
} | |
}) | |
println "Number of Scripts to run: ${fileList.size()}" | |
fileList.each { | |
execFile = it.fileName | |
sql.execute ('insert into version_info(version, filename, starttime) values(?, ?, now())', [it.versionNumber, it.fileName]) | |
println "executing ${execFile}" | |
runSql (releaseDir, execFile) | |
sql.execute('update version_info set endtime = now() where version = ?', [it.versionNumber]) | |
} | |
} | |
task resetDatabase (dependsOn: ['dropDatabase', 'updateDatabase']) | |
task dbFromDump (dependsOn: ['dropDatabase', 'loadDatabase']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment