From 885cecfc9496cfff23efc13245893d470cc8f724 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Sat, 14 Dec 2019 11:13:59 +0300 Subject: [PATCH] postgresql modules: use query parameters with cursor objects (#65791) --- ...l_modules_use_query_params_with_cursor.yml | 4 ++++ .../database/postgresql/postgresql_set.py | 4 ++-- .../database/postgresql/postgresql_slot.py | 22 +++++++++++-------- .../postgresql/postgresql_subscription.py | 8 +++---- .../tasks/postgresql_slot_initial.yml | 4 ++-- 5 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 changelogs/fragments/65791-postgresql_modules_use_query_params_with_cursor.yml diff --git a/changelogs/fragments/65791-postgresql_modules_use_query_params_with_cursor.yml b/changelogs/fragments/65791-postgresql_modules_use_query_params_with_cursor.yml new file mode 100644 index 00000000000..831ffd6cad7 --- /dev/null +++ b/changelogs/fragments/65791-postgresql_modules_use_query_params_with_cursor.yml @@ -0,0 +1,4 @@ +bugfixes: +- postgresql_set - use query parameters with cursor object (https://github.com/ansible/ansible/pull/65791). +- postgresql_slot - use query parameters with cursor object (https://github.com/ansible/ansible/pull/65791). +- postgresql_subscription - use query parameters with cursor object (https://github.com/ansible/ansible/pull/65791). diff --git a/lib/ansible/modules/database/postgresql/postgresql_set.py b/lib/ansible/modules/database/postgresql/postgresql_set.py index da8d24b77f5..ba0bec55071 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_set.py +++ b/lib/ansible/modules/database/postgresql/postgresql_set.py @@ -185,9 +185,9 @@ POSSIBLE_SIZE_UNITS = ("mb", "gb", "tb") def param_get(cursor, module, name): query = ("SELECT name, setting, unit, context, boot_val " - "FROM pg_settings WHERE name = '%s'" % name) + "FROM pg_settings WHERE name = %(name)s") try: - cursor.execute(query) + cursor.execute(query, {'name': name}) info = cursor.fetchall() cursor.execute("SHOW %s" % name) val = cursor.fetchone() diff --git a/lib/ansible/modules/database/postgresql/postgresql_slot.py b/lib/ansible/modules/database/postgresql/postgresql_slot.py index b33255b3747..ff6198e518e 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_slot.py +++ b/lib/ansible/modules/database/postgresql/postgresql_slot.py @@ -184,26 +184,30 @@ class PgSlot(object): if kind == 'physical': # Check server version (needs for immedately_reserverd needs 9.6+): if self.cursor.connection.server_version < 96000: - query = "SELECT pg_create_physical_replication_slot('%s')" % self.name + query = "SELECT pg_create_physical_replication_slot(%(name)s)" else: - query = "SELECT pg_create_physical_replication_slot('%s', %s)" % (self.name, immediately_reserve) + query = "SELECT pg_create_physical_replication_slot(%(name)s, %(i_reserve)s)" + + self.changed = exec_sql(self, query, + query_params={'name': self.name, 'i_reserve': immediately_reserve}, + ddl=True) elif kind == 'logical': - query = "SELECT pg_create_logical_replication_slot('%s', '%s')" % (self.name, output_plugin) - - self.changed = exec_sql(self, query, ddl=True) + query = "SELECT pg_create_logical_replication_slot(%(name)s, %(o_plugin)s)" + self.changed = exec_sql(self, query, + query_params={'name': self.name, 'o_plugin': output_plugin}, ddl=True) def drop(self): if not self.exists: return False - query = "SELECT pg_drop_replication_slot('%s')" % self.name - self.changed = exec_sql(self, query, ddl=True) + query = "SELECT pg_drop_replication_slot(%(name)s)" + self.changed = exec_sql(self, query, query_params={'name': self.name}, ddl=True) def __slot_exists(self): - query = "SELECT slot_type FROM pg_replication_slots WHERE slot_name = '%s'" % self.name - res = exec_sql(self, query, add_to_executed=False) + query = "SELECT slot_type FROM pg_replication_slots WHERE slot_name = %(name)s" + res = exec_sql(self, query, query_params={'name': self.name}, add_to_executed=False) if res: self.exists = True self.kind = res[0][0] diff --git a/lib/ansible/modules/database/postgresql/postgresql_subscription.py b/lib/ansible/modules/database/postgresql/postgresql_subscription.py index d7e1759deaa..20fbc8133fa 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_subscription.py +++ b/lib/ansible/modules/database/postgresql/postgresql_subscription.py @@ -555,9 +555,9 @@ class PgSubscription(): "ON s.subdbid = d.oid " "JOIN pg_catalog.pg_roles AS r " "ON s.subowner = r.oid " - "WHERE s.subname = '%s' AND d.datname = '%s'" % (self.name, self.db)) + "WHERE s.subname = %(name)s AND d.datname = %(db)s") - result = exec_sql(self, query, add_to_executed=False) + result = exec_sql(self, query, query_params={'name': self.name, 'db': self.db}, add_to_executed=False) if result: return result[0] else: @@ -573,9 +573,9 @@ class PgSubscription(): "FROM pg_catalog.pg_subscription_rel r " "JOIN pg_catalog.pg_subscription s ON s.oid = r.srsubid " "JOIN pg_catalog.pg_class c ON c.oid = r.srrelid " - "WHERE s.subname = '%s'" % self.name) + "WHERE s.subname = %(name)s") - result = exec_sql(self, query, add_to_executed=False) + result = exec_sql(self, query, query_params={'name': self.name}, add_to_executed=False) if result: return [dict(row) for row in result] else: diff --git a/test/integration/targets/postgresql_slot/tasks/postgresql_slot_initial.yml b/test/integration/targets/postgresql_slot/tasks/postgresql_slot_initial.yml index 3cf47ac6bbc..e7977f4bcdd 100644 --- a/test/integration/targets/postgresql_slot/tasks/postgresql_slot_initial.yml +++ b/test/integration/targets/postgresql_slot/tasks/postgresql_slot_initial.yml @@ -73,7 +73,7 @@ - assert: that: - result is changed - - result.queries == ["SELECT pg_create_physical_replication_slot('slot0', False)"] + - result.queries == ["SELECT pg_create_physical_replication_slot('slot0', false)"] when: postgres_version_resp.stdout is version('9.6', '>=') - assert: @@ -177,7 +177,7 @@ - assert: that: - result is changed - - result.queries == ["SELECT pg_create_physical_replication_slot('slot1', True)"] + - result.queries == ["SELECT pg_create_physical_replication_slot('slot1', true)"] when: postgres_version_resp.stdout is version('9.6', '>=') # Check, rowcount must be 1