| 220 | |
| 221 | # First pass -- work out which databases actually need to be created, |
| 222 | # and which ones are test mirrors or duplicate entries in DATABASES |
| 223 | mirrored_aliases = {} |
| 224 | test_databases = {} |
| 225 | for alias in connections: |
| 226 | connection = connections[alias] |
| 227 | if connection.settings_dict['TEST_MIRROR']: |
| 228 | # If the database is marked as a test mirror, save |
| 229 | # the alias. |
| 230 | mirrored_aliases[alias] = connection.settings_dict['TEST_MIRROR'] |
| 231 | else: |
| 232 | # Store the (engine, name) pair. If we have two aliases |
| 233 | # with the same pair, we only need to create the test database |
| 234 | # once. |
| 235 | test_databases.setdefault(( |
| 236 | connection.settings_dict['HOST'], |
| 237 | connection.settings_dict['ENGINE'], |
| 238 | connection.settings_dict['NAME'], |
| 239 | ), []).append(alias) |
| 240 | |
| 241 | # Second pass -- actually create the databases. |
222 | | for alias in connections: |
223 | | connection = connections[alias] |
224 | | # If the database is a test mirror, redirect it's connection |
225 | | # instead of creating a test database. |
226 | | if connection.settings_dict['TEST_MIRROR']: |
227 | | mirrors.append((alias, connection)) |
228 | | mirror_alias = connection.settings_dict['TEST_MIRROR'] |
229 | | connections._connections[alias] = connections[mirror_alias] |
230 | | else: |
231 | | old_names.append((connection, connection.settings_dict['NAME'])) |
232 | | connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive) |
| 244 | for (host, engine, db_name), aliases in test_databases.items(): |
| 245 | # Actually create the database for the first connection |
| 246 | connection = connections[aliases[0]] |
| 247 | old_names.append((connection, db_name, True)) |
| 248 | test_db_name = connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive) |
| 249 | for alias in aliases[1:]: |
| 250 | connection = connections[alias] |
| 251 | if db_name: |
| 252 | old_names.append((connection, db_name, False)) |
| 253 | connection.settings_dict['NAME'] = test_db_name |
| 254 | else: |
| 255 | # If settings_dict['NAME'] isn't defined, we have a backend where |
| 256 | # the name isn't important -- e.g., SQLite, which uses :memory:. |
| 257 | # Force create the database instead of assuming it's a duplicate. |
| 258 | old_names.append((connection, db_name, True)) |
| 259 | connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive) |
| 260 | |
| 261 | for alias, mirror_alias in mirrored_aliases.items(): |
| 262 | mirrors.append((alias, connections[alias].settings_dict['NAME'])) |
| 263 | connections[alias].settings_dict['NAME'] = connections[mirror_alias].settings_dict['NAME'] |
| 264 | |