#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/site/update_db.py $
$Id: update_db.py 29620 2007-03-15 20:19:23Z dbinger $

When we update the live site, this is our procedure.
Make a tarball of the running code and database file.
This is the emergency kit.
Copy the revised code into place.

If the code revisions require corresponding data revisions, 
  Copy the database file to a temporary name.
  Run an update_db.py script that changes attribute names, classes,
  or whatever is needed to convert the database to agree with the 
  new version of the code.  
  qp -d <site>
  Move the old database file away.  Move the fixed version into place.
  qp -u <site>
Otherwise,
  there are no data changes, so we just 
  qp -q <site>

This script is intended to serve as an example of an update_db.py
for a site named "dulcinea".
"""
from qp.lib.site import Site
import sys
connection = root = commit = pack = trace = None
# This call sets connection, root, commit, pack, and trace.
Site('dulcinea').update_db_environment(globals(), *sys.argv[1:])

@trace
def repickle():
    # We have changed some classes from old-style to new-style.
    # By itself, that is not a problem, but we have also made the
    # new-style classes use slots for attributes, and that means
    # that a __setstate__ is required to unpickle the old-style
    # pickles.  With this function, we re-pickle all objects that
    # contain references to these classes, and then the __setstate__
    # methods will not be needed.
    # First hack in a temporary __setststate__ method.
    def __setstate__(self, state):
        for key, value in state.items():
            setattr(self, key, value)
    from dulcinea.physical_value import PhysicalValue
    from dulcinea.range_value import RangeValue
    from dulcinea.tolerance import Tolerance
    from dulcinea.history import Event
    from dulcinea.survey import Answer
    PhysicalValue.__setstate__ = __setstate__
    RangeValue.__setstate__ = __setstate__
    Tolerance.__setstate__ = __setstate__
    Event.__setstate__ = __setstate__
    Answer.__setstate__ = __setstate__
    # Now repickle everything that appears to contain an instance of one of
    # these classes.
    from durus.connection import touch_every_reference
    touch_every_reference(
        connection, 'PhysicalValue', 'RangeValue', 'Tolerance',
        'Event', 'Answer')


# Uncomment this if you want the script to run repickle().
#repickle()

commit() # probably required.
pack() # optional, but nice if you have time for it.  
