| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Tobias Birkefeld (@tcraxs) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: postgresql_sequence |
| 14 |
short_description: Create, drop, or alter a PostgreSQL sequence |
| 15 |
description: |
| 16 |
- Allows to create, drop or change the definition of a sequence generator. |
| 17 |
options: |
| 18 |
sequence: |
| 19 |
description: |
| 20 |
- The name of the sequence. |
| 21 |
required: true |
| 22 |
type: str |
| 23 |
aliases: |
| 24 |
- name |
| 25 |
state: |
| 26 |
description: |
| 27 |
- The sequence state. |
| 28 |
- If I(state=absent) other options will be ignored except of I(name) and |
| 29 |
I(schema). |
| 30 |
default: present |
| 31 |
choices: [ absent, present ] |
| 32 |
type: str |
| 33 |
data_type: |
| 34 |
description: |
| 35 |
- Specifies the data type of the sequence. Valid types are bigint, integer, |
| 36 |
and smallint. bigint is the default. The data type determines the default |
| 37 |
minimum and maximum values of the sequence. For more info see the |
| 38 |
documentation |
| 39 |
U(https://www.postgresql.org/docs/current/sql-createsequence.html). |
| 40 |
- Supported from PostgreSQL 10. |
| 41 |
choices: [ bigint, integer, smallint ] |
| 42 |
type: str |
| 43 |
increment: |
| 44 |
description: |
| 45 |
- Increment specifies which value is added to the current sequence value |
| 46 |
to create a new value. |
| 47 |
- A positive value will make an ascending sequence, a negative one a |
| 48 |
descending sequence. The default value is 1. |
| 49 |
type: int |
| 50 |
minvalue: |
| 51 |
description: |
| 52 |
- Minvalue determines the minimum value a sequence can generate. The |
| 53 |
default for an ascending sequence is 1. The default for a descending |
| 54 |
sequence is the minimum value of the data type. |
| 55 |
type: int |
| 56 |
aliases: |
| 57 |
- min |
| 58 |
maxvalue: |
| 59 |
description: |
| 60 |
- Maxvalue determines the maximum value for the sequence. The default for |
| 61 |
an ascending sequence is the maximum |
| 62 |
value of the data type. The default for a descending sequence is -1. |
| 63 |
type: int |
| 64 |
aliases: |
| 65 |
- max |
| 66 |
start: |
| 67 |
description: |
| 68 |
- Start allows the sequence to begin anywhere. The default starting value |
| 69 |
is I(minvalue) for ascending sequences and I(maxvalue) for descending |
| 70 |
ones. |
| 71 |
type: int |
| 72 |
cache: |
| 73 |
description: |
| 74 |
- Cache specifies how many sequence numbers are to be preallocated and |
| 75 |
stored in memory for faster access. The minimum value is 1 (only one |
| 76 |
value can be generated at a time, i.e., no cache), and this is also |
| 77 |
the default. |
| 78 |
type: int |
| 79 |
cycle: |
| 80 |
description: |
| 81 |
- The cycle option allows the sequence to wrap around when the I(maxvalue) |
| 82 |
or I(minvalue) has been reached by an ascending or descending sequence |
| 83 |
respectively. If the limit is reached, the next number generated will be |
| 84 |
the minvalue or maxvalue, respectively. |
| 85 |
- If C(false) (NO CYCLE) is specified, any calls to nextval after the sequence |
| 86 |
has reached its maximum value will return an error. False (NO CYCLE) is |
| 87 |
the default. |
| 88 |
type: bool |
| 89 |
default: false |
| 90 |
cascade: |
| 91 |
description: |
| 92 |
- Automatically drop objects that depend on the sequence, and in turn all |
| 93 |
objects that depend on those objects. |
| 94 |
- Ignored if I(state=present). |
| 95 |
- Only used with I(state=absent). |
| 96 |
type: bool |
| 97 |
default: false |
| 98 |
rename_to: |
| 99 |
description: |
| 100 |
- DEPRECATED (see the L(discussion,https://github.com/ansible-collections/community.postgresql/issues/820)). |
| 101 |
This option will be removed in version 5.0.0. |
| 102 |
To rename a sequence, use the M(community.postgresql.postgresql_query) module. |
| 103 |
- The new name for the I(sequence). |
| 104 |
- Works only for existing sequences. |
| 105 |
type: str |
| 106 |
owner: |
| 107 |
description: |
| 108 |
- Set the owner for the I(sequence). |
| 109 |
type: str |
| 110 |
schema: |
| 111 |
description: |
| 112 |
- The schema of the I(sequence). This is be used to create and relocate |
| 113 |
a I(sequence) in the given schema. |
| 114 |
default: public |
| 115 |
type: str |
| 116 |
newschema: |
| 117 |
description: |
| 118 |
- The new schema for the I(sequence). Will be used for moving a |
| 119 |
I(sequence) to another I(schema). |
| 120 |
- Works only for existing sequences. |
| 121 |
type: str |
| 122 |
session_role: |
| 123 |
description: |
| 124 |
- Switch to session_role after connecting. The specified I(session_role) |
| 125 |
must be a role that the current I(login_user) is a member of. |
| 126 |
- Permissions checking for SQL commands is carried out as though |
| 127 |
the I(session_role) were the one that had logged in originally. |
| 128 |
type: str |
| 129 |
login_db: |
| 130 |
description: |
| 131 |
- Name of database to connect to and run queries against. |
| 132 |
- The V(db) and V(database) aliases are deprecated and will be removed in version 5.0.0. |
| 133 |
type: str |
| 134 |
default: '' |
| 135 |
aliases: |
| 136 |
- database |
| 137 |
- db |
| 138 |
trust_input: |
| 139 |
description: |
| 140 |
- If C(false), check whether values of parameters I(sequence), I(schema), I(rename_to), |
| 141 |
I(owner), I(newschema), I(session_role) are potentially dangerous. |
| 142 |
- It makes sense to use C(false) only when SQL injections via the parameters are possible. |
| 143 |
type: bool |
| 144 |
default: true |
| 145 |
version_added: '0.2.0' |
| 146 |
|
| 147 |
notes: |
| 148 |
- If you do not pass db parameter, sequence will be created in the database |
| 149 |
named postgres. |
| 150 |
|
| 151 |
attributes: |
| 152 |
check_mode: |
| 153 |
support: full |
| 154 |
|
| 155 |
seealso: |
| 156 |
- module: community.postgresql.postgresql_table |
| 157 |
- module: community.postgresql.postgresql_owner |
| 158 |
- module: community.postgresql.postgresql_privs |
| 159 |
- module: community.postgresql.postgresql_tablespace |
| 160 |
- name: CREATE SEQUENCE reference |
| 161 |
description: Complete reference of the CREATE SEQUENCE command documentation. |
| 162 |
link: https://www.postgresql.org/docs/current/sql-createsequence.html |
| 163 |
- name: ALTER SEQUENCE reference |
| 164 |
description: Complete reference of the ALTER SEQUENCE command documentation. |
| 165 |
link: https://www.postgresql.org/docs/current/sql-altersequence.html |
| 166 |
- name: DROP SEQUENCE reference |
| 167 |
description: Complete reference of the DROP SEQUENCE command documentation. |
| 168 |
link: https://www.postgresql.org/docs/current/sql-dropsequence.html |
| 169 |
author: |
| 170 |
- Tobias Birkefeld (@tcraxs) |
| 171 |
- Thomas O'Donnell (@andytom) |
| 172 |
extends_documentation_fragment: |
| 173 |
- community.postgresql.postgres |
| 174 |
|
| 175 |
''' |
| 176 |
|
| 177 |
EXAMPLES = r''' |
| 178 |
- name: Create an ascending bigint sequence called foobar in the default |
| 179 |
database |
| 180 |
community.postgresql.postgresql_sequence: |
| 181 |
name: foobar |
| 182 |
|
| 183 |
- name: Create an ascending integer sequence called foobar, starting at 101 |
| 184 |
community.postgresql.postgresql_sequence: |
| 185 |
name: foobar |
| 186 |
data_type: integer |
| 187 |
start: 101 |
| 188 |
|
| 189 |
- name: Create an descending sequence called foobar, starting at 101 and |
| 190 |
preallocated 10 sequence numbers in cache |
| 191 |
community.postgresql.postgresql_sequence: |
| 192 |
name: foobar |
| 193 |
increment: -1 |
| 194 |
cache: 10 |
| 195 |
start: 101 |
| 196 |
|
| 197 |
- name: Create an ascending sequence called foobar, which cycle between 1 to 10 |
| 198 |
community.postgresql.postgresql_sequence: |
| 199 |
name: foobar |
| 200 |
cycle: true |
| 201 |
min: 1 |
| 202 |
max: 10 |
| 203 |
|
| 204 |
- name: Create an ascending bigint sequence called foobar in the default |
| 205 |
database with owner foobar |
| 206 |
community.postgresql.postgresql_sequence: |
| 207 |
name: foobar |
| 208 |
owner: foobar |
| 209 |
|
| 210 |
- name: Change the schema of an existing sequence to foobar |
| 211 |
community.postgresql.postgresql_sequence: |
| 212 |
name: foobar |
| 213 |
newschema: foobar |
| 214 |
|
| 215 |
- name: Change the owner of an existing sequence to foobar |
| 216 |
community.postgresql.postgresql_sequence: |
| 217 |
name: foobar |
| 218 |
owner: foobar |
| 219 |
|
| 220 |
- name: Drop a sequence called foobar |
| 221 |
community.postgresql.postgresql_sequence: |
| 222 |
name: foobar |
| 223 |
state: absent |
| 224 |
|
| 225 |
- name: Drop a sequence called foobar with cascade |
| 226 |
community.postgresql.postgresql_sequence: |
| 227 |
name: foobar |
| 228 |
cascade: true |
| 229 |
state: absent |
| 230 |
''' |
| 231 |
|
| 232 |
RETURN = r''' |
| 233 |
state: |
| 234 |
description: Sequence state at the end of execution. |
| 235 |
returned: success |
| 236 |
type: str |
| 237 |
sample: 'present' |
| 238 |
sequence: |
| 239 |
description: Sequence name. |
| 240 |
returned: success |
| 241 |
type: str |
| 242 |
sample: 'foobar' |
| 243 |
queries: |
| 244 |
description: List of queries that was tried to be executed. |
| 245 |
returned: success |
| 246 |
type: str |
| 247 |
sample: [ "CREATE SEQUENCE \"foo\"" ] |
| 248 |
schema: |
| 249 |
description: Name of the schema of the sequence. |
| 250 |
returned: success |
| 251 |
type: str |
| 252 |
sample: 'foo' |
| 253 |
data_type: |
| 254 |
description: Shows the current data type of the sequence. |
| 255 |
returned: success |
| 256 |
type: str |
| 257 |
sample: 'bigint' |
| 258 |
increment: |
| 259 |
description: The value of increment of the sequence. A positive value will |
| 260 |
make an ascending sequence, a negative one a descending |
| 261 |
sequence. |
| 262 |
returned: success |
| 263 |
type: int |
| 264 |
sample: -1 |
| 265 |
minvalue: |
| 266 |
description: The value of minvalue of the sequence. |
| 267 |
returned: success |
| 268 |
type: int |
| 269 |
sample: 1 |
| 270 |
maxvalue: |
| 271 |
description: The value of maxvalue of the sequence. |
| 272 |
returned: success |
| 273 |
type: int |
| 274 |
sample: 9223372036854775807 |
| 275 |
start: |
| 276 |
description: The value of start of the sequence. |
| 277 |
returned: success |
| 278 |
type: int |
| 279 |
sample: 12 |
| 280 |
cycle: |
| 281 |
description: Shows if the sequence cycle or not. |
| 282 |
returned: success |
| 283 |
type: bool |
| 284 |
sample: false |
| 285 |
owner: |
| 286 |
description: Shows the current owner of the sequence |
| 287 |
after the successful run of the task. |
| 288 |
returned: success |
| 289 |
type: str |
| 290 |
sample: 'postgres' |
| 291 |
newname: |
| 292 |
description: Shows the new sequence name after rename. |
| 293 |
returned: success |
| 294 |
type: str |
| 295 |
sample: 'barfoo' |
| 296 |
newschema: |
| 297 |
description: Shows the new schema of the sequence after schema change. |
| 298 |
returned: success |
| 299 |
type: str |
| 300 |
sample: 'foobar' |
| 301 |
''' |
| 302 |
|
| 303 |
|
| 304 |
from ansible.module_utils.basic import AnsibleModule |
| 305 |
from ansible_collections.community.postgresql.plugins.module_utils.database import \ |
| 306 |
check_input |
| 307 |
from ansible_collections.community.postgresql.plugins.module_utils.postgres import ( |
| 308 |
connect_to_db, |
| 309 |
ensure_required_libs, |
| 310 |
exec_sql, |
| 311 |
get_conn_params, |
| 312 |
pg_cursor_args, |
| 313 |
postgres_common_argument_spec, |
| 314 |
) |
| 315 |
|
| 316 |
|
| 317 |
class Sequence(object): |
| 318 |
"""Implements behavior of CREATE, ALTER or DROP SEQUENCE PostgreSQL command. |
| 319 |
|
| 320 |
Arguments: |
| 321 |
module (AnsibleModule) -- object of AnsibleModule class |
| 322 |
cursor (cursor) -- cursor object of psycopg library |
| 323 |
|
| 324 |
Attributes: |
| 325 |
module (AnsibleModule) -- object of AnsibleModule class |
| 326 |
cursor (cursor) -- cursor object of psycopg library |
| 327 |
changed (bool) -- something was changed after execution or not |
| 328 |
executed_queries (list) -- executed queries |
| 329 |
name (str) -- name of the sequence |
| 330 |
owner (str) -- name of the owner of the sequence |
| 331 |
schema (str) -- name of the schema (default: public) |
| 332 |
data_type (str) -- data type of the sequence |
| 333 |
start_value (int) -- value of the sequence start |
| 334 |
minvalue (int) -- minimum value of the sequence |
| 335 |
maxvalue (int) -- maximum value of the sequence |
| 336 |
increment (int) -- increment value of the sequence |
| 337 |
cycle (bool) -- sequence can cycle or not |
| 338 |
new_name (str) -- name of the renamed sequence |
| 339 |
new_schema (str) -- name of the new schema |
| 340 |
exists (bool) -- sequence exists or not |
| 341 |
""" |
| 342 |
|
| 343 |
def __init__(self, module, cursor): |
| 344 |
self.module = module |
| 345 |
self.cursor = cursor |
| 346 |
self.executed_queries = [] |
| 347 |
self.name = self.module.params['sequence'] |
| 348 |
self.owner = '' |
| 349 |
self.schema = self.module.params['schema'] |
| 350 |
self.data_type = '' |
| 351 |
self.start_value = '' |
| 352 |
self.minvalue = '' |
| 353 |
self.maxvalue = '' |
| 354 |
self.increment = '' |
| 355 |
self.cycle = '' |
| 356 |
self.new_name = '' |
| 357 |
self.new_schema = '' |
| 358 |
self.exists = False |
| 359 |
# Collect info |
| 360 |
self.get_info() |
| 361 |
|
| 362 |
def get_info(self): |
| 363 |
"""Getter to refresh and get sequence info""" |
| 364 |
query = ("SELECT " |
| 365 |
"s.sequence_schema AS schemaname, " |
| 366 |
"s.sequence_name AS sequencename, " |
| 367 |
"pg_get_userbyid(c.relowner) AS sequenceowner, " |
| 368 |
"s.data_type::regtype AS data_type, " |
| 369 |
"s.start_value AS start_value, " |
| 370 |
"s.minimum_value AS min_value, " |
| 371 |
"s.maximum_value AS max_value, " |
| 372 |
"s.increment AS increment_by, " |
| 373 |
"s.cycle_option AS cycle " |
| 374 |
"FROM information_schema.sequences s " |
| 375 |
"JOIN pg_class c ON c.relname = s.sequence_name " |
| 376 |
"LEFT JOIN pg_namespace n ON n.oid = c.relnamespace " |
| 377 |
"WHERE NOT pg_is_other_temp_schema(n.oid) " |
| 378 |
"AND c.relkind = 'S'::\"char\" " |
| 379 |
"AND sequence_name = %(name)s " |
| 380 |
"AND sequence_schema = %(schema)s") |
| 381 |
|
| 382 |
res = exec_sql(self, query, |
| 383 |
query_params={'name': self.name, 'schema': self.schema}, |
| 384 |
add_to_executed=False) |
| 385 |
|
| 386 |
if not res: |
| 387 |
self.exists = False |
| 388 |
return False |
| 389 |
|
| 390 |
if res: |
| 391 |
self.exists = True |
| 392 |
self.schema = res[0]['schemaname'] |
| 393 |
self.name = res[0]['sequencename'] |
| 394 |
self.owner = res[0]['sequenceowner'] |
| 395 |
self.data_type = res[0]['data_type'] |
| 396 |
self.start_value = res[0]['start_value'] |
| 397 |
self.minvalue = res[0]['min_value'] |
| 398 |
self.maxvalue = res[0]['max_value'] |
| 399 |
self.increment = res[0]['increment_by'] |
| 400 |
self.cycle = res[0]['cycle'] |
| 401 |
|
| 402 |
def create(self): |
| 403 |
"""Implements CREATE SEQUENCE command behavior.""" |
| 404 |
query = ['CREATE SEQUENCE'] |
| 405 |
query.append(self.__add_schema()) |
| 406 |
|
| 407 |
if self.module.params.get('data_type'): |
| 408 |
query.append('AS %s' % self.module.params['data_type']) |
| 409 |
|
| 410 |
if self.module.params.get('increment'): |
| 411 |
query.append('INCREMENT BY %s' % self.module.params['increment']) |
| 412 |
|
| 413 |
if self.module.params.get('minvalue'): |
| 414 |
query.append('MINVALUE %s' % self.module.params['minvalue']) |
| 415 |
|
| 416 |
if self.module.params.get('maxvalue'): |
| 417 |
query.append('MAXVALUE %s' % self.module.params['maxvalue']) |
| 418 |
|
| 419 |
if self.module.params.get('start'): |
| 420 |
query.append('START WITH %s' % self.module.params['start']) |
| 421 |
|
| 422 |
if self.module.params.get('cache'): |
| 423 |
query.append('CACHE %s' % self.module.params['cache']) |
| 424 |
|
| 425 |
if self.module.params.get('cycle'): |
| 426 |
query.append('CYCLE') |
| 427 |
|
| 428 |
return exec_sql(self, ' '.join(query), return_bool=True) |
| 429 |
|
| 430 |
def drop(self): |
| 431 |
"""Implements DROP SEQUENCE command behavior.""" |
| 432 |
query = ['DROP SEQUENCE'] |
| 433 |
query.append(self.__add_schema()) |
| 434 |
|
| 435 |
if self.module.params.get('cascade'): |
| 436 |
query.append('CASCADE') |
| 437 |
|
| 438 |
return exec_sql(self, ' '.join(query), return_bool=True) |
| 439 |
|
| 440 |
def rename(self): |
| 441 |
"""Implements ALTER SEQUENCE RENAME TO command behavior.""" |
| 442 |
query = ['ALTER SEQUENCE'] |
| 443 |
query.append(self.__add_schema()) |
| 444 |
query.append('RENAME TO "%s"' % self.module.params['rename_to']) |
| 445 |
|
| 446 |
return exec_sql(self, ' '.join(query), return_bool=True) |
| 447 |
|
| 448 |
def set_owner(self): |
| 449 |
"""Implements ALTER SEQUENCE OWNER TO command behavior.""" |
| 450 |
query = ['ALTER SEQUENCE'] |
| 451 |
query.append(self.__add_schema()) |
| 452 |
query.append('OWNER TO "%s"' % self.module.params['owner']) |
| 453 |
|
| 454 |
return exec_sql(self, ' '.join(query), return_bool=True) |
| 455 |
|
| 456 |
def set_schema(self): |
| 457 |
"""Implements ALTER SEQUENCE SET SCHEMA command behavior.""" |
| 458 |
query = ['ALTER SEQUENCE'] |
| 459 |
query.append(self.__add_schema()) |
| 460 |
query.append('SET SCHEMA "%s"' % self.module.params['newschema']) |
| 461 |
|
| 462 |
return exec_sql(self, ' '.join(query), return_bool=True) |
| 463 |
|
| 464 |
def __add_schema(self): |
| 465 |
return '"%s"."%s"' % (self.schema, self.name) |
| 466 |
|
| 467 |
|
| 468 |
# =========================================== |
| 469 |
# Module execution. |
| 470 |
# |
| 471 |
|
| 472 |
def main(): |
| 473 |
argument_spec = postgres_common_argument_spec() |
| 474 |
argument_spec.update( |
| 475 |
sequence=dict(type='str', required=True, aliases=['name']), |
| 476 |
state=dict(type='str', default='present', choices=['absent', 'present']), |
| 477 |
data_type=dict(type='str', choices=['bigint', 'integer', 'smallint']), |
| 478 |
increment=dict(type='int'), |
| 479 |
minvalue=dict(type='int', aliases=['min']), |
| 480 |
maxvalue=dict(type='int', aliases=['max']), |
| 481 |
start=dict(type='int'), |
| 482 |
cache=dict(type='int'), |
| 483 |
cycle=dict(type='bool', default=False), |
| 484 |
schema=dict(type='str', default='public'), |
| 485 |
cascade=dict(type='bool', default=False), |
| 486 |
rename_to=dict(type='str', removed_in_version='5.0.0', |
| 487 |
removed_from_collection='community.postgresql'), |
| 488 |
owner=dict(type='str'), |
| 489 |
newschema=dict(type='str'), |
| 490 |
login_db=dict(type='str', default='', aliases=['db', 'database'], deprecated_aliases=[ |
| 491 |
{ |
| 492 |
'name': 'db', |
| 493 |
'version': '5.0.0', |
| 494 |
'collection_name': 'community.postgresql', |
| 495 |
}, |
| 496 |
{ |
| 497 |
'name': 'database', |
| 498 |
'version': '5.0.0', |
| 499 |
'collection_name': 'community.postgresql', |
| 500 |
}], |
| 501 |
), |
| 502 |
session_role=dict(type='str'), |
| 503 |
trust_input=dict(type="bool", default=True), |
| 504 |
) |
| 505 |
module = AnsibleModule( |
| 506 |
argument_spec=argument_spec, |
| 507 |
supports_check_mode=True, |
| 508 |
mutually_exclusive=[ |
| 509 |
['rename_to', 'data_type'], |
| 510 |
['rename_to', 'increment'], |
| 511 |
['rename_to', 'minvalue'], |
| 512 |
['rename_to', 'maxvalue'], |
| 513 |
['rename_to', 'start'], |
| 514 |
['rename_to', 'cache'], |
| 515 |
['rename_to', 'cycle'], |
| 516 |
['rename_to', 'cascade'], |
| 517 |
['rename_to', 'owner'], |
| 518 |
['rename_to', 'newschema'], |
| 519 |
['cascade', 'data_type'], |
| 520 |
['cascade', 'increment'], |
| 521 |
['cascade', 'minvalue'], |
| 522 |
['cascade', 'maxvalue'], |
| 523 |
['cascade', 'start'], |
| 524 |
['cascade', 'cache'], |
| 525 |
['cascade', 'cycle'], |
| 526 |
['cascade', 'owner'], |
| 527 |
['cascade', 'newschema'], |
| 528 |
] |
| 529 |
) |
| 530 |
|
| 531 |
if not module.params["trust_input"]: |
| 532 |
check_input( |
| 533 |
module, |
| 534 |
module.params['sequence'], |
| 535 |
module.params['schema'], |
| 536 |
module.params['rename_to'], |
| 537 |
module.params['owner'], |
| 538 |
module.params['newschema'], |
| 539 |
module.params['session_role'], |
| 540 |
) |
| 541 |
|
| 542 |
# Note: we don't need to check mutually exclusive params here, because they are |
| 543 |
# checked automatically by AnsibleModule (mutually_exclusive=[] list above). |
| 544 |
|
| 545 |
# Change autocommit to False if check_mode: |
| 546 |
autocommit = not module.check_mode |
| 547 |
# Ensure psycopg libraries are available before connecting to DB: |
| 548 |
ensure_required_libs(module) |
| 549 |
# Connect to DB and make cursor object: |
| 550 |
conn_params = get_conn_params(module, module.params) |
| 551 |
db_connection, dummy = connect_to_db(module, conn_params, autocommit=autocommit) |
| 552 |
cursor = db_connection.cursor(**pg_cursor_args) |
| 553 |
|
| 554 |
############## |
| 555 |
# Create the object and do main job: |
| 556 |
data = Sequence(module, cursor) |
| 557 |
|
| 558 |
# Set defaults: |
| 559 |
changed = False |
| 560 |
|
| 561 |
# Create new sequence |
| 562 |
if not data.exists and module.params['state'] == 'present': |
| 563 |
if module.params.get('rename_to'): |
| 564 |
module.fail_json(msg="Sequence '%s' does not exist, nothing to rename" % module.params['sequence']) |
| 565 |
if module.params.get('newschema'): |
| 566 |
module.fail_json(msg="Sequence '%s' does not exist, change of schema not possible" % module.params['sequence']) |
| 567 |
|
| 568 |
changed = data.create() |
| 569 |
|
| 570 |
# Drop non-existing sequence |
| 571 |
elif not data.exists and module.params['state'] == 'absent': |
| 572 |
# Nothing to do |
| 573 |
changed = False |
| 574 |
|
| 575 |
# Drop existing sequence |
| 576 |
elif data.exists and module.params['state'] == 'absent': |
| 577 |
changed = data.drop() |
| 578 |
|
| 579 |
# Rename sequence |
| 580 |
if data.exists and module.params.get('rename_to'): |
| 581 |
if data.name != module.params['rename_to']: |
| 582 |
changed = data.rename() |
| 583 |
if changed: |
| 584 |
data.new_name = module.params['rename_to'] |
| 585 |
|
| 586 |
# Refresh information |
| 587 |
if module.params['state'] == 'present': |
| 588 |
data.get_info() |
| 589 |
|
| 590 |
# Change owner, schema and settings |
| 591 |
if module.params['state'] == 'present' and data.exists: |
| 592 |
# change owner |
| 593 |
if module.params.get('owner'): |
| 594 |
if data.owner != module.params['owner']: |
| 595 |
changed = data.set_owner() |
| 596 |
|
| 597 |
# Set schema |
| 598 |
if module.params.get('newschema'): |
| 599 |
if data.schema != module.params['newschema']: |
| 600 |
changed = data.set_schema() |
| 601 |
if changed: |
| 602 |
data.new_schema = module.params['newschema'] |
| 603 |
|
| 604 |
# Rollback if it's possible and check_mode: |
| 605 |
if module.check_mode: |
| 606 |
db_connection.rollback() |
| 607 |
else: |
| 608 |
db_connection.commit() |
| 609 |
|
| 610 |
cursor.close() |
| 611 |
db_connection.close() |
| 612 |
|
| 613 |
# Make return values: |
| 614 |
kw = dict( |
| 615 |
changed=changed, |
| 616 |
state='present', |
| 617 |
sequence=data.name, |
| 618 |
queries=data.executed_queries, |
| 619 |
schema=data.schema, |
| 620 |
data_type=data.data_type, |
| 621 |
increment=data.increment, |
| 622 |
minvalue=data.minvalue, |
| 623 |
maxvalue=data.maxvalue, |
| 624 |
start=data.start_value, |
| 625 |
cycle=data.cycle, |
| 626 |
owner=data.owner, |
| 627 |
) |
| 628 |
|
| 629 |
if module.params['state'] == 'present': |
| 630 |
if data.new_name: |
| 631 |
kw['newname'] = data.new_name |
| 632 |
if data.new_schema: |
| 633 |
kw['newschema'] = data.new_schema |
| 634 |
|
| 635 |
elif module.params['state'] == 'absent': |
| 636 |
kw['state'] = 'absent' |
| 637 |
|
| 638 |
module.exit_json(**kw) |
| 639 |
|
| 640 |
|
| 641 |
if __name__ == '__main__': |
| 642 |
main() |