Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create 'view migration' script using alembic tool

Does somebody know how to use create_view function to create a view using alembic upgrade function? for example, we have:

CREATE VIEW myview AS
SELECT column_name(s)
FROM table_name
 WHERE condition

Now we want to define a view in alembic upgrade function script to create 'myview'. How to realize that?

Thanks.

like image 398
user1342336 Avatar asked Feb 19 '23 19:02

user1342336


1 Answers

I know, the question is way too old and probably the possibility didn't exist at that time. However for all the ones coming to this question now: there exists a possibility now described in the cookbook.

It leverages the creation, deletion and replacement of objects like views and stored procedures by giving you additional operations like op.create_view, op.drop_view or op.replace_view(myview, replaces="3f2ab897a.myview"). The same is true for stored procedures and can be also extended for e.g. user defined functions.

A view is defined the following way:

myview = ReplaceableObject('myview',
    """
    SELECT * FROM mytable
    """
)

Of course all of this can also be done with simple op.execute statements, however in this case alembic takes care of the DROP VIEW, CREATE VIEW and ALTER VIEW commands.

like image 51
taffit Avatar answered Feb 25 '23 11:02

taffit