forked from GESoftware-CF/uaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mssql.gradle
69 lines (59 loc) · 2.1 KB
/
mssql.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
/*
* ****************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2017] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
* ****************************************************************************
*/
import groovy.sql.Sql
apply from: file('shared_versions.gradle')
repositories {
mavenCentral()
}
configurations {
driver
}
dependencies {
driver group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: mssqlVersion
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each {File file ->
loader.addURL(file.toURL())
}
class CreateDatabaseTask extends DefaultTask {
@Input
def databaseName
@TaskAction
void execute() {
def props = [user: 'sa', password: 'changemeCHANGEME1234!'] as Properties
def url = 'jdbc:sqlserver://localhost:1433;database=master;'
def driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
def sql = null
for (int i=0; sql==null && i<5; i++) {
try {
sql = Sql.newInstance(url, props, driver)
} catch (Exception ignored) {
println "Failed to connect to SQL Server, trying again in 10 seconds."
sleep(10000)
}
}
sql.call('sp_configure \'contained database authentication\', 1')
sql.call('RECONFIGURE')
sql.call('CREATE DATABASE ' + databaseName + ' CONTAINMENT = PARTIAL;')
sql.call('USE ' + databaseName + ';')
sql.call('CREATE USER root WITH PASSWORD = \'changemeCHANGEME1234!\';')
sql.call('EXEC sp_addrolemember N\'db_owner\', N\'root\';')
sql.commit()
sql.close()
}
}
task createDatabase(type: CreateDatabaseTask) {
databaseName = project.properties['databaseName']
}