[Modeling-cvs] ProjectModeling/Modeling/scripts bug861048.py,NONE,1.1
Status: Abandoned
Brought to you by:
sbigaret
From: <sbi...@us...> - 2003-12-17 17:00:12
|
Update of /cvsroot/modeling/ProjectModeling/Modeling/scripts In directory sc8-pr-cvs1:/tmp/cvs-serv22467/Modeling/scripts Added Files: bug861048.py Log Message: Added scripts/bug861048.py --- NEW FILE: bug861048.py --- #! /usr/bin/env python #----------------------------------------------------------------------------- # # Modeling Framework: an Object-Relational Bridge for python # (c) 2001, 2002, 2003 Sebastien Bigaret # # This file is part of the Modeling Framework. # # The Modeling Framework is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as published # by the Free Software Foundation; either version 2 of the License, or (at # your option) any later version. # # The Modeling Framework is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License along # with the Modeling Framework; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # #----------------------------------------------------------------------------- """ Diagnose bug #861048 and tells what should be done, if appropriate """ __version__='$Revision: 1.1 $'[11:-2] def bug861048(model): """Tells whether the supplied model is affected by bug#861048, and prints the necessary steps to corect the database, if appropriate""" dicts=[] for e in model.entities(): for relationship in e.relationships(): # the following conditions are roughly copy/pasted from SchemaGeneration if relationship.isToMany() or relationship.isFlattened(): continue # if there is an inverseRelationship, it should be a toMany relationship inverseRel = relationship.inverseRelationship() if not (inverseRel is None or inverseRel.isToMany() ): continue # Both source and destination entities should belong to the same model srcEntity=relationship.sourceEntity() dstEntity=relationship.destinationEntity() if srcEntity.model() != dstEntity.model(): continue if not dstEntity.subEntities(): continue from string import join vars={'table': srcEntity.externalName(), 'relName': relationship.name(), 'FKs': join([attr.columnName() for attr in relationship.sourceAttributes()], ','), 'dst_table': dstEntity.externalName(), 'PKs': join([attr.columnName() for attr in relationship.destinationAttributes()], ','), } dicts.append(vars) sqlite='CONSTRAINT %(relName)s FOREIGN KEY (%(FKs)s) REFERENCES %(dst_table)s(%(PKs)s)' others='ALTER TABLE %(table)s DROP CONSTRAINT %(relName)s;' if not dicts: print "The model %s is not affected by bug #861048"%model.name() else: print """The model %s is affected by bug #861048"""%model.name() print """ - PostgreSQL, MySQL, Oracle: connect to your db and correct the schema with the following statements:""" print "\n---- cut here ----" for vars in dicts: print others%vars print "---- cut here ----" print """ - SQLite; sorry, but SQLite does not support ALTER TABLE, so you have no other choice but to: 1. backup the file containing your database, 2. dump your database: $ sqlite your_database .dump > dump.db.sqlite 3. edit the file dump.db.sqlite and:""" for vars in dicts: print " - in CREATE TABLE %(table)s (...), remove the statement:"%vars print sqlite%vars print """ (you may have to remove a trailing comma from the previous statement if the CONSTRAINT line was the last one before the closing bracket in CREATE TABLE) 4. recreate your database: $ mv your_database your_database.backup $ sqlite your_database ".read dump.db.sqlite" """ return dicts if __name__=="__main__": import os, sys if len(sys.argv)!=2: print "Usage: %s <model_file>"%sys.argv[0] sys.exit() from Modeling import ModelSet, Model model=Model.loadModel(sys.argv[1]) bug861048(model) |