Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New module postgresql_table - create postgresql table on remote host #52077

Open
wants to merge 9 commits into
base: devel
from

Conversation

Projects
None yet
5 participants
@Andersson007
Copy link
Contributor

Andersson007 commented Feb 12, 2019

SUMMARY

Postgresql_table module allows to create (including LIKE statement), drop, rename, or truncate a table. Also allows to change some attributes, specifically, owner, tablespace, storage parameters.

ISSUE TYPE
  • New Module Pull Request
MAIN PARAMETERS
db: databasename
table: tablename
state: present | absent
columns: col1 int primary key, col2 text not null, col3 biging
owner: owner
unlogged: yes | no
like: src_table
including: all
storage_params: fill_factor=100, autovacuum_vacuum_threshold=1
truncate: yes | no
rename: newname
EXAMPLES
- name: Create tbl2 in the acme database with the DDL like tbl1 with testuser as an owner
  postgresql_table:
    db: acme
    name: tbl2
    like: tbl1
    owner: testuser

- name: Create tbl2 in the acme database and tablespace ssd with the DDL like tbl1 including comments and indexes
  postgresql_table:
    db: acme
    table: tbl2
    like: tbl1
    including: comments, indexes
    tablespace: ssd

- name: Create test_table with several columns in ssd tablespace with fillfactor=10 and autovacuum_analyze_threshold=1
  postgresql_table:
    name: test_table
    columns: id bigserial primary key, num bigint, stories text
    tablespace: ssd
    storage_params: fillfactor=10, autovacuum_analyze_threshold=1

- name: Create an unlogged table
  postgresql_table:
    name: useless_data
    columns: waste_id int
    unlogged: true

- name: Rename table foo to bar
  postgresql_table:
    table: foo
    rename: bar

- name: Set owner to someuser
  postgresql_table:
    name: foo
    owner: someuser

- name: Change tablespace of foo table to new_tablespace and set owner to new_user
  postgresql_table:
    name: foo
    tablespace: new_tablespace
    owner: new_user

- name: Truncate table foo
  postgresql_table:
    name: foo
    truncate: yes

- name: Drop table foo
  postgresql_table:
    name: foo
    state: absent
RETURN
table:
  description: Name of a table.
  returned: always
  type: str
  sample: 'foo'
state:
  description: Table state.
  returned: always
  type: str
  sample: 'present'
owner:
  description: Table owner.
  returned: always
  type: str
  sample: 'postgres'
tablespace:
  description: Tablespace.
  returned: always
  type: str
  sample: 'ssd_tablespace'
query:
  description: Query that was tried to be execute.
  returned: always
  type: str
  sample: 'CREATE TABLE test_table (id bigint)'
storage_params:
  description: Storage parameters.
  returned: always
  type: list
  sample: [ "fillfactor=100", "autovacuum_analyze_threshold=1" ]
@Andersson007

This comment has been minimized.

Copy link
Contributor Author

Andersson007 commented Feb 12, 2019

+label: postgresql

@ansibot

This comment has been minimized.

@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented Feb 12, 2019

@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented Feb 12, 2019

@Dorn- @andytom @b6d @dschep @jbscalia @jensdepuydt @kostiantyn-nemchenko @kustodian @matburt @nerzhul

As a maintainer of a module in the same namespace this new module has been submitted to, your vote counts for shipits. Please review this module and add shipit if you would like to see it merged.

click here for bot help

@Andersson007

This comment has been minimized.

Copy link
Contributor Author

Andersson007 commented Feb 13, 2019

ready_for_review

@Andersson007

This comment has been minimized.

Copy link
Contributor Author

Andersson007 commented Feb 15, 2019

@dagwieers

This comment has been minimized.

Copy link
Member

dagwieers commented Feb 15, 2019

@Andersson007 There is no need to 'cc' the same people again. We do this so these people are subscribed to the issue. As a result they will get every update to this ticket. Doing it again adds no value, except causes more mails to be send for no real value.

@Andersson007

This comment has been minimized.

Copy link
Contributor Author

Andersson007 commented Feb 15, 2019

Ok, I didn't notice amenonsen was here.

kw = dict((params_map[k], v) for (k, v) in iteritems(module.params)
if k in params_map and v != "" and v is not None)

if not HAS_PSYCOPG2:

This comment has been minimized.

@Glandos

Glandos Mar 7, 2019

Contributor

You can use ensure_libs.

This comment has been minimized.

@Andersson007

Andersson007 Mar 7, 2019

Author Contributor

@dagwieers , could you give us your opinion ?

This comment has been minimized.

@Andersson007

Andersson007 Mar 11, 2019

Author Contributor

I need opinions of experienced contributors, @gundalow , maybe you know, is this common way to check libs now?

if is_localhost and module.params["login_unix_socket"] != "":
kw["host"] = module.params["login_unix_socket"]

if psycopg2.__version__ < '2.4.3' and sslrootcert is not None:

This comment has been minimized.

@Glandos

Glandos Mar 7, 2019

Contributor

And remove this afterwards.

This comment has been minimized.

@Andersson007

Andersson007 Mar 13, 2019

Author Contributor

I can't do it without the approval by older brothers. Thank you for the comment

@Andersson007 Andersson007 force-pushed the Andersson007:postgresql_table2 branch from 7066197 to 91becc5 Mar 11, 2019

@Andersson007 Andersson007 reopened this Mar 11, 2019

@Andersson007 Andersson007 force-pushed the Andersson007:postgresql_table2 branch from 91becc5 to c2bbfce Mar 18, 2019

@Andersson007 Andersson007 force-pushed the Andersson007:postgresql_table2 branch from c2bbfce to 90efdc7 Mar 21, 2019

module.fail_json(msg='psycopg2 must be at least 2.4.3 in order to user the ssl_rootcert parameter')

try:
db_connection = psycopg2.connect(**kw)

This comment has been minimized.

@tcraxs

tcraxs Mar 21, 2019

Contributor

In addition to @Glandos comment, I think you can use it like so (or see here):

import ansible.module_utils.postgres as pgutils
...
    try:
        pgutils.ensure_libs(sslrootcert=module.params.get('ssl_rootcert'))
        db_connection = psycopg2.connect(**kw)
...
    except pgutils.LibraryError as e:
         module.fail_json(msg="unable to connect to database: {0}".format(to_native(e)), exception=traceback.format_exc())
    except TypeError as e:
        if 'sslrootcert' in e.args[0]:
            module.fail_json(msg='Postgresql server must be at least version 8.4 to support sslrootcert',
                exception=traceback.format_exc())
...

You could also add exception=traceback.format_exc()to your exceptions like here

This comment has been minimized.

@tcraxs

tcraxs Mar 21, 2019

Contributor

and then remove the parts Glandos point you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.