-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.gradle
107 lines (97 loc) · 4.15 KB
/
database.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
ext.dropDatabaseSql = " DROP DATABASE IF EXISTS ${ant.properties."db.name"}; "
ext.createDatabaseSql = "CREATE DATABASE ${ant.properties."db.name"};"
ext.grantPermissionsSql = """ GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES
ON ${ant.properties."db.name"}.*
TO '${ant.properties."db.user"}'@'@@HOST@@'
IDENTIFIED BY '${ant.properties."db.pass"}';
FLUSH PRIVILEGES;"""
ext.getPass = {
String pass = ''
if (ant.properties."db.adminpass"){
pass ="-p${ant.properties."db.adminpass"}"
}
pass
}
ext.sql = { Map args ->
String sqlQuery = args.sqlQuery
String host = args.host?:ant.properties."db.host"
String keyFile = args.keyFile?:ant.properties."build.userkey"
String buildUser = args.buildUser?:ant.properties."build.user"
String passphrase = args.passphrase?:ant.properties."build.userPassphrase"
Boolean failOnError = args.failOnError?: true
def cmd = "${ant.properties["mysql.executable"]} -u${ant.properties."db.adminuser"} ${getPass()} -h${ant.properties."db.host"} -e\"${sqlQuery}\""
new File(ant.properties.remoteOutputFile).append("\nRunning:: ${sqlQuery}\nOn host ${host} As ${buildUser}\nOutput:\n")
ant.sshexec(
username: buildUser,
host:host,
command: cmd,
failonerror:failOnError,
keyfile:keyFile,
passphrase:passphrase,
append: true,
trust:true,
outputproperty: "drushOutputBuffer",
output:ant.properties.remoteOutputFile,
verbose: 'true'
)
}
ext.restoreDatabaseFromGzip = {Map args ->
String file = args?.file?:"${ant.properties.site}_${ant.properties.env}_${ant.properties.BUILD_NUMBER}.sql.gz"
String host = args?.host?:ant.properties."db.host"
String keyFile = args?.keyFile?:ant.properties."build.userkey"
String buildUser = args?.buildUser?:ant.properties."build.user"
String passphrase = args?.passphrase?:ant.properties."build.userPassphrase"
Boolean failOnError = args?.failOnError?: true
def cmd = "gunzip < ${file} | ${ant.properties["mysql.executable"]} -u${ant.properties."db.adminuser"} ${getPass()} -h${ant.properties."db.host"} ${ant.properties."db.name"}"
new File(ant.properties.remoteOutputFile).append("\nRunning:: ${cmd}\nOn host ${host} As ${buildUser}\nOutput:\n")
ant.sshexec(
username: buildUser,
host:host,
command: cmd,
failonerror:failOnError,
keyfile:keyFile,
passphrase:passphrase,
append: true,
trust:true,
outputproperty: "drushOutputBuffer",
output:ant.properties.remoteOutputFile,
verbose: 'true'
)
}
ext.backupDatabaseToGzip = { Map args ->
String file = args?.file?:"${ant.properties.site}_${ant.properties.env}_${ant.properties.BUILD_NUMBER}.sql.gz"
String host = args?.host?:ant.properties."db.host"
String keyFile = args?.keyFile?:ant.properties."build.userkey"
String buildUser = args?.buildUser?:ant.properties."build.user"
String passphrase = args?.passphrase?:ant.properties."build.userPassphrase"
Boolean failOnError = args?.failOnError?: true
def cmd = "${ant.properties["mysqldump.executable"]} -u${ant.properties."db.adminuser"} ${getPass()} -h${ant.properties."db.host"} ${ant.properties."db.name"} | gzip > ${file}"
new File(ant.properties.remoteOutputFile).append("\nRunning:: ${cmd}\nOn host ${host} As ${buildUser}\nOutput:\n")
ant.sshexec(
username: buildUser,
host:host,
command: cmd,
failonerror:failOnError,
keyfile:keyFile,
passphrase:passphrase,
append: true,
trust:true,
outputproperty: "drushOutputBuffer",
output:ant.properties.remoteOutputFile,
verbose: 'true'
)
}
task dropDatabase() << {
def host = ant.properties."db.host"
sql( sqlQuery: dropDatabaseSql)
}
task backupDatabase() << {
backupDatabaseToGzip()
}
task recreateDatabase() << {
sql( sqlQuery: dropDatabaseSql)
sql( sqlQuery: createDatabaseSql)
}
task restoreDatabase() << {
restoreDatabaseFromGzip()
}