This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/db.py

40 lines
1.3 KiB
Python
Raw Normal View History

from sqlalchemy import event
2018-08-08 19:25:53 +00:00
from sqlalchemy.dialects import postgresql
from sqlalchemy_utils import view
2018-09-07 10:35:32 +00:00
from teal.db import SchemaSQLAlchemy
2018-08-08 19:25:53 +00:00
2018-07-14 14:41:22 +00:00
2018-09-07 10:35:32 +00:00
class SQLAlchemy(SchemaSQLAlchemy):
2018-08-08 19:25:53 +00:00
"""
Superuser must create the required extensions in the public
schema of the database, as it is in the `search_path`
defined in teal.
"""
UUID = postgresql.UUID
2018-07-14 14:41:22 +00:00
def drop_all(self, bind='__all__', app=None):
"""A faster nuke-like option to drop everything."""
self.drop_schema()
self.drop_schema(schema='common')
2018-04-10 15:06:39 +00:00
db = SQLAlchemy(session_options={"autoflush": False})
def create_view(name, selectable):
"""Creates a view.
This is an adaptation from sqlalchemy_utils.view. See
`the test on sqlalchemy-utils <https://github.com/kvesteri/
sqlalchemy-utils/blob/master/tests/test_views.py>`_ for an
example on how to use.
"""
table = view.create_table_from_selectable(name=name, selectable=selectable, metadata=None)
# We need to ensure views are created / destroyed before / after
# SchemaSQLAlchemy's listeners execute
# That is why insert=True in 'after_create'
event.listen(db.metadata, 'after_create', view.CreateView(name, selectable), insert=True)
event.listen(db.metadata, 'before_drop', view.DropView(name))
return table