Comments (2)
alembic
and, by extension, alembic_utils
provide tooling for people who want to produce procedural migration scripts for altering their DB's schema. Supporting point-in-time reflection is not a goal of the project
But, if your use case is simple enough, you may be able to do something like
from alembic_utils.experimental import collect instances
from alembic_utils.pg_function import PGFunction
def create_database(connection, metadata) -> None:
"""Create database from sqlalchemy metadata + alembic_utils PGFunctions"""
sqlalchemy_utils.functions.create_database(...)
for function in collect_instances(your_app, PGFunction):
connection.execute(function.to_sql_statement_create())
from alembic_utils.
Thanks for the quick reply! I followed a similar approach using event listening
def pgfunction_to_sqlalchemy_text_create_or_replace(function: PGFunction):
statement = f"CREATE OR REPLACE FUNCTION {function.literal_schema}.{function.literal_signature} {function.definition}"
return text(statement)
def create_all_pg_triggers(connection: Connection, pg_triggers: Iterable[PGTrigger]):
for pg_trigger in pg_triggers:
for generated_text in pg_trigger.to_sql_statement_create_or_replace():
connection.execute(generated_text.text)
def create_all_pg_functions(connection: Connection, pg_functions: Iterable[PGFunction]):
for pg_function in pg_functions:
connection.execute(pgfunction_to_sqlalchemy_text_create_or_replace(pg_function))
@event.listens_for(BaseModel.metadata, 'after_create', named=True)
def create_category_pg_functions_and_triggers_on_db(connection: Connection, **kwargs):
create_all_pg_functions(connection=connection, pg_functions=PG_FUNCTION_LIST)
create_all_pg_triggers(connection=connection, pg_triggers=PG_TRIGGER_LIST)
from alembic_utils.
Related Issues (20)
- [Question] Using Alembic Utils without SQL Alchemy models HOT 3
- Compatibility with SQLAlchemy 2.0 HOT 4
- How to add a function with an " text[] default array['text', 'text2']"? HOT 3
- PGExtension replace `create` with `create if not exists` HOT 4
- Must be owner of materialized view mat_view_name HOT 2
- Alembic autogenerate broken for 'internal' PG functions HOT 6
- INFO: Transaction approach incompatible with MySQL
- alembic check broken - diff cannot be rendered HOT 1
- Materialized view change detection fails if upstream view has changed HOT 6
- DropOp dependency ordering when dropping multiple associated entities HOT 4
- Publishing next release HOT 1
- alembic_utils does not work with Redshift Dialect. HOT 1
- Adapting simulate_entities() to use .begin() instead of .begin_nested() HOT 1
- Extremely slow HOT 4
- connection was closed in the middle of operation HOT 2
- Unexpected white space inclusion in signature (Pg error: function ... does not exist) HOT 1
- Is it possible to create PGTrigger in all the table by expanding declarative_base? HOT 3
- Document the equivilent of HOT 1
- Downgrade does not reflect old definition
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alembic_utils.