TL DR: How do I get alembic for understanding and generating SQL for materialized views created in sqlalchemy?
I am using flask-sqlalchemy as well as using alembic with postgres. To get a materialized view working with sqlalchemy, I followed a good post on this topic. I used it to a large extent, with a few minor deviations (the article also uses the sqlalchemy bulb, however, in the full code example, the sqlalchemy database is used instead).
class ActivityView(db.Model):
__table__ = create_materialized_view(
'activity_view',
db.select([
Activity.id.label('id'),
Activity.name.label('name'),
Activity.start_date.label('start_date'),
]).where(
db.and_(
Activity.start_date != None,
Activity.start_date <=
datetime_to_str(datetime.now(tz=pytz.UTC) + timedelta(hours=48))
)
)
)
@classmethod
def refresh(cls, concurrently=True):
refresh_materialized_view(cls.__table__.fullname, concurrently)
db.Index('activity_view_index',
ActivityView.__table__.c.id, ActivityView.__table__.c.start_date,
unique=True)
Methods create_materialized_viewand refresh_materialized_vieware taken directly from the blog post.
, , , - , , , , - , alembic alembic ?
, , , . alembic, . SQL, / alembic, :
op.execute(activities_view_sql)
, SQL .
, , -, .
alembic ActivityView, , , alembic , ?
!