Skip to content

Commit

Permalink
Dedupe CREATE SCHEMA PUBLIC statements
Browse files Browse the repository at this point in the history
Previously, the CreateSchemaParser blindly added a schema. Instead,
only add the schema if it's not `public`. For additional safety, throw
an error if a user tries to add a schema that already exists.
  • Loading branch information
jschaf committed Jan 28, 2020
1 parent 735c05c commit ebb9f66
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,20 @@ public static void parse(final PgDatabase database,
final Parser parser = new Parser(statement);
parser.expect("CREATE", "SCHEMA");

if (parser.expectOptional("AUTHORIZATION")) {
final PgSchema schema = new PgSchema(
ParserUtils.getObjectName(parser.parseIdentifier()));
database.addSchema(schema);
schema.setAuthorization(schema.getName());

final String definition = parser.getRest();

if (definition != null && !definition.isEmpty()) {
schema.setDefinition(definition);
}
} else {
final PgSchema schema = new PgSchema(
ParserUtils.getObjectName(parser.parseIdentifier()));
String schemaName = ParserUtils.getObjectName(parser.parseIdentifier());
PgSchema schema = database.getSchema(schemaName);
if (schema == null) {
schema = new PgSchema(schemaName);
database.addSchema(schema);
}

if (parser.expectOptional("AUTHORIZATION")) {
schema.setAuthorization(
ParserUtils.getObjectName(parser.parseIdentifier()));
}

final String definition = parser.getRest();
if (parser.expectOptional("AUTHORIZATION")) {
schema.setAuthorization(schema.getName());
}

if (definition != null && !definition.isEmpty()) {
schema.setDefinition(definition);
}
final String definition = parser.getRest();
if (definition != null && !definition.isEmpty()) {
schema.setDefinition(definition);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/schema/PgDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package cz.startnet.utils.pgdiff.schema;

import cz.startnet.utils.pgdiff.parsers.ParserException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -135,8 +136,15 @@ public List<PgSchema> getSchemas() {
* Adds {@code schema} to the lists of schemas.
*
* @param schema schema
* @throws ParserException Thrown if schema was already added to the database.
*/
public void addSchema(final PgSchema schema) {
for (PgSchema existingSchema : schemas) {
if (existingSchema.getName().equals(schema.getName())) {
throw new ParserException(
"Schema '" + schema.getName() + "' already added to database");
}
}
schemas.add(schema);
}

Expand Down
1 change: 1 addition & 0 deletions src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public static Collection<?> parameters() {
, {"alter_view_owner", false, false, false, false}
, {"grant_on_table_cols_mixed", false, false, false, false}
, {"grant_on_view_cols_mixed", false, false, false, false}
, {"create_schema_no_change_table", false, false, false, false}
});
}
/**
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE SCHEMA public;
CREATE TABLE public.node (instance_id text);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE SCHEMA public;
CREATE TABLE public.node (instance_id text);

0 comments on commit ebb9f66

Please sign in to comment.