Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions sqlalchemy_utils/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,40 @@


class CreateView(DDLElement):
def __init__(self, name, selectable, materialized=False):
def __init__(self, name, selectable, materialized=False, schema=None):
self.name = name
self.selectable = selectable
self.materialized = materialized
self.schema = schema


@compiler.compiles(CreateView)
def compile_create_materialized_view(element, compiler, **kw):
return 'CREATE {}VIEW {} AS {}'.format(
return 'CREATE {}VIEW {}{} AS {}'.format(
'MATERIALIZED ' if element.materialized else '',
f'{compiler.dialect.identifier_preparer.quote(element.schema)}.'
if element.schema
else '',
compiler.dialect.identifier_preparer.quote(element.name),
compiler.sql_compiler.process(element.selectable, literal_binds=True),
)


class DropView(DDLElement):
def __init__(self, name, materialized=False, cascade=True):
def __init__(self, name, materialized=False, cascade=True, schema=None):
self.name = name
self.materialized = materialized
self.cascade = cascade
self.schema = schema


@compiler.compiles(DropView)
def compile_drop_materialized_view(element, compiler, **kw):
return 'DROP {}VIEW IF EXISTS {} {}'.format(
return 'DROP {}VIEW IF EXISTS {}{} {}'.format(
'MATERIALIZED ' if element.materialized else '',
f'{compiler.dialect.identifier_preparer.quote(element.schema)}.'
if element.schema
else '',
compiler.dialect.identifier_preparer.quote(element.name),
'CASCADE' if element.cascade else ''
)
Expand Down Expand Up @@ -103,7 +111,7 @@ def create_materialized_view(
sa.event.listen(
metadata,
'after_create',
CreateView(name, selectable, materialized=True)
CreateView(name, selectable, materialized=True, schema=metadata.schema)
)

@sa.event.listens_for(metadata, 'after_create')
Expand All @@ -114,7 +122,7 @@ def create_indexes(target, connection, **kw):
sa.event.listen(
metadata,
'before_drop',
DropView(name, materialized=True)
DropView(name, materialized=True, schema=metadata.schema)
)
return table

Expand Down Expand Up @@ -163,7 +171,11 @@ def create_view(
metadata=None
)

sa.event.listen(metadata, 'after_create', CreateView(name, selectable))
sa.event.listen(
metadata,
'after_create',
CreateView(name, selectable, schema=metadata.schema)
)

@sa.event.listens_for(metadata, 'after_create')
def create_indexes(target, connection, **kw):
Expand All @@ -173,7 +185,7 @@ def create_indexes(target, connection, **kw):
sa.event.listen(
metadata,
'before_drop',
DropView(name, cascade=cascade_on_drop)
DropView(name, cascade=cascade_on_drop, schema=metadata.schema)
)
return table

Expand Down