Changeset 1611


Ignore:
Timestamp:
Nov 10, 2008, 11:41:13 PM (16 years ago)
Author:
broder
Message:

Eliminate the need for a deep copy of database objects during the
migration with weird SQLAlchemy haxory.

This requires the migration script to have its own unique version of
the database library

Location:
trunk/scripts
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scripts/xvm-migrate-machine

    r1437 r1611  
    88
    99
    10 from invirt import database
     10from lib import database
    1111
    1212dev_db_uri = 'postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen'
     13database.connect(dev_db_uri)
     14dev_sess = database.session
    1315
     16database.connect()
     17prod_sess = database.session
    1418
    1519def take_data(machine_name):
    1620## dump from dev db; save info well
    17   database.connect(dev_db_uri)
    18   database.session.begin()
    19   machine = database.Machine.query().get_by(name=machine_name)
    20   disks = database.Disk.query().filter_by(machine=machine).all()
    21   nics = database.NIC.query().filter_by(machine=machine).all()
    22 ## remove from dev db; ideally atomic with dump
     21  dev_sess.begin()
     22  machine = dev_sess.query(database.Machine).filter_by(name=machine_name).one()
     23 
     24  # Clean out the ACL just so we don't have to think about it
     25  machine.acl = []
     26  dev_sess.update(machine)
     27 
     28  disks = machine.disks
     29  nics = machine.nics
    2330  for r in disks + nics + [machine]:
    2431    database.session.delete(r)
     32 
    2533  database.session.commit()
    26   return machine, disks, nics
    27 
     34 
     35  for r in disks + nics + [machine]:
     36    dev_sess.expunge(i)
     37    del i._instance_key
     38 
     39  return machine
    2840
    2941## shut down if up
     
    3850
    3951## add to dev db
    40 def restore_data(machine, disks, nics):
    41   database.connect() # to prod DB
    42   database.session.begin()
     52def restore_data(machine):
     53  machine.type = prod_sess.query(database.Type).filter_by(type_id=machine.type.type_id).one()
     54  prod_sess.begin()
    4355  ## now copy machine, disks, nics to new ORM objects (yuck, oh well)
    4456  ## and database.session.save(those)
    45   database.session.commit()
     57  prod_sess.save(machine)
     58  prod_sess.commit()
    4659
    4760
Note: See TracChangeset for help on using the changeset viewer.