Skip to content

Commit 1c74ffc

Browse files
committed
Fix to use schema if set in PostgresInitDatabase
1 parent 722ca35 commit 1c74ffc

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

ebean-datasource-api/src/main/java/io/ebean/datasource/PostgresInitDatabase.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ public class PostgresInitDatabase implements InitDatabase {
1717
public void run(Connection connection, DataSourceConfig config) throws SQLException {
1818
String username = config.getUsername();
1919
String password = config.getPassword();
20-
log.log(System.Logger.Level.INFO, "Creating schema and role for {0}", username);
21-
execute(connection, String.format("create schema if not exists %s", username));
20+
String schema = config.getSchema();
21+
if (schema == null) {
22+
schema = username;
23+
}
24+
log.log(System.Logger.Level.INFO, "Creating schema {0} role {1}", schema, username);
25+
execute(connection, String.format("create schema if not exists %s", schema));
2226
execute(connection, String.format("create role %s with login password '%s'", username, password));
23-
execute(connection, String.format("grant all on schema %s to %s", username, username));
27+
execute(connection, String.format("grant all on schema %s to %s", schema, username));
2428
}
2529

2630
private void execute(Connection connection, String sql) throws SQLException {

ebean-datasource/src/test/java/io/ebean/datasource/test/PostgresInitTest.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,39 @@ void test_with_clientInfo() throws SQLException {
7777
}
7878

7979
@Test
80-
void test_with_applicationName() throws SQLException {
80+
void test_with_applicationNameAndSchema() throws SQLException {
8181
DataSourceConfig ds = new DataSourceConfig();
8282
ds.setUrl("jdbc:postgresql://127.0.0.1:9999/app");
83-
// our application credentials (typically same as db and schema name with Postgres)
84-
ds.setUsername("app");
85-
ds.setPassword("app_pass");
86-
// database owner credentials used to create the "app" role as needed
87-
ds.setOwnerUsername("db_owner");
88-
ds.setOwnerPassword("test");
83+
ds.setSchema("fred");
84+
ds.setUsername("db_owner");
85+
ds.setPassword("test");
8986
ds.setApplicationName("my-application-name");
9087

9188
DataSourcePool pool = DataSourceFactory.create("app", ds);
9289
try {
9390
try (Connection connection = pool.getConnection()) {
94-
try (PreparedStatement statement = connection.prepareStatement("create table if not exists app.my_table (acol integer);")) {
91+
try (PreparedStatement statement = connection.prepareStatement("create schema if not exists fred;")) {
9592
statement.execute();
9693
}
9794
connection.commit();
95+
try (PreparedStatement statement = connection.prepareStatement("create table if not exists fred_table (acol integer);")) {
96+
statement.execute();
97+
}
98+
try (PreparedStatement statement = connection.prepareStatement("insert into fred_table (acol) values (?);")) {
99+
statement.setInt(1, 42);
100+
int rows = statement.executeUpdate();
101+
assertThat(rows).isEqualTo(1);
102+
}
103+
try (PreparedStatement statement = connection.prepareStatement("select acol from fred.fred_table")) {
104+
try (ResultSet resultSet = statement.executeQuery()) {
105+
while(resultSet.next()) {
106+
int res = resultSet.getInt(1);
107+
assertThat(res).isEqualTo(42);
108+
}
109+
}
110+
}
111+
connection.commit();
112+
98113
try (PreparedStatement statement = connection.prepareStatement("select application_name from pg_stat_activity where usename = ?")) {
99114
statement.setString(1, "app");
100115
try (ResultSet rset = statement.executeQuery()) {

0 commit comments

Comments
 (0)