Skip to content

Commit

Permalink
Add basic support for foreign tables
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomgalmeida authored and avbasov committed Sep 4, 2016
1 parent 17d958c commit a9e43b4
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ [email protected].
* Add support for ALTER TABLE ... OWNER TO (serge-pouliquen-itf)
* Add support for CREATE TYPE (Karol Rybak)
* Add support for CREATE EXTENSION (Átila Camurça Alves)
* Add basic support for CREATE FOREIGN TABLE (Bruno Almeida)

#### Fixes
* Added hint to use "CREATE TABLE ... CONSTRAINT name PRIMARY KEY/UNIQUE ..."
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ private static void updateTableColumns(final PrintWriter writer,
PgDiffUtils.getQuotedName(newTable.getName());
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println("ALTER TABLE " + quotedTableName);
writer.println("ALTER " + ((newTable.isForeign()) ? "FOREIGN ":"") + "TABLE " + quotedTableName);

for (int i = 0; i < statements.size(); i++) {
writer.print(statements.get(i));
Expand All @@ -632,7 +632,7 @@ private static void updateTableColumns(final PrintWriter writer,

if (!dropDefaultsColumns.isEmpty()) {
writer.println();
writer.println("ALTER TABLE " + quotedTableName);
writer.println("ALTER " + ((newTable.isForeign()) ? "FOREIGN ":"") + "TABLE " + quotedTableName);

for (int i = 0; i < dropDefaultsColumns.size(); i++) {
writer.print("\tALTER COLUMN ");
Expand Down Expand Up @@ -865,7 +865,7 @@ private static void alterOwnerTo(final PrintWriter writer,

if (newOwnerTo != null && !newOwnerTo.equals(oldOwnerTo)) {
writer.println();
writer.println("ALTER TABLE "
writer.println("ALTER " + ((newTable.isForeign()) ? "FOREIGN ":"") + "TABLE "
+ PgDiffUtils.getQuotedName(newTable.getName())
+ " OWNER TO " + newTable.getOwnerTo() + ";");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class PgDumpLoader { //NOPMD
* Pattern for testing whether it is CREATE TABLE statement.
*/
private static final Pattern PATTERN_CREATE_TABLE = Pattern.compile(
"^CREATE[\\s]+(UNLOGGED\\s)*TABLE[\\s]+.*$",
"^CREATE[\\s]+(UNLOGGED\\s|FOREIGN\\s)*TABLE[\\s]+.*$",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
/**
* Pattern for testing whether it is CREATE VIEW or CREATE MATERIALIZED
Expand All @@ -67,7 +67,7 @@ public class PgDumpLoader { //NOPMD
* Pattern for testing whether it is ALTER TABLE statement.
*/
private static final Pattern PATTERN_ALTER_TABLE =
Pattern.compile("^ALTER[\\s]+TABLE[\\s]+.*$",
Pattern.compile("^ALTER[\\s](FOREIGN)*TABLE[\\s]+.*$",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
/**
* Pattern for testing whether it is CREATE SEQUENCE statement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static void parse(final PgDatabase database,
* relation types, so we just ignore type here and derive its type from
* the original CREATE command.
*/
parser.expectOptional("FOREIGN");
//OK FOREIGN TABLE
if (parser.expectOptional("TABLE")) {
parser.expectOptional("ONLY");
} else if (parser.expectOptional("MATERIALIZED", "VIEW")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void parse(final PgDatabase database,
final Parser parser = new Parser(statement);
parser.expect("CREATE");
final boolean unlogged = parser.expectOptional("UNLOGGED");
final boolean foreign = parser.expectOptional("FOREIGN");
parser.expect("TABLE");

// Optional IF NOT EXISTS, irrelevant for our purposes
Expand All @@ -49,6 +50,7 @@ public static void parse(final PgDatabase database,

final PgTable table = new PgTable(ParserUtils.getObjectName(tableName), database, schema);
table.setUnlogged(unlogged);
table.setForeign(foreign);
schema.addRelation(table);

parser.expect("(");
Expand Down Expand Up @@ -77,7 +79,7 @@ public static void parse(final PgDatabase database,
if (parser.expectOptional("INHERITS")) {
parseInherits(database, parser, table);
} else if (parser.expectOptional("WITHOUT")) {
table.setWith("OIDS=false");
table.setWith("OIDS=false");
} else if (parser.expectOptional("WITH")) {
if (parser.expectOptional("OIDS")
|| parser.expectOptional("OIDS=true")) {
Expand All @@ -89,8 +91,10 @@ public static void parse(final PgDatabase database,
}
} else if (parser.expectOptional("TABLESPACE")) {
table.setTablespace(parser.parseString());
} else if (parser.expectOptional("SERVER")) {
table.setForeignServer(parser.getExpression());
} else {
parser.throwUnsupportedCommand();
parser.throwUnsupportedCommand();
}
}
}
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/cz/startnet/utils/pgdiff/schema/PgTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public class PgTable extends PgRelation {
* Is this a UNLOGGED table?
*/
private boolean unlogged;

/**
* Is this a FOREIGN table?
*/
private boolean foreign;
private String foreignServer;

/**
* PgDatabase
Expand Down Expand Up @@ -114,6 +118,9 @@ public String getCreationSQL(final PgSchema schema) {
if (isUnlogged()) {
sbSQL.append("UNLOGGED ");
}
if (isForeign()) {
sbSQL.append("FOREIGN ");
}
sbSQL.append("TABLE ");
sbSQL.append(PgDiffUtils.getQuotedName(name));
sbSQL.append(" (");
Expand Down Expand Up @@ -181,6 +188,11 @@ public String getCreationSQL(final PgSchema schema) {
}
}

if (isForeign()) {
sbSQL.append("SERVER ");
sbSQL.append(getForeignServer());
}

if (tablespace != null && !tablespace.isEmpty()) {
sbSQL.append(System.getProperty("line.separator"));
sbSQL.append("TABLESPACE ");
Expand Down Expand Up @@ -401,4 +413,30 @@ public boolean isUnlogged() {
public void setUnlogged(boolean unlogged) {
this.unlogged = unlogged;
}

/**
* Foreign Tables
*/

@Override
public String getDropSQL() {
return "DROP " + ((isForeign()) ? "FOREIGN ":"") + getRelationKind() + " " +
PgDiffUtils.getQuotedName(getName()) + ";";
}

public boolean isForeign() {
return foreign;
}

public void setForeign(boolean foreign) {
this.foreign = foreign;
}

public void setForeignServer(String server){
foreignServer = server;
}

public String getForeignServer(){
return foreignServer;
}
}
7 changes: 7 additions & 0 deletions src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ public static Collection<?> parameters() {
{"add_table_issue115", false, false, false, false},
{"add_column_issue134", false, false, false, false},
{"add_column_issue188", false, false, false, false},
{"add_column_issue188", false, false, false, false},
{"view_alias_with_quote", false, false, false, false},
// Tests view triggers (support for 'INSTEAD OF')
//90
{"view_triggers", false, false, false, false},
// Tests privileges
{"grant_on_table_sequence", false, false, false, false},
Expand All @@ -245,6 +247,11 @@ public static Collection<?> parameters() {
, {"add_type", false, false, false, false}
, {"drop_type", false, false, false, false}
, {"alter_type", false, false, false, false}
// Test Foreign Tables
, {"foreign_create_table", false, false, false, false}
, {"foreign_drop_table", false, false, false, false}
//100
, {"foreign_alter_type", false, false, false, false}
});
}
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER FOREIGN TABLE foreign_to_alter
ADD COLUMN country_code character varying(5),
ALTER COLUMN ref2 TYPE character varying(20) /* TYPE change - table: foreign_to_alter original: character varying(60) new: character varying(20) */,
ALTER COLUMN deleted TYPE numeric(1,0) /* TYPE change - table: foreign_to_alter original: boolean new: numeric(1,0) */;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE FOREIGN TABLE foreign_to_alter (
id bigint,
user_id bigint,
ref1 character varying(60),
ref2 character varying(20),
inplay_stake_coef numeric,
pre_match_stake_coef numeric,
punter_limits character varying,
name character varying(60),
colour_cat character varying(30),
deleted numeric(1,0),
country_code character varying(5)
)
SERVER ats
OPTIONS (
updatable 'false'
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE FOREIGN TABLE foreign_to_alter (
id bigint,
user_id bigint,
ref1 character varying(60),
ref2 character varying(60),
inplay_stake_coef numeric,
pre_match_stake_coef numeric,
punter_limits character varying,
name character varying(60),
colour_cat character varying(30),
deleted boolean
)
SERVER ats
OPTIONS (
updatable 'false'
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

CREATE FOREIGN TABLE foreign_to_create (
id bigint
)SERVER ats
OPTIONS (
updatable 'false'
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

CREATE FOREIGN TABLE foreign_to_create (
id bigint
) SERVER ats
OPTIONS (
updatable 'false'
);
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

DROP FOREIGN TABLE foreign_to_drop;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE FOREIGN TABLE foreign_to_drop (
id bigint
)
SERVER ats
OPTIONS (
updatable 'false'
);

0 comments on commit a9e43b4

Please sign in to comment.