def get_playlist_table_class(id): if id is None: raise ValueError("A playlist id must be specified") table_name = "_playlist_{}".format(id) # table name is prefix + table_id # if table exist use autoload otherwise create the table table_exist = table_name in sqlalchemy.inspect(db.engine).get_table_names() class PTable(Base): __tablename__ = table_name # table name # set table args to load existing table if possible __table_args__ = { 'extend_existing': True, 'autoload': table_exist, 'autoload_with': db.get_engine() } id = db.Column(db.Integer, primary_key=True) element_type = db.Column(db.String(10), default="") drawing_id = db.Column( db.Integer, default=None) # drawing id added explicitely for possible queries element_options = db.Column(db.String(1000), default="") # element options # change class attrs manually to avoid getting a warning ("This declarative base already contains a class with the same class name and module name") PTable.__name__ = table_name PTable.__qualname__ = table_name PTable.__module__ = table_name if not table_exist: PTable.__table__.create(db.get_engine()) return PTable
class PTable(Base): __tablename__ = table_name # table name # set table args to load existing table if possible __table_args__ = { 'extend_existing': True, 'autoload': table_exist, 'autoload_with': db.get_engine() } id = db.Column(db.Integer, primary_key=True) element_type = db.Column(db.String(10), default="") drawing_id = db.Column( db.Integer, default=None) # drawing id added explicitely for possible queries element_options = db.Column(db.String(1000), default="") # element options
def delete_playlist_table(id): """ Delete a table associated to a single playlist. """ p_class = get_playlist_table_class(id) p_class.__table__.drop(db.get_engine())
def tearDown(self): db.session.remove() db.drop_all() db.get_engine(self.app).dispose() self._ctx.pop()