| 54 | | # Create the test database and connect to it. We need autocommit() because |
|---|
| 55 | | # PostgreSQL doesn't allow CREATE DATABASE statements within transactions. |
|---|
| 56 | | cursor = db.cursor() |
|---|
| 57 | | try: |
|---|
| 58 | | db.connection.autocommit() |
|---|
| 59 | | except AttributeError: |
|---|
| 60 | | pass |
|---|
| 61 | | self.output(1, "Creating test database") |
|---|
| 62 | | try: |
|---|
| 63 | | cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME) |
|---|
| 64 | | except: |
|---|
| 65 | | confirm = raw_input("The test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME) |
|---|
| 66 | | if confirm == 'yes': |
|---|
| 67 | | cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) |
|---|
| | 54 | # If we're using SQLite, it's more convient to test against an in-memory database |
|---|
| | 55 | if settings.DATABASE_ENGINE == "sqlite3": |
|---|
| | 56 | global TEST_DATABASE_NAME |
|---|
| | 57 | TEST_DATABASE_NAME = ":memory:" |
|---|
| | 58 | else: |
|---|
| | 59 | # Create the test database and connect to it. We need autocommit() because |
|---|
| | 60 | # PostgreSQL doesn't allow CREATE DATABASE statements within transactions. |
|---|
| | 61 | cursor = db.cursor() |
|---|
| | 62 | try: |
|---|
| | 63 | db.connection.autocommit() |
|---|
| | 64 | except AttributeError: |
|---|
| | 65 | pass |
|---|
| | 66 | self.output(1, "Creating test database") |
|---|
| | 67 | try: |
|---|
| 104 | | # Remove the test database, to clean up after ourselves. Connect to the |
|---|
| 105 | | # previous database (not the test database) to do so, because it's not |
|---|
| 106 | | # allowed to delete a database while being connected to it. |
|---|
| 107 | | db.close() |
|---|
| 108 | | settings.DATABASE_NAME = old_database_name |
|---|
| 109 | | cursor = db.cursor() |
|---|
| 110 | | self.output(1, "Deleting test database") |
|---|
| 111 | | try: |
|---|
| 112 | | db.connection.autocommit() |
|---|
| 113 | | except AttributeError: |
|---|
| 114 | | pass |
|---|
| 115 | | else: |
|---|
| 116 | | time.sleep(1) # To avoid "database is being accessed by other users" errors. |
|---|
| 117 | | cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) |
|---|
| | 109 | # Unless we're using SQLite, remove the test database, to clean up after |
|---|
| | 110 | # ourselves. Connect to the previous database (not the test database) |
|---|
| | 111 | # to do so, because it's not allowed to delete a database while being |
|---|
| | 112 | # connected to it. |
|---|
| | 113 | if settings.DATABASE_ENGINE != "sqlite3": |
|---|
| | 114 | db.close() |
|---|
| | 115 | settings.DATABASE_NAME = old_database_name |
|---|
| | 116 | cursor = db.cursor() |
|---|
| | 117 | self.output(1, "Deleting test database") |
|---|
| | 118 | try: |
|---|
| | 119 | db.connection.autocommit() |
|---|
| | 120 | except AttributeError: |
|---|
| | 121 | pass |
|---|
| | 122 | else: |
|---|
| | 123 | time.sleep(1) # To avoid "database is being accessed by other users" errors. |
|---|
| | 124 | cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) |
|---|