def status(command, args): get_engine() get_model() ret = compare_metadata(metadata, MetaData(metadata.bind)) for l in ret: print l if not ret: print "Database matches model"
def setUp(self): if not self.model: self.model = get_model() if not self.model: raise "Unable to run database tests without a model" # list of constraints we will collect constraints = list() for item in self._get_soClasses(): if isinstance(item, types.TypeType) and issubclass(item, sqlobject.SQLObject) and item != sqlobject.SQLObject \ and item != InheritableSQLObject: # create table without applying constraints, collect # all the constaints for later creation. # see http://sqlobject.org/FAQ.html#mutually-referencing-tables # for more info collected_constraints = item.createTable(ifNotExists=True, applyConstraints=False) if collected_constraints: constraints.extend(collected_constraints) # now that all tables are created, add the constaints we collected for postponed_constraint in constraints: # item is the last processed item and we borrow its connection item._connection.query(postponed_constraint)
def setUp(self): if not self.model: self.model = get_model() if not self.model: raise "Unable to run database tests without a model" # list of constraints we will collect constraints = list() for item in self._get_soClasses(): if isinstance(item, types.TypeType) and issubclass(item, sqlobject.SQLObject) and item != sqlobject.SQLObject \ and item != InheritableSQLObject: # create table without applying constraints, collect # all the constaints for later creation. # see http://sqlobject.org/FAQ.html#mutually-referencing-tables # for more info collected_constraints = item.createTable( ifNotExists=True, applyConstraints=False) if collected_constraints: constraints.extend(collected_constraints) # now that all tables are created, add the constaints we collected for postponed_constraint in constraints: # item is the last processed item and we borrow its connection item._connection.query(postponed_constraint)
def setUp(self): if not self.model: self.model = get_model() if not self.model: raise "Unable to run database tests without a model" for item in self.model.__dict__.values(): if isinstance(item, types.TypeType) and issubclass(item, sqlobject.SQLObject) and item != sqlobject.SQLObject \ and item != InheritableSQLObject: item.createTable(ifNotExists=True)
def create_tables(drop_all=False): """Create all tables defined in the model in the database. Optionally drop existing tables before creating them. """ from turbogears.util import get_model from inspect import isclass model = get_model() if not model: from rmidb2.command import ConfigurationError raise ConfigurationError( "Unable to create database tables without a model") try: so_classes = [model.__dict__[x] for x in model.soClasses] except AttributeError: so_classes = model.__dict__.values() if drop_all: print "Dropping all database tables defined in model." for item in reversed(so_classes): if (isclass(item) and issubclass(item, SQLObject) and item is not SQLObject and item is not InheritableSQLObject): item.dropTable(ifExists=True, cascade=True) # list of constraints we will collect constraints = list() for item in so_classes: if (isclass(item) and issubclass(item, SQLObject) and item is not SQLObject and item is not InheritableSQLObject): # create table without applying constraints, collect # all the constaints for later creation. # see http://sqlobject.org/FAQ.html#mutually-referencing-tables # for more info collected_constraints = item.createTable( ifNotExists=True, applyConstraints=False) if collected_constraints: constraints.extend(collected_constraints) # now that all tables are created, add the constaints we collected for postponed_constraint in constraints: # item is the last processed item and we borrow its connection item._connection.query(postponed_constraint) print "All database tables defined in model created."
def run(self): """Run the shell""" self.find_config() locals = dict(__name__='tg-admin') try: mod = get_model() if mod: locals.update(mod.__dict__) except (pkg_resources.DistributionNotFound, ImportError), e: mod = None print "Warning: Failed to import your data model: %s" % e print "You will not have access to your data model objects." print
def create_tables(drop_all=False): """Create all tables defined in the model in the database. Optionally drop existing tables before creating them. """ from turbogears.util import get_model from inspect import isclass model = get_model() if not model: from proteindigest.command import ConfigurationError raise ConfigurationError( "Unable to create database tables without a model") try: so_classes = [model.__dict__[x] for x in model.soClasses] except AttributeError: so_classes = model.__dict__.values() if drop_all: print "Dropping all database tables defined in model." for item in reversed(so_classes): if isclass(item) and issubclass(item, SQLObject) and \ item is not SQLObject and item is not InheritableSQLObject: item.dropTable(ifExists=True, cascade=True) # list of constraints we will collect constraints = list() for item in so_classes: if isclass(item) and issubclass(item, SQLObject) and \ item is not SQLObject and item is not InheritableSQLObject: # create table without applying constraints, collect # all the constaints for later creation. # see http://sqlobject.org/FAQ.html#mutually-referencing-tables # for more info collected_constraints = item.createTable(ifNotExists=True, applyConstraints=False) if collected_constraints: constraints.extend(collected_constraints) # now that all tables are created, add the constaints we collected for postponed_constraint in constraints: # item is the last processed item and we borrow its connection item._connection.query(postponed_constraint) print "All database tables defined in model created."
def run(self): """Run the shell""" self.find_config() mod = get_model() if mod: locals = mod.__dict__ else: locals = dict(__name__="tg-admin") if config.get("sqlalchemy.dburi"): using_sqlalchemy = True database.get_engine() locals.update(dict(session=database.session, metadata=database.metadata)) else: using_sqlalchemy = False try: # try to use IPython if possible from IPython.terminal.interactiveshell import TerminalInteractiveShell class CustomIPShell(TerminalInteractiveShell): def raw_input(self, *args, **kw): try: return TerminalInteractiveShell.raw_input(self, *args, **kw) # needs decoding (see below)? except EOFError: r = raw_input("Do you wish to commit your " "database changes? [yes]") if not r.lower().startswith("n"): if using_sqlalchemy: self.push("session.flush()") else: self.push("hub.commit()") raise EOFError shell = CustomIPShell(user_ns=locals) shell.mainloop() except ImportError: import code class CustomShell(code.InteractiveConsole): def raw_input(self, *args, **kw): try: import readline except ImportError: pass try: r = code.InteractiveConsole.raw_input(self, *args, **kw) for encoding in (getattr(sys.stdin, 'encoding', None), sys.getdefaultencoding(), 'utf-8', 'latin-1'): if encoding: try: return r.decode(encoding) except UnicodeError: pass return r except EOFError: r = raw_input("Do you wish to commit your " "database changes? [yes]") if not r.lower().startswith("n"): if using_sqlalchemy: self.push("session.flush()") else: self.push("hub.commit()") raise EOFError shell = CustomShell(locals=locals) shell.interact()
def run(self): "Run the shell" self.find_config() mod = get_model() if mod: locals = mod.__dict__ else: locals = dict(__name__="tg-admin") if config.get("sqlalchemy.dburi"): using_sqlalchemy = True database.bind_meta_data() locals.update(session=database.session, metadata=database.metadata) else: using_sqlalchemy = False try: # try to use IPython if possible import IPython class CustomIPShell(IPython.iplib.InteractiveShell): def raw_input(self, *args, **kw): try: return \ IPython.iplib.InteractiveShell.raw_input(self, *args, **kw) except EOFError: b = raw_input("Do you wish to commit your " "database changes? [yes]") if not b.startswith("n"): if using_sqlalchemy: self.push("session.flush()") else: self.push("hub.commit()") raise EOFError shell = IPython.Shell.IPShell(user_ns=locals, shell_class=CustomIPShell) shell.mainloop() except ImportError: import code class CustomShell(code.InteractiveConsole): def raw_input(self, *args, **kw): try: import readline except ImportError: pass try: return code.InteractiveConsole.raw_input(self, *args, **kw) except EOFError: b = raw_input("Do you wish to commit your " "database changes? [yes]") if not b.startswith("n"): if using_sqlalchemy: self.push("session.flush()") else: self.push("hub.commit()") raise EOFError shell = CustomShell(locals=locals) shell.interact()
def list_(command, args): get_model() for tbl in metadata.tables.values(): print tbl.fullname
def create(command, args): print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) get_engine() get_model() metadata.create_all()
def sacreate(command, args): print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) from turbogears.database import bind_meta_data, metadata bind_meta_data() get_model() metadata.create_all()
def setUp(self): if not self.model: self.model = get_model() if not self.model: raise Exception("Unable to run database tests without a model")
def get_model_name(self): if not get_project_name(): return False return get_model()