This function used to drop the database
(connection, database_name)
| 557 | |
| 558 | |
| 559 | def drop_database(connection, database_name): |
| 560 | """This function used to drop the database""" |
| 561 | if database_name not in ["postgres", "template1", "template0"]: |
| 562 | pg_cursor = connection.cursor() |
| 563 | if connection.info.server_version >= 90100: |
| 564 | pg_cursor.execute( |
| 565 | "SELECT pg_terminate_backend(pg_stat_activity.pid) " |
| 566 | "FROM pg_catalog.pg_stat_activity " |
| 567 | "WHERE pg_stat_activity.datname ='%s' AND " |
| 568 | "pid <> pg_backend_pid();" % database_name |
| 569 | ) |
| 570 | else: |
| 571 | pg_cursor.execute( |
| 572 | "SELECT pg_terminate_backend(procpid) " |
| 573 | "FROM pg_catalog.pg_stat_activity " |
| 574 | "WHERE pg_stat_activity.datname ='%s' " |
| 575 | "AND current_query='<IDLE>';" % database_name |
| 576 | ) |
| 577 | pg_cursor.execute("SELECT * FROM pg_catalog.pg_database db WHERE" |
| 578 | " db.datname='%s'" % database_name) |
| 579 | if pg_cursor.fetchall(): |
| 580 | old_isolation_level = connection.isolation_level |
| 581 | set_isolation_level(connection, 0) |
| 582 | if connection.info.server_version >= 130000: |
| 583 | pg_cursor.execute( |
| 584 | '''DROP DATABASE "%s" WITH (FORCE)''' % database_name) |
| 585 | else: |
| 586 | pg_cursor.execute('''DROP DATABASE "%s"''' % database_name) |
| 587 | set_isolation_level(connection, old_isolation_level) |
| 588 | connection.commit() |
| 589 | connection.close() |
| 590 | |
| 591 | |
| 592 | def drop_database_multiple(connection, database_names): |
no test coverage detected