def apply_view(self, base, properties): """ Transform the sqlmodel to view model :param base: Model cls :param properties: properties of the model :exception: MigrationException :exception: ViewException """ tablename = base.__tablename__ if hasattr(base, '__view__'): view = base.__view__ elif tablename in self.registry.loaded_views: view = self.registry.loaded_views[tablename] else: if not hasattr(base, 'sqlalchemy_view_declaration'): raise ViewException( "%r.'sqlalchemy_view_declaration' is required to " "define the query to apply of the view" % base) view = table(tablename) selectable = getattr(base, 'sqlalchemy_view_declaration')() if isinstance(selectable, Query): selectable = selectable.subquery() for c in selectable.c: c._make_proxy(view) metadata = self.registry.declarativebase.metadata event.listen(metadata, 'before_create', DropView(view, if_exists=True)) event.listen(metadata, 'after_create', CreateView(view, selectable)) event.listen(metadata, 'before_drop', DropView(view, if_exists=True)) self.registry.loaded_views[tablename] = view pks = [ col for col in properties['loaded_columns'] if getattr(getattr(base, anyblok_column_prefix + col), 'primary_key', False) ] if not pks: raise ViewException("%r have any primary key defined" % base) pks = [getattr(view.c, x) for x in pks] mapper_properties = self.get_mapper_properties(base, view, properties) setattr(base, '__view__', view) __mapper__ = mapper(base, view, primary_key=pks, properties=mapper_properties) self.registry.declarativebase._decl_class_registry[ base.__name__] = base setattr(base, '__mapper__', __mapper__)
def test_drop_cascade(): expected_result = """ DROP VIEW myview CASCADE """ view = Table('myview', sa.MetaData()) drop_view = DropView(view, cascade=True) assert clean(expected_result) == clean(compile_query(drop_view))
def test_drop_basic_view(): expected_result = """ DROP VIEW myview """ view = Table('myview', sa.MetaData()) drop_view = DropView(view) assert clean(expected_result) == clean(compile_query(drop_view))
def test_drop_with_delimited_identifier(): expected_result = """ DROP VIEW IF EXISTS "my nice view!" """ view = Table('my nice view!', sa.MetaData()) drop_view = DropView(view, if_exists=True) assert clean(expected_result) == clean(compile_query(drop_view))
def test_drop_if_exists(): expected_result = """ DROP VIEW IF EXISTS myview """ view = Table('myview', sa.MetaData()) drop_view = DropView(view, if_exists=True) assert clean(expected_result) == clean(compile_query(drop_view))
date_range = st.sidebar.slider('Укажите временной диапазон:', value=(date(2002, 9, 1), date.today()), format='D.M.Y') filters = { 'campus': campus, 'tag': tag, 'branch': branch, 'section': section, 'person': person } filtered_view = Table('filtered_view', metadata) filter_data = filter_rows(data, filters, date_range) drop_view = DropView(filtered_view, if_exists=True).compile() c.execute(str(drop_view)) conn.commit() filter_view_query = CreateView(filtered_view, filter_data).compile() c.execute(str(filter_view_query)) conn.commit() # -------------------------------------------------- First Page Section ------------------------------------------------ st.title('Корпус новостного раздела Вышки') st.header('Статистика по запросу') avg_text_len = c.execute( 'SELECT avg(length(text)) FROM filtered_view;').fetchone()[0] news_count = c.execute(
def create_or_drop_view(self, view: Table, view_query: Any) -> DDLElement: if self.action == VaultAction.CREATE: return CreateView(view, view_query) elif self.action == VaultAction.DROP: return DropView(view)