Skip to content

Commit 610577c

Browse files
committed
Merge remote-tracking branch 'origin/master' into release
2 parents 70783b9 + 292d2fa commit 610577c

2 files changed

Lines changed: 65 additions & 11 deletions

File tree

src/main/java/ru/fusionsoft/dbgit/postgres/DBAdapterPostgres.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,16 @@ public Map<String, DBTable> getTables(String schema) {
212212
" JOIN ONLY pg_catalog.pg_namespace n2 ON n2.oid = c2.relnamespace\n" +
213213
" WHERE c.conrelid = to_regclass('\"' || schemaname || '\".\"' || tablename || '\"')::oid\n" +
214214
" and c1.relkind = 'r' AND c.contype = 'f'\n" +
215-
" )\n" +
216-
"from pg_tables \n" +
215+
" ), \n" +
216+
"(SELECT oid FROM pg_class WHERE relname = tablename and relnamespace = (select oid from pg_namespace where nspname = :schema)) oid, \n" +
217+
" pg_get_partkeydef((SELECT oid FROM pg_class WHERE relname = tablename and relnamespace = (select oid from pg_namespace where nspname = :schema))) partkeydef, \n" +
218+
" parent.relname parent, \n" +
219+
" pg_get_expr(child.relpartbound, child.oid) \n" +
220+
"from pg_tables \n" +
221+
"left outer join pg_inherits on (SELECT oid FROM pg_class WHERE relname = tablename and relnamespace = (select oid from pg_namespace where nspname = :schema)) = pg_inherits.inhrelid \n" +
222+
"left outer JOIN pg_class parent ON pg_inherits.inhparent = parent.oid \n" +
223+
"left outer JOIN pg_class child ON pg_inherits.inhrelid = child.oid \n" +
224+
217225
"where upper(schemaname) = upper(:schema)";
218226
Connection connect = getConnection();
219227

@@ -229,6 +237,10 @@ public Map<String, DBTable> getTables(String schema) {
229237
if(rs.getArray("dependencies") != null){
230238
table.setDependencies(new HashSet<>(Arrays.asList((String[])rs.getArray("dependencies").getArray())));
231239
} else table.setDependencies(new HashSet<>());
240+
if (rs.getString("parent") != null) {
241+
table.getDependencies().add(schema + "/" + rs.getString("parent") + ".tbl");
242+
}
243+
232244
rowToProperties(rs, table.getOptions());
233245
listTable.put(nameTable, table);
234246
}
@@ -255,8 +267,15 @@ public DBTable getTable(String schema, String name) {
255267
" JOIN ONLY pg_catalog.pg_namespace n2 ON n2.oid = c2.relnamespace\n" +
256268
" WHERE c.conrelid = to_regclass('\"' || schemaname || '\".\"' || tablename || '\"')::oid\n" +
257269
" and c1.relkind = 'r' AND c.contype = 'f'\n" +
258-
" )" +
270+
" ), \n" +
271+
"(SELECT oid FROM pg_class WHERE relname = tablename and relnamespace = (select oid from pg_namespace where nspname = '"+schema+"')) oid, \n" +
272+
" pg_get_partkeydef((SELECT oid FROM pg_class WHERE relname = tablename and relnamespace = (select oid from pg_namespace where nspname = '"+schema+"'))) partkeydef, \n" +
273+
" parent.relname parent, \n" +
274+
" pg_get_expr(child.relpartbound, child.oid) \n" +
259275
"from pg_tables \n" +
276+
"left outer join pg_inherits on (SELECT oid FROM pg_class WHERE relname = tablename and relnamespace = (select oid from pg_namespace where nspname = '"+schema+"')) = pg_inherits.inhrelid \n" +
277+
"left outer JOIN pg_class parent ON pg_inherits.inhparent = parent.oid \n" +
278+
"left outer JOIN pg_class child ON pg_inherits.inhrelid = child.oid \n" +
260279
"where upper(schemaname) = upper('"+schema+"') \n" +
261280
"and tablename = '"+name+"'\n";
262281
try {
@@ -277,6 +296,10 @@ public DBTable getTable(String schema, String name) {
277296
if(rs.getArray("dependencies") != null){
278297
table.setDependencies(new HashSet<>(Arrays.asList((String[])rs.getArray("dependencies").getArray())));
279298
} else table.setDependencies(new HashSet<>());
299+
if (rs.getString("parent") != null) {
300+
table.getDependencies().add(schema + "/" + rs.getString("parent") + ".tbl");
301+
}
302+
280303
rowToProperties(rs, table.getOptions());
281304
}
282305
stmt.close();

src/main/java/ru/fusionsoft/dbgit/postgres/DBRestoreTablePostgres.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void restoreTablePostgres(IMetaObject obj) throws Exception
6262
ConsoleWriter.detailsPrint(lang.getValue("general", "restore", "restoreTable").withParams(schema+"."+tblName), 1);
6363

6464
//find existing table and set tablespace or create
65-
if(existingTable.loadFromDB()){
65+
if (existingTable.loadFromDB()){
6666
StringProperties exTablespace = existingTable.getTable().getOptions().get("tablespace");
6767
StringProperties restoreTablespace = restoreTable.getTable().getOptions().get("tablespace");
6868
if(restoreTablespace != null &&
@@ -91,18 +91,38 @@ public void restoreTablePostgres(IMetaObject obj) throws Exception
9191
ConsoleWriter.detailsPrintlnGreen(lang.getValue("general", "ok"));
9292
} else {
9393
ConsoleWriter.detailsPrint(lang.getValue("general", "restore", "createTable"), 2);
94+
95+
Collection<DBTableField> fields = restoreTable.getFields().values();
96+
StringBuilder columnsBld = new StringBuilder();
97+
98+
for (DBTableField field : fields) {
99+
String fieldStr = field.getName() + " " + field.getTypeSQL().replace("NOT NULL" , "");
100+
columnsBld.append(fieldStr).append(", ");
101+
}
102+
String columns = columnsBld.substring(0, columnsBld.length() - 2);
103+
94104
String createTableDdl = MessageFormat.format(
95-
"create table {0}.{1}() tablespace {2};\n alter table {0}.{1} owner to {3}"
105+
"create table {0}.{1}({4}) {2};\n alter table {0}.{1} owner to {3}"
96106
,schema
97107
,tblName
98108
,restoreTable.getTable().getOptions().getChildren().containsKey("tablespace")
99-
? restoreTable.getTable().getOptions().get("tablespace").getData()
100-
: "pg_default"
109+
? "tablespace " + restoreTable.getTable().getOptions().get("tablespace").getData()
110+
: ""
101111
,restoreTable.getTable().getOptions().getChildren().containsKey("owner")
102112
? restoreTable.getTable().getOptions().get("owner").getData()
103113
: "postgres"
114+
, columns
104115
);
116+
117+
if (restoreTable.getTable().getOptions().getChildren().containsKey("partkeydef")) {
118+
createTableDdl = createTableDdl.replace(" ) ",
119+
") PARTITION BY " +
120+
restoreTable.getTable().getOptions().getChildren().get("partkeydef")
121+
+ " ");
122+
}
123+
105124
st.execute(createTableDdl);
125+
106126
ConsoleWriter.detailsPrintlnGreen(lang.getValue("general", "ok"));
107127
}
108128

@@ -131,12 +151,13 @@ public void restoreTablePostgres(IMetaObject obj) throws Exception
131151

132152
for(DBTableField tblField : values) {
133153
String fieldName = DBAdapterPostgres.escapeNameIfNeeded(tblField.getName());
154+
/*
134155
st.execute(
135156
"alter table "+ tblSam +" add column "
136157
+ fieldName + " "
137158
+ tblField.getTypeSQL().replace("NOT NULL", "")
138159
);
139-
160+
*/
140161
if (tblField.getDescription() != null && tblField.getDescription().length() > 0)
141162
st.execute(
142163
"COMMENT ON COLUMN " + tblSam + "."
@@ -160,7 +181,16 @@ public void restoreTablePostgres(IMetaObject obj) throws Exception
160181
}
161182
ConsoleWriter.detailsPrintlnGreen(lang.getValue("general", "ok"));
162183
}
163-
184+
185+
if (restoreTable.getTable().getOptions().getChildren().containsKey("parent")) {
186+
String attachPart = " ALTER TABLE " +
187+
schema + "." + restoreTable.getTable().getOptions().getChildren().get("parent") +
188+
" ATTACH PARTITION " +
189+
schema + "." + tblName + " " +
190+
restoreTable.getTable().getOptions().getChildren().get("pg_get_expr");
191+
st.execute(attachPart);
192+
}
193+
164194
if(!diffTableFields.entriesOnlyOnRight().isEmpty()) {
165195
ConsoleWriter.detailsPrint(lang.getValue("general", "restore", "droppingColumns"), 2);
166196
for(DBTableField tblField:diffTableFields.entriesOnlyOnRight().values()) {
@@ -309,7 +339,7 @@ public void restoreTableFieldsPostgres(IMetaObject obj) throws Exception
309339
}
310340
// set primary key
311341
for(DBConstraint tableconst: restoreTable.getConstraints().values()) {
312-
if(tableconst.getConstraintType().equals("p")) {
342+
if(tableconst.getConstraintType().equals("p") && !restoreTable.getTable().getOptions().getChildren().containsKey("parent")) {
313343
st.execute("alter table "+ tblName +" add constraint "+ DBAdapterPostgres.escapeNameIfNeeded(tableconst.getName()) + " "+tableconst.getSql().replace(" " + tableconst.getSql() + ".", " " + schema + "."));
314344
break;
315345
}
@@ -461,7 +491,8 @@ private void createConstraint(MetaTable restoreTable, DBConstraint constr, State
461491
.replace(" " + constr.getSchema() + ".", " " + schema + ".")
462492
.replace("REFERENCES ", "REFERENCES " + schema + ".")
463493
);
464-
st.execute(constrDdl);
494+
if (!restoreTable.getTable().getOptions().getChildren().containsKey("parent"))
495+
st.execute(constrDdl);
465496
}
466497

467498

0 commit comments

Comments
 (0)