Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Correct order of creation/destruction of FDW-related objects.

 * pyrseas/database.py (Database.diff_map): Reorder calls and add
   calls to drop servers and FDWs.
 * pyrseas/dbobject/foreign.py (ForeignDataWrapperDict.diff_map): Mark
   wrappers to be dropped.  (ForeignDataWrapperDict._drop): New method
   to actually drop the wrappers.  (ForeignServerDict.diff_map): Mark
   servers to be dropped.  (ForeignServerDict._drop): New method to
   actually drop the servers.
 * tests/dbobject/test_foreign.py: New tests to verify order of
   multiple object creation/destruction.
  • Loading branch information...
commit c0f146e96b26c04796e4dfbf7dceffbc2f8378f8 1 parent 0b3df87
@jmafc jmafc authored
View
6 pyrseas/database.py
@@ -218,9 +218,9 @@ def diff_map(self, input_map, schemas=[]):
stmts.append(self.db.tsparsers.diff_map(self.ndb.tsparsers))
stmts.append(self.db.tsconfigs.diff_map(self.ndb.tsconfigs))
stmts.append(self.db.casts.diff_map(self.ndb.casts))
- stmts.append(self.db.usermaps.diff_map(self.ndb.usermaps))
- stmts.append(self.db.servers.diff_map(self.ndb.servers))
stmts.append(self.db.fdwrappers.diff_map(self.ndb.fdwrappers))
+ stmts.append(self.db.servers.diff_map(self.ndb.servers))
+ stmts.append(self.db.usermaps.diff_map(self.ndb.usermaps))
stmts.append(self.db.ftables.diff_map(self.ndb.ftables))
stmts.append(self.db.operators._drop())
stmts.append(self.db.operclasses._drop())
@@ -228,5 +228,7 @@ def diff_map(self, input_map, schemas=[]):
stmts.append(self.db.functions._drop())
stmts.append(self.db.types._drop())
stmts.append(self.db.schemas._drop())
+ stmts.append(self.db.servers._drop())
+ stmts.append(self.db.fdwrappers._drop())
stmts.append(self.db.languages._drop())
return [s for s in flatten(stmts)]
View
22 pyrseas/dbobject/foreign.py
@@ -183,6 +183,17 @@ def diff_map(self, inwrappers):
for fdw in self.keys():
# if missing, drop it
if fdw not in inwrappers:
+ self[fdw].dropped = True
+ return stmts
+
+ def _drop(self):
+ """Actually drop the wrappers
+
+ :return: SQL statements
+ """
+ stmts = []
+ for fdw in self.keys():
+ if hasattr(self[fdw], 'dropped'):
stmts.append(self[fdw].drop())
return stmts
@@ -335,6 +346,17 @@ def diff_map(self, inservers):
for srv in self.keys():
# if missing, drop it
if srv not in inservers:
+ self[srv].dropped = True
+ return stmts
+
+ def _drop(self):
+ """Actually drop the servers
+
+ :return: SQL statements
+ """
+ stmts = []
+ for srv in self.keys():
+ if hasattr(self[srv], 'dropped'):
stmts.append(self[srv].drop())
return stmts
View
31 tests/dbobject/test_foreign.py
@@ -151,6 +151,14 @@ def test_create_server(self):
dbsql = self.db.process_map(inmap)
self.assertEqual(fix_indent(dbsql[0]), CREATE_FS_STMT)
+ def test_create_wrapper_server(self):
+ "Create a foreign data wrapper and its server"
+ inmap = self.std_map()
+ inmap.update({'foreign data wrapper fdw1': {'server fs1': {}}})
+ dbsql = self.db.process_map(inmap)
+ self.assertEqual(fix_indent(dbsql[0]), CREATE_FDW_STMT)
+ self.assertEqual(fix_indent(dbsql[1]), CREATE_FS_STMT)
+
def test_create_server_type_version(self):
"Create a foreign server with type and version"
self.db.execute_commit(CREATE_FDW_STMT)
@@ -242,6 +250,16 @@ def test_create_user_mapping(self):
dbsql = self.db.process_map(inmap)
self.assertEqual(fix_indent(dbsql[0]), CREATE_UM_STMT)
+ def test_create_wrapper_server_mapping(self):
+ "Create a FDW, server and user mapping"
+ inmap = self.std_map()
+ inmap.update({'foreign data wrapper fdw1': {'server fs1': {
+ 'user mappings': {'PUBLIC': {}}}}})
+ dbsql = self.db.process_map(inmap)
+ self.assertEqual(fix_indent(dbsql[0]), CREATE_FDW_STMT)
+ self.assertEqual(fix_indent(dbsql[1]), CREATE_FS_STMT)
+ self.assertEqual(fix_indent(dbsql[2]), CREATE_UM_STMT)
+
def test_create_user_mapping_options(self):
"Create a user mapping with options"
self.db.execute(CREATE_FDW_STMT)
@@ -347,6 +365,19 @@ def test_drop_foreign_table(self):
dbsql = self.db.process_map(inmap)
self.assertEqual(dbsql[0], "DROP FOREIGN TABLE ft1")
+ def test_drop_foreign_table_server(self):
+ "Drop a foreign table and associated server"
+ if self.db.version < 90100:
+ self.skipTest('Only available on PG 9.1')
+ self.db.execute(CREATE_FDW_STMT)
+ self.db.execute(CREATE_FS_STMT)
+ self.db.execute_commit(CREATE_FT_STMT)
+ inmap = self.std_map()
+ inmap.update({'foreign data wrapper fdw1': {}})
+ dbsql = self.db.process_map(inmap)
+ self.assertEqual(dbsql[0], "DROP FOREIGN TABLE ft1")
+ self.assertEqual(dbsql[1], "DROP SERVER fs1")
+
def suite():
tests = unittest.TestLoader().loadTestsFromTestCase(
Please sign in to comment.
Something went wrong with that request. Please try again.