Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedupe CREATE SCHEMA PUBLIC statements #265

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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);