Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
78639fff
Commit
78639fff
authored
Apr 25, 2016
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5788 from eddumelendez/gh-5402
* pr/5788: Support different schema/data DB script users
parents
229d1fa3
99b830eb
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
138 additions
and
5 deletions
+138
-5
DataSourceInitializer.java
...mework/boot/autoconfigure/jdbc/DataSourceInitializer.java
+17
-5
DataSourceProperties.java
...amework/boot/autoconfigure/jdbc/DataSourceProperties.java
+53
-0
DataSourceInitializerTests.java
...k/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
+45
-0
DataSourcePropertiesTests.java
...rk/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java
+19
-0
appendix-application-properties.adoc
...cs/src/main/asciidoc/appendix-application-properties.adoc
+4
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java
View file @
78639fff
/*
* Copyright 2012-201
5
the original author or authors.
* Copyright 2012-201
6
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
*
* @author Dave Syer
* @author Phillip Webb
* @author Eddú Meléndez
* @since 1.1.0
* @see DataSourceAutoConfiguration
*/
...
...
@@ -78,7 +79,9 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
private
void
runSchemaScripts
()
{
List
<
Resource
>
scripts
=
getScripts
(
this
.
properties
.
getSchema
(),
"schema"
);
if
(!
scripts
.
isEmpty
())
{
runScripts
(
scripts
);
String
username
=
this
.
properties
.
getSchemaUsername
();
String
password
=
this
.
properties
.
getSchemaPassword
();
runScripts
(
scripts
,
username
,
password
);
try
{
this
.
applicationContext
.
publishEvent
(
new
DataSourceInitializedEvent
(
this
.
dataSource
));
...
...
@@ -111,7 +114,9 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
private
void
runDataScripts
()
{
List
<
Resource
>
scripts
=
getScripts
(
this
.
properties
.
getData
(),
"data"
);
runScripts
(
scripts
);
String
username
=
this
.
properties
.
getDataUsername
();
String
password
=
this
.
properties
.
getDataPassword
();
runScripts
(
scripts
,
username
,
password
);
}
private
List
<
Resource
>
getScripts
(
String
locations
,
String
fallback
)
{
...
...
@@ -141,7 +146,7 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
return
resources
;
}
private
void
runScripts
(
List
<
Resource
>
resources
)
{
private
void
runScripts
(
List
<
Resource
>
resources
,
String
username
,
String
password
)
{
if
(
resources
.
isEmpty
())
{
return
;
}
...
...
@@ -154,7 +159,14 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
for
(
Resource
resource
:
resources
)
{
populator
.
addScript
(
resource
);
}
DatabasePopulatorUtils
.
execute
(
populator
,
this
.
dataSource
);
DataSource
dataSource
=
this
.
dataSource
;
if
(
StringUtils
.
hasText
(
username
)
&&
StringUtils
.
hasText
(
password
))
{
dataSource
=
DataSourceBuilder
.
create
(
this
.
properties
.
getClassLoader
())
.
driverClassName
(
this
.
properties
.
determineDriverClassName
())
.
url
(
this
.
properties
.
determineUrl
()).
username
(
username
)
.
password
(
password
).
build
();
}
DatabasePopulatorUtils
.
execute
(
populator
,
dataSource
);
}
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java
View file @
78639fff
...
...
@@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
* @author Maciej Walkowiak
* @author Stephane Nicoll
* @author Benedikt Ritter
* @author Eddú Meléndez
* @since 1.1.0
*/
@ConfigurationProperties
(
prefix
=
"spring.datasource"
)
...
...
@@ -103,11 +104,31 @@ public class DataSourceProperties
*/
private
String
schema
;
/**
* User of the database to execute DDL scripts (if different).
*/
private
String
schemaUsername
;
/**
* Password of the database to execute DDL scripts (if different).
*/
private
String
schemaPassword
;
/**
* Data (DML) script resource reference.
*/
private
String
data
;
/**
* User of the database to execute DML scripts.
*/
private
String
dataUsername
;
/**
* Password of the database to execute DML scripts.
*/
private
String
dataPassword
;
/**
* Do not stop if an error occurs while initializing the database.
*/
...
...
@@ -338,6 +359,22 @@ public class DataSourceProperties
this
.
schema
=
schema
;
}
public
String
getSchemaUsername
()
{
return
this
.
schemaUsername
;
}
public
void
setSchemaUsername
(
String
schemaUsername
)
{
this
.
schemaUsername
=
schemaUsername
;
}
public
String
getSchemaPassword
()
{
return
this
.
schemaPassword
;
}
public
void
setSchemaPassword
(
String
schemaPassword
)
{
this
.
schemaPassword
=
schemaPassword
;
}
public
String
getData
()
{
return
this
.
data
;
}
...
...
@@ -346,6 +383,22 @@ public class DataSourceProperties
this
.
data
=
script
;
}
public
String
getDataUsername
()
{
return
this
.
dataUsername
;
}
public
void
setDataUsername
(
String
dataUsername
)
{
this
.
dataUsername
=
dataUsername
;
}
public
String
getDataPassword
()
{
return
this
.
dataPassword
;
}
public
void
setDataPassword
(
String
dataPassword
)
{
this
.
dataPassword
=
dataPassword
;
}
public
boolean
isContinueOnError
()
{
return
this
.
continueOnError
;
}
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
View file @
78639fff
...
...
@@ -25,6 +25,7 @@ import org.junit.After;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.beans.factory.UnsatisfiedDependencyException
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
...
...
@@ -200,6 +201,50 @@ public class DataSourceInitializerTests {
}
}
@Test
public
void
testDataSourceInitializedWithSchemaCredentials
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.schema:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-schema.sql"
),
"spring.datasource.data:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-data.sql"
),
"spring.datasource.schema-username:admin"
,
"spring.datasource.schema-password:admin"
);
try
{
this
.
context
.
refresh
();
fail
(
"User does not exist"
);
}
catch
(
Exception
ex
)
{
assertThat
(
ex
).
isInstanceOf
(
UnsatisfiedDependencyException
.
class
);
}
}
@Test
public
void
testDataSourceInitializedWithDataCredentials
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.schema:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-schema.sql"
),
"spring.datasource.data:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-data.sql"
),
"spring.datasource.data-username:admin"
,
"spring.datasource.data-password:admin"
);
try
{
this
.
context
.
refresh
();
fail
(
"User does not exist"
);
}
catch
(
Exception
ex
)
{
assertThat
(
ex
).
isInstanceOf
(
UnsatisfiedDependencyException
.
class
);
}
}
@Configuration
@EnableConfigurationProperties
protected
static
class
TwoDataSources
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java
View file @
78639fff
...
...
@@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Maciej Walkowiak
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
public
class
DataSourcePropertiesTests
{
...
...
@@ -99,4 +100,22 @@ public class DataSourcePropertiesTests {
assertThat
(
properties
.
determinePassword
()).
isEqualTo
(
"bar"
);
}
@Test
public
void
determineCredentialsForSchemaScripts
()
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
setSchemaUsername
(
"foo"
);
properties
.
setSchemaPassword
(
"bar"
);
assertThat
(
properties
.
getSchemaUsername
()).
isEqualTo
(
"foo"
);
assertThat
(
properties
.
getSchemaPassword
()).
isEqualTo
(
"bar"
);
}
@Test
public
void
determineCredentialsForDataScripts
()
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
setDataUsername
(
"foo"
);
properties
.
setDataPassword
(
"bar"
);
assertThat
(
properties
.
getDataUsername
()).
isEqualTo
(
"foo"
);
assertThat
(
properties
.
getDataPassword
()).
isEqualTo
(
"bar"
);
}
}
spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc
View file @
78639fff
...
...
@@ -590,6 +590,8 @@ content into your application; rather pick only the properties that you need.
# DATASOURCE ({sc-spring-boot-autoconfigure}/jdbc/DataSourceAutoConfiguration.{sc-ext}[DataSourceAutoConfiguration] & {sc-spring-boot-autoconfigure}/jdbc/DataSourceProperties.{sc-ext}[DataSourceProperties])
spring.datasource.continue-on-error=false # Do not stop if an error occurs while initializing the database.
spring.datasource.data= # Data (DML) script resource reference.
spring.datasource.data-username= # User of the database to execute DML scripts (if different).
spring.datasource.data-password= # Password of the database to execute DML scripts (if different).
spring.datasource.dbcp.*= # Commons DBCP specific settings
spring.datasource.dbcp2.*= # Commons DBCP2 specific settings
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
...
...
@@ -601,6 +603,8 @@ content into your application; rather pick only the properties that you need.
spring.datasource.password= # Login password of the database.
spring.datasource.platform=all # Platform to use in the schema resource (schema-${platform}.sql).
spring.datasource.schema= # Schema (DDL) script resource reference.
spring.datasource.schema-username= # User of the database to execute DDL scripts (if different).
spring.datasource.schema-password= # Password of the database to execute DDL scripts (if different).
spring.datasource.separator=; # Statement separator in SQL initialization scripts.
spring.datasource.sql-script-encoding= # SQL scripts encoding.
spring.datasource.tomcat.*= # Tomcat datasource specific settings
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment