web.db

Package web :: Module db |
[hide private]
[frames] | no frames]

# Source Code for Module web.db

      1  #!/usr/bin/python
      2
      3  import sqlalchemy
      4  from sqlalchemy import create_engine
      5  from sqlalchemy.orm import sessionmaker, scoped_session
      6  from sqlalchemy.ext.declarative import declarative_base
      7
      8  import config
      9
     10  Base = declarative_base()
     11
     12
     13 -def get_tables(model):
     14      for i in dir(model):
     15          var = getattr(model, i)
     16          if type(var) == type(Base) and var.__module__ == model.__name__:
     17              # subtables, many to many relations
     18              for i in dir(var):
     19                  var2 = getattr(var, i)
     20                  if type(var2) == sqlalchemy.schema.Table:
     21                      yield var2
     22              yield var
     23
     24
     25  models = ('main.model',
     26            'pluginmanager.plugins.model',
     27            'user.model',
     28            'clientjob.model',)
     29
     30 -def init_model():
     31      '''
     32      returns the needed Base and metadata for defining new classes.
     33      New classes should inheritance from Base.
     34
     35      >>> Base, metadata = init_model()
     36      '''
     37
     38      base = declarative_base()
     39      metadata = base.metadata
     40
     41      return base, metadata
     42
     43
     44 -def create_from_model(model, database=config.DB):
     45      '''
     46      Create tables defined in a model.
     47      '''
     48
     49      metadata = model.metadata
     50      db = create_engine(database, echo=False)
     51      for table in get_tables(model):
     52          if type(table) == sqlalchemy.schema.Table:
     53              t = table
     54          else:
     55              print "creating table %s" % table.__tablename__
     56              t = metadata.tables[table.__tablename__]
     57          t.create(bind=db, checkfirst=True)
     58
     59
     60 -def drop_from_model(model, database=config.DB):
     61      '''
     62      Drop tables defined in a model.
     63      '''
     64
     65      metadata = model.metadata
     66      s = connect()
     67
     68      for table in get_tables(model):
     69          if type(table) == sqlalchemy.schema.Table:
     70              continue
     71          for i in s.query(table).all():
     72              s.delete(i)
     73
     74      for table in get_tables(model):
     75          if type(table) == sqlalchemy.schema.Table:
     76              t = table
     77          else:
     78              print "dropping table %s" % table.__tablename__
     79              t = metadata.tables[table.__tablename__]
     80          t.drop(bind=s.bind, checkfirst=True)
     81      s.commit()
     82
     83
     84 -def connect(database=config.DB):
     85      db = create_engine(database, echo=False)
     86      session = scoped_session(sessionmaker(bind=db))
     87      return session
     88
     89
     90 -def create(database=config.DB):
     91      db = create_engine(database, echo=False)
     92      for model in models:
     93          m = __import__(model, fromlist=["true"])
     94          m.metadata.create_all(db)
     95
     96
     97  if __name__ == '__main__':
     98      create()
     99