~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to database/schema/upgrade.py

  • Committer: Curtis Hovey
  • Date: 2011-12-29 05:29:36 UTC
  • mto: This revision was merged to the branch mainline in revision 14606.
  • Revision ID: curtis.hovey@canonical.com-20111229052936-c261pibg1p6ze6m4
Moved canonical.config to lp.services.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
__metaclass__ = type
11
11
 
12
 
import _pythonpath
 
12
# pylint: disable-msg=W0403
 
13
import _pythonpath  # Sort PYTHONPATH
13
14
 
14
15
from cStringIO import StringIO
15
16
import glob
 
17
import os.path
16
18
from optparse import OptionParser
17
 
import os.path
18
19
import re
19
20
from tempfile import NamedTemporaryFile
20
21
from textwrap import dedent
22
23
from bzrlib.branch import Branch
23
24
from bzrlib.errors import NotBranchError
24
25
 
25
 
from lp.services.database.postgresql import fqn
26
 
from lp.services.database.sqlbase import (
 
26
from lp.services.scripts import db_options, logger_options, logger
 
27
from canonical.database.sqlbase import (
27
28
    connect,
28
29
    ISOLATION_LEVEL_AUTOCOMMIT,
29
30
    sqlvalues,
30
31
    )
31
 
from lp.services.scripts import (
32
 
    db_options,
33
 
    logger,
34
 
    logger_options,
35
 
    )
 
32
from canonical.database.postgresql import fqn
36
33
import replication.helpers
37
34
 
38
35
 
110
107
        LaunchpadDatabaseRevision.start_time
111
108
            = transaction_timestamp() AT TIME ZONE 'UTC';
112
109
    """)
 
110
START_UPDATE_LOG_SQL = dedent("""\
 
111
    INSERT INTO LaunchpadDatabaseUpdateLog (
 
112
        start_time, end_time, branch_nick, revno, revid)
 
113
    VALUES (transaction_timestamp() AT TIME ZONE 'UTC', NULL, %s, %s, %s);
 
114
    """)
 
115
FINISH_UPDATE_LOG_SQL = dedent("""\
 
116
    UPDATE LaunchpadDatabaseUpdateLog
 
117
    SET end_time = statement_timestamp() AT TIME ZONE 'UTC'
 
118
    WHERE start_time = transaction_timestamp() AT TIME ZONE 'UTC';
 
119
    """)
113
120
 
114
121
 
115
122
def to_seconds(td):
162
169
 
163
170
def apply_patches_normal(con):
164
171
    """Update a non replicated database."""
 
172
    # On dev environments, until we create a fresh database baseline the
 
173
    # LaunchpadDatabaseUpdateLog tables does not exist at this point (it
 
174
    # will be created later via database patch). Don't try to update
 
175
    # LaunchpadDatabaseUpdateLog if it does not exist.
 
176
    cur = con.cursor()
 
177
    cur.execute("""
 
178
        SELECT EXISTS (
 
179
            SELECT TRUE FROM information_schema.tables
 
180
            WHERE
 
181
                table_schema='public'
 
182
                AND table_name='launchpaddatabaseupdatelog')
 
183
            """)
 
184
    updatelog_exists = cur.fetchone()[0]
 
185
 
 
186
    # Add a record to LaunchpadDatabaseUpdateLog that we are starting
 
187
    # an update.
 
188
    if updatelog_exists:
 
189
        cur.execute(START_UPDATE_LOG_SQL % sqlvalues(*get_bzr_details()))
 
190
 
165
191
    # trusted.sql contains all our stored procedures, which may
166
192
    # be required for patches to apply correctly so must be run first.
167
193
    apply_other(con, 'trusted.sql')
168
194
 
169
195
    # Prepare to repair patch timestamps if necessary.
170
 
    cur = con.cursor()
171
196
    cur.execute(FIX_PATCH_TIMES_PRE_SQL)
172
197
 
173
198
    # Apply the patches
182
207
    # Update comments.
183
208
    apply_comments(con)
184
209
 
 
210
    # Update the LaunchpadDatabaseUpdateLog record, stating the
 
211
    # completion time.
 
212
    if updatelog_exists:
 
213
        cur.execute(FINISH_UPDATE_LOG_SQL)
 
214
 
185
215
 
186
216
def apply_patches_replicated():
187
217
    """Update a Slony-I cluster."""
215
245
            # Flush or we might lose statements from buffering.
216
246
            combined_sql.flush()
217
247
 
 
248
    # Add a LaunchpadDatabaseUpdateLog record that we are starting patch
 
249
    # application.
 
250
    add_sql(START_UPDATE_LOG_SQL % sqlvalues(*get_bzr_details()))
 
251
 
218
252
    # Apply trusted.sql
219
253
    add_sql(open(os.path.join(SCHEMA_DIR, 'trusted.sql'), 'r').read())
220
254