Skip to content

postgresql_table: Fix multiple schema handling (#817)#818

Merged
hunleyd merged 1 commit intoansible-collections:mainfrom
wiktel:table-fix-multiple-schemas
Apr 2, 2025
Merged

postgresql_table: Fix multiple schema handling (#817)#818
hunleyd merged 1 commit intoansible-collections:mainfrom
wiktel:table-fix-multiple-schemas

Conversation

@rlaager
Copy link
Contributor

@rlaager rlaager commented Mar 27, 2025

SUMMARY

The postgresql_table command shows spurious change status (and attempts unnecessary ALTER TABLE...OWNER TO) when multiple schemas have tables of the same name (which is the table being operated on by the task).

This happens because the query to get the existing information does not properly limit by schema name for the pg_tables view. It only limits for the pg_namespace part of the query. As a result, this query returns multiple rows, one per schema containing a table of the specified name.

Fixes #817

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

postgresql_table

ADDITIONAL INFORMATION

BEFORE:

changed: [krls1.sip.wiktel.com] => (item=office) => changed=true
  ansible_loop_var: item
  invocation:
    module_args:
      ca_cert: null
      cascade: false
      columns:
      - username                  varchar
      - domain                    varchar
      - name_path                 varchar
      - greeting_path             varchar                                                                                                - password                  varchar                                                                                                connect_params: {}
      db: freeswitch
      including: null
      like: null
      login_host: ''
      login_password: ''
      login_unix_socket: ''
      login_user: postgres
      name: freeswitch_office.voicemail_prefs
      owner: freeswitch_office
      port: 5432
      rename: null
      session_role: null
      ssl_cert: null
      ssl_key: null
      ssl_mode: prefer
      state: present
      storage_params: null
      table: freeswitch_office.voicemail_prefs
      tablespace: null
      truncate: false
      trust_input: true
      unlogged: false
  item: office
  owner: freeswitch_krls_pbx2
  queries:
  - ALTER TABLE "freeswitch_office"."voicemail_prefs" OWNER TO "freeswitch_office"
  state: present
  storage_params: []
  table: freeswitch_office.voicemail_prefs
  tablespace: ''

AFTER:

ok: [krls1.sip.wiktel.com] => (item=freeswitch_office) => changed=false
  ansible_loop_var: item
  invocation:
    module_args:
      ca_cert: null
      cascade: false
      columns:
      - username                  varchar
      - domain                    varchar
      - name_path                 varchar
      - greeting_path             varchar
      - password                  varchar
      connect_params: {}
      db: freeswitch
      including: null
      like: null
      login_host: ''
      login_password: ''
      login_unix_socket: ''
      login_user: postgres
      name: freeswitch_office.voicemail_prefs
      owner: freeswitch_office
      port: 5432
      rename: null
      session_role: null
      ssl_cert: null
      ssl_key: null
      ssl_mode: prefer
      state: present
      storage_params: null
      table: freeswitch_office.voicemail_prefs
      tablespace: null
      truncate: false
      trust_input: true
      unlogged: false
  item: office
  owner: freeswitch_office
  queries: []
  state: present
  storage_params: []
  table: freeswitch_office.voicemail_prefs
  tablespace: ''

@hunleyd
Copy link
Collaborator

hunleyd commented Mar 28, 2025

This looks good @rlaager but would you mind adding a changelog fragment and possibly a test?

@Andersson007
Copy link
Collaborator

@rlaager hi, here's the changelog guide link
thanks!

@rlaager rlaager force-pushed the table-fix-multiple-schemas branch from 370383e to 4de5c62 Compare March 31, 2025 21:35
@rlaager
Copy link
Contributor Author

rlaager commented Mar 31, 2025

Changelog fragment added. It and the commit message parallel 09fec84, which was the same issue with the postgresql_idx module.

…ions#817)

When checking to see if an table exists, the schema name needs to be
taken into account.  That is, the same name can exist in multiple
schemas.

Fixes ansible-collections#817
@rlaager rlaager force-pushed the table-fix-multiple-schemas branch from 54cff26 to 007b528 Compare April 1, 2025 20:20
@hunleyd hunleyd self-requested a review April 2, 2025 13:23
@hunleyd hunleyd merged commit c82bbba into ansible-collections:main Apr 2, 2025
26 checks passed
@hunleyd
Copy link
Collaborator

hunleyd commented Apr 2, 2025

thanks @rlaager !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

postgresql_table fails when multiple tables with the same name exist in different schemas

3 participants