Code

Ticket #3174: patch-boulder-oracle-sprint.diff

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