~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to database/schema/upgrade.py

  • Committer: Curtis Hovey
  • Date: 2011-05-27 21:53:34 UTC
  • mto: This revision was merged to the branch mainline in revision 13136.
  • Revision ID: curtis.hovey@canonical.com-20110527215334-jqlkmt52nnl4bpeh
Moved launchpad.event into registry interfaces.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
__metaclass__ = type
11
11
 
12
12
# pylint: disable-msg=W0403
13
 
import _pythonpath  # Sort PYTHONPATH
 
13
import _pythonpath # Sort PYTHONPATH
14
14
 
15
15
from cStringIO import StringIO
16
16
import glob
17
17
import os.path
18
18
from optparse import OptionParser
19
19
import re
 
20
import sys
20
21
from tempfile import NamedTemporaryFile
21
22
from textwrap import dedent
22
23
 
100
101
 
101
102
def to_seconds(td):
102
103
    """Convert a timedelta to seconds."""
103
 
    return td.days * (24 * 60 * 60) + td.seconds + td.microseconds / 1000000.0
 
104
    return td.days * (24*60*60) + td.seconds + td.microseconds/1000000.0
104
105
 
105
106
 
106
107
def report_patch_times(con, todays_patches):
178
179
    # The first script applies the DB patches to all nodes.
179
180
 
180
181
    # First make sure the cluster is synced.
181
 
    log.info("Waiting for cluster to sync, pre-update.")
 
182
    log.info("Waiting for cluster to sync.")
182
183
    replication.helpers.sync(timeout=600)
183
184
 
184
185
    outf = StringIO()
246
247
        """)
247
248
 
248
249
    # Execute the script with slonik.
249
 
    log.info("slonik(1) schema upgrade script generated. Invoking.")
250
250
    if not replication.helpers.execute_slonik(outf.getvalue()):
251
251
        log.fatal("Aborting.")
252
 
        raise SystemExit(4)
253
 
    log.info("slonik(1) schema upgrade script completed.")
254
252
 
255
253
    # Cleanup our temporary files - they applied successfully.
256
254
    for temporary_file in temporary_files:
258
256
    del temporary_files
259
257
 
260
258
    # Wait for replication to sync.
261
 
    log.info("Waiting for patches to apply to slaves and cluster to sync.")
262
259
    replication.helpers.sync(timeout=0)
263
260
 
264
261
    # The db patches have now been applied to all nodes, and we are now
351
348
            print >> outf, dedent("""\
352
349
                echo 'Subscribing holding set to @node%d_node.';
353
350
                subscribe set (
354
 
                    id=@holding_set, provider=@master_node,
355
 
                    receiver=@node%d_node, forward=yes);
356
 
                wait for event (
357
 
                    origin=@master_node, confirmed=all,
358
 
                    wait on=@master_node, timeout=0);
 
351
                    id=@holding_set,
 
352
                    provider=@master_node, receiver=@node%d_node, forward=yes);
359
353
                echo 'Waiting for sync';
360
354
                sync (id=@master_node);
361
355
                wait for event (
362
356
                    origin=@master_node, confirmed=ALL,
363
 
                    wait on=@master_node, timeout=0);
 
357
                    wait on=@master_node, timeout=0
 
358
                    );
364
359
                """ % (slave_node.node_id, slave_node.node_id))
365
360
 
366
361
        print >> outf, dedent("""\
367
362
            echo 'Merging holding set to lpmain';
368
363
            merge set (
369
 
                id=@lpmain_set, add id=@holding_set, origin=@master_node);
 
364
                id=@lpmain_set, add id=@holding_set, origin=@master_node
 
365
                );
370
366
            """)
371
367
 
372
368
        # Execute the script and sync.
373
 
        log.info(
374
 
            "Generated slonik(1) script to replicate new objects. Invoking.")
375
369
        if not replication.helpers.execute_slonik(outf.getvalue()):
376
370
            log.fatal("Aborting.")
377
 
        log.info(
378
 
            "slonik(1) script to replicate new objects completed.")
379
 
        log.info("Waiting for sync.")
380
371
        replication.helpers.sync(timeout=0)
381
 
    else:
382
 
        log.info("No new tables or sequences to replicate.")
383
372
 
384
373
    # We also scan for tables and sequences we want to drop and do so using
385
374
    # a final slonik script. Instead of dropping tables in the DB patch,
422
411
                exit 1;
423
412
                }
424
413
            """ % sql.name)
425
 
        log.info("Generated slonik(1) script to drop tables. Invoking.")
426
414
        if not replication.helpers.execute_slonik(sk.getvalue()):
427
415
            log.fatal("Aborting.")
428
 
        log.info("slonik(1) script to drop tables completed.")
429
416
        sql.close()
430
417
 
431
418
    # Now drop sequences. We don't do this at the same time as the tables,
468
455
                exit 1;
469
456
                }
470
457
            """ % sql.name)
471
 
        log.info("Generated slonik(1) script to drop sequences. Invoking.")
472
458
        if not replication.helpers.execute_slonik(sk.getvalue()):
473
459
            log.fatal("Aborting.")
474
 
        log.info("slonik(1) script to drop sequences completed.")
475
 
    log.info("Waiting for final sync.")
476
460
    replication.helpers.sync(timeout=0)
477
461
 
478
462
 
491
475
        m = re.search('patch-(\d+)-(\d+)-(\d).sql$', patch_file)
492
476
        if m is None:
493
477
            log.fatal('Invalid patch filename %s' % repr(patch_file))
494
 
            raise SystemExit(1)
 
478
            sys.exit(1)
495
479
 
496
480
        major, minor, patch = [int(i) for i in m.groups()]
497
481
        if (major, minor, patch) in dbpatches:
498
 
            continue  # This patch has already been applied
 
482
            continue # This patch has already been applied
499
483
        log.debug("Found patch %d.%d.%d -- %s" % (
500
484
            major, minor, patch, patch_file
501
485
            ))
520
504
    if (major, minor, patch) not in applied_patches(con):
521
505
        log.fatal("%s failed to update LaunchpadDatabaseRevision correctly"
522
506
                % patch_file)
523
 
        raise SystemExit(2)
 
507
        sys.exit(2)
524
508
 
525
509
    # Commit changes if we allow partial updates.
526
510
    if options.commit and options.partial:
539
523
        # environment.
540
524
        log.fatal(
541
525
            "Last non-whitespace character of %s must be a semicolon", script)
542
 
        raise SystemExit(3)
 
526
        sys.exit(3)
543
527
    cur.execute(sql)
544
528
 
545
529
    if not no_commit and options.commit and options.partial: