Ticket #3174: patch-boulder-oracle-sprint.diff
File patch-boulder-oracle-sprint.diff, 11.3 KB (added by , 18 years ago) |
---|
-
django_src/django/db/backends/oracle/creation.py
35 35 PASSWORD = 'Im_a_lumberjack' 36 36 OLD_DATABASE_USER = None 37 37 OLD_DATABASE_PASSWORD = None 38 REMEMBER = {} 38 39 39 40 def create_test_db(settings, connection, backend, verbosity=1, autoclobber=False): 40 if verbosity >= 1:41 print "Creating test database..."42 41 43 42 TEST_DATABASE_NAME = _test_database_name(settings) 43 TEST_DATABASE_USER = _test_database_user(settings) 44 TEST_DATABASE_PASSWD = _test_database_passwd(settings) 45 TEST_DATABASE_TBLSPACE = _test_database_tblspace(settings) 46 TEST_DATABASE_TBLSPACE_TMP = _test_database_tblspace_tmp(settings) 44 47 48 parameters = { 49 'dbname': TEST_DATABASE_NAME, 50 'user': TEST_DATABASE_USER, 51 'password': TEST_DATABASE_PASSWD, 52 'tblspace': TEST_DATABASE_TBLSPACE, 53 'tblspace_temp': TEST_DATABASE_TBLSPACE_TMP, 54 } 55 56 REMEMBER['user'] = settings.DATABASE_USER 57 REMEMBER['passwd'] = settings.DATABASE_PASSWORD 58 45 59 cursor = connection.cursor() 46 try:47 _create_test_db(cursor, TEST_DATABASE_NAME, verbosity)48 except Exception, e:49 sys.stderr.write("Got an error creating the test database: %s\n" % e)50 if not autoclobber:51 confirm = raw_input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME)52 if autoclobber or confirm == 'yes':53 try:54 if verbosity >= 1:55 print "Destroying old test database..."56 _destroy_test_db(cursor, TEST_DATABASE_NAME, verbosity)57 if verbosity >= 1:58 print "Creating test database..."59 _create_test_db(cursor, TEST_DATABASE_NAME, verbosity)60 except Exception, e:61 sys.stderr.write("Got an error recreating the test database: %s\n" % e)62 sys.exit(2)63 else:64 print "Tests cancelled."65 sys.exit(1)66 60 61 if _test_database_create(settings): 62 if verbosity >= 1: 63 print "Creating test database..." 64 try: 65 _create_test_db(cursor, parameters, verbosity) 66 except Exception, e: 67 sys.stderr.write("Got an error creating the test database: %s\n" % e) 68 if not autoclobber: 69 confirm = raw_input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME) 70 if autoclobber or confirm == 'yes': 71 try: 72 if verbosity >= 1: 73 print "Destroying old test database..." 74 _destroy_test_db(cursor, parameters, verbosity) 75 if verbosity >= 1: 76 print "Creating test database..." 77 _create_test_db(cursor, parameters, verbosity) 78 except Exception, e: 79 sys.stderr.write("Got an error recreating the test database: %s\n" % e) 80 sys.exit(2) 81 else: 82 print "Tests cancelled." 83 sys.exit(1) 84 85 if _test_user_create(settings): 86 if verbosity >= 1: 87 print "Creating test user..." 88 try: 89 _create_test_user(cursor, parameters, verbosity) 90 except Exception, e: 91 sys.stderr.write("Got an error creating the test user: %s\n" % e) 92 if not autoclobber: 93 confirm = raw_input("It appears the test user, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_USER) 94 if autoclobber or confirm == 'yes': 95 try: 96 if verbosity >= 1: 97 print "Destroying old test user..." 98 _destroy_test_user(cursor, parameters, verbosity) 99 if verbosity >= 1: 100 print "Creating test user..." 101 _create_test_user(cursor, parameters, verbosity) 102 except Exception, e: 103 sys.stderr.write("Got an error recreating the test user: %s\n" % e) 104 sys.exit(2) 105 else: 106 print "Tests cancelled." 107 sys.exit(1) 108 67 109 connection.close() 68 settings.DATABASE_USER = TEST_DATABASE_ NAME69 settings.DATABASE_PASSWORD = PASSWORD110 settings.DATABASE_USER = TEST_DATABASE_USER 111 settings.DATABASE_PASSWORD = TEST_DATABASE_PASSWD 70 112 71 113 # Get a cursor (even though we don't need one yet). This has 72 114 # the side effect of initializing the test database. 73 115 cursor = connection.cursor() 74 116 75 117 def destroy_test_db(settings, connection, backend, old_database_name, verbosity=1): 76 if verbosity >= 1:77 print "Destroying test database..."78 118 connection.close() 79 119 80 120 TEST_DATABASE_NAME = _test_database_name(settings) 121 TEST_DATABASE_USER = _test_database_user(settings) 122 TEST_DATABASE_PASSWD = _test_database_passwd(settings) 123 TEST_DATABASE_TBLSPACE = _test_database_tblspace(settings) 124 TEST_DATABASE_TBLSPACE_TMP = _test_database_tblspace_tmp(settings) 81 125 settings.DATABASE_NAME = old_database_name 82 126 #settings.DATABASE_USER = 'old_user' 83 127 #settings.DATABASE_PASSWORD = 'old_password' 128 settings.DATABASE_USER = REMEMBER['user'] 129 settings.DATABASE_PASSWORD = REMEMBER['passwd'] 84 130 131 parameters = { 132 'dbname': TEST_DATABASE_NAME, 133 'user': TEST_DATABASE_USER, 134 'password': TEST_DATABASE_PASSWD, 135 'tblspace': TEST_DATABASE_TBLSPACE, 136 'tblspace_temp': TEST_DATABASE_TBLSPACE_TMP, 137 } 138 85 139 cursor = connection.cursor() 86 140 time.sleep(1) # To avoid "database is being accessed by other users" errors. 87 _destroy_test_db(cursor, TEST_DATABASE_NAME, verbosity) 141 if _test_user_create(settings): 142 if verbosity >= 1: 143 print "Destroying test user..." 144 _destroy_test_user(cursor, parameters, verbosity) 145 if _test_database_create(settings): 146 if verbosity >= 1: 147 print "Destroying test database..." 148 _destroy_test_db(cursor, parameters, verbosity) 88 149 connection.close() 89 150 90 def _create_test_db(cursor, dbname, verbosity):151 def _create_test_db(cursor, parameters, verbosity): 91 152 if verbosity >= 2: 92 print "_create_test_db(): dbname = %s" % dbname153 print "_create_test_db(): dbname = %s" % parameters['dbname'] 93 154 statements = [ 94 """CREATE TABLESPACE %( user)s95 DATAFILE '%( user)s.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M155 """CREATE TABLESPACE %(tblspace)s 156 DATAFILE '%(tblspace)s.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M 96 157 """, 97 """CREATE TEMPORARY TABLESPACE %( user)s_temp98 TEMPFILE '%( user)s_temp.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M158 """CREATE TEMPORARY TABLESPACE %(tblspace_temp)s 159 TEMPFILE '%(tblspace_temp)s.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M 99 160 """, 161 ] 162 _execute_statements(cursor, statements, parameters, verbosity) 163 164 def _create_test_user(cursor, parameters, verbosity): 165 if verbosity >= 2: 166 print "_create_test_user(): username = %s" % parameters['user'] 167 statements = [ 100 168 """CREATE USER %(user)s 101 169 IDENTIFIED BY %(password)s 102 DEFAULT TABLESPACE %( user)s103 TEMPORARY TABLESPACE %( user)s_temp170 DEFAULT TABLESPACE %(tblspace)s 171 TEMPORARY TABLESPACE %(tblspace_temp)s 104 172 """, 105 173 """GRANT CONNECT, RESOURCE TO %(user)s""", 106 174 ] 107 _execute_statements(cursor, statements, dbname, verbosity)175 _execute_statements(cursor, statements, parameters, verbosity) 108 176 109 def _destroy_test_db(cursor, dbname, verbosity):177 def _destroy_test_db(cursor, parameters, verbosity): 110 178 if verbosity >= 2: 111 print "_destroy_test_db(): dbname=%s" % dbname179 print "_destroy_test_db(): dbname=%s" % parameters['dbname'] 112 180 statements = [ 181 'DROP TABLESPACE %(tblspace)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS', 182 'DROP TABLESPACE %(tblspace_temp)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS', 183 ] 184 _execute_statements(cursor, statements, parameters, verbosity) 185 186 def _destroy_test_user(cursor, parameters, verbosity): 187 if verbosity >= 2: 188 print "_destroy_test_user(): user=%s" % parameters['user'] 189 print "Be patient: This can take some time..." 190 statements = [ 113 191 'DROP USER %(user)s CASCADE', 114 'DROP TABLESPACE %(user)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',115 'DROP TABLESPACE %(user)s_TEMP INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',116 192 ] 117 _execute_statements(cursor, statements, dbname, verbosity)193 _execute_statements(cursor, statements, parameters, verbosity) 118 194 119 def _execute_statements(cursor, statements, dbname, verbosity):195 def _execute_statements(cursor, statements, parameters, verbosity): 120 196 for template in statements: 121 stmt = template % {'user': dbname, 122 'password': PASSWORD} 197 stmt = template % parameters 123 198 if verbosity >= 2: 124 199 print stmt 125 200 try: … … 129 204 raise 130 205 131 206 def _test_database_name(settings): 132 if settings.TEST_DATABASE_NAME: 133 name = settings.TEST_DATABASE_NAME 134 else: 135 name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME 207 name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME 208 try: 209 if settings.TEST_DATABASE_NAME: 210 name = settings.TEST_DATABASE_NAME 211 except AttributeError: 212 pass 213 except: 214 raise 136 215 return name 216 217 def _test_database_create(settings): 218 name = True 219 try: 220 if settings.TEST_DATABASE_CREATE 221 name = True 222 else: 223 name = False 224 except AttributeError: 225 pass 226 except: 227 raise 228 return name 229 230 def _test_user_create(settings): 231 name = True 232 try: 233 if settings.TEST_USER_CREATE 234 name = True 235 else: 236 name = False 237 except AttributeError: 238 pass 239 except: 240 raise 241 return name 242 243 def _test_database_user(settings): 244 name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME 245 try: 246 if settings.TEST_DATABASE_USER: 247 name = settings.TEST_DATABASE_USER 248 except AttributeError: 249 pass 250 except: 251 raise 252 return name 253 254 def _test_database_passwd(settings): 255 name = PASSWORD 256 try: 257 if settings.TEST_DATABASE_PASSWD: 258 name = settings.TEST_DATABASE_PASSWD 259 except AttributeError: 260 pass 261 except: 262 raise 263 return name 264 265 def _test_database_tblspace(settings): 266 name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME 267 try: 268 if settings.TEST_DATABASE_TBLSPACE: 269 name = settings.TEST_DATABASE_TBLSPACE 270 except AttributeError: 271 pass 272 except: 273 raise 274 return name 275 276 def _test_database_tblspace_tmp(settings): 277 name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME + '_temp' 278 try: 279 if settings.TEST_DATABASE_TBLSPACE_TMP: 280 name = settings.TEST_DATABASE_TBLSPACE_TMP 281 except AttributeError: 282 pass 283 except: 284 raise 285 return name