Skip to content

Commit 58b1a8b

Browse files
authored
chore(deps): fix the compatibility issue introduced by SQLAlchemy 2.0.38 (#989)
Signed-off-by: behnazh-w <[email protected]>
1 parent cc1f7fc commit 58b1a8b

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

src/macaron/database/views.py

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 - 2023, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 2025, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
# pylint: skip-file
@@ -16,7 +16,7 @@
1616
from sqlalchemy import Connection
1717
from sqlalchemy.ext import compiler
1818
from sqlalchemy.schema import BaseDDLElement, DDLElement, MetaData, SchemaItem, Table
19-
from sqlalchemy.sql import Select, TableClause, table
19+
from sqlalchemy.sql import Select
2020

2121

2222
class CreateView(DDLElement):
@@ -52,7 +52,32 @@ def view_exists(
5252
state: Any | None = None,
5353
**kw: Any,
5454
) -> bool:
55-
"""View exists."""
55+
"""Check if a view exists in the database.
56+
57+
This function checks whether a view, defined by the `CreateView` or `DropView`
58+
object, already exists in the database. It relies on the provided `bind` connection
59+
to inspect the existing views in the database.
60+
61+
Parameters
62+
----------
63+
ddl : BaseDDLElement
64+
The DDL element that represents the creation or dropping of a view.
65+
target : SchemaItem
66+
The target schema item (not directly used in this check).
67+
bind : Connection | None
68+
The database connection used to inspect the database for existing views.
69+
tables : list[Table] | None, optional
70+
A list of tables (not directly used in this check).
71+
state : Any | None, optional
72+
The state of the object (not directly used in this check).
73+
kw : Any
74+
Additional keyword arguments passed to the function (not directly used).
75+
76+
Returns
77+
-------
78+
bool
79+
Returns `True` if the view exists in the database, `False` otherwise.
80+
"""
5681
if isinstance(ddl, CreateView) or isinstance(ddl, DropView):
5782
assert isinstance(bind, Connection)
5883
return ddl.name in sa.inspect(bind).get_view_names()
@@ -67,19 +92,60 @@ def view_doesnt_exist(
6792
state: Any | None = None,
6893
**kw: Any,
6994
) -> bool:
70-
"""Not view exists."""
95+
"""Check if a view does not exist in the database.
96+
97+
This function is the inverse of `view_exists`. It returns `True` if the view
98+
defined by the `CreateView` or `DropView` object does not exist in the database.
99+
100+
Parameters
101+
----------
102+
ddl : BaseDDLElement
103+
The DDL element that represents the creation or dropping of a view.
104+
target : SchemaItem
105+
The target schema item (not directly used in this check).
106+
bind : Connection | None
107+
The database connection used to inspect the database for existing views.
108+
tables : list[Table] | None, optional
109+
A list of tables (not directly used in this check).
110+
state : Any | None, optional
111+
The state of the object (not directly used in this check).
112+
kw : Any
113+
Additional keyword arguments passed to the function (not directly used).
114+
115+
Returns
116+
-------
117+
bool
118+
Returns `True` if the view does not exist in the database, `False` otherwise.
119+
"""
71120
return not view_exists(ddl, target, bind, **kw)
72121

73122

74-
def create_view(name: str, metadata: MetaData, selectable: Select[Any]) -> TableClause:
75-
"""Create a view."""
76-
view = table(name)
77-
view._columns._populate_separate_keys(col._make_proxy(view) for col in selectable.selected_columns)
123+
def create_view(name: str, metadata: MetaData, selectable: Select[Any]) -> None:
124+
"""Create and manage a view for an existing table, including its lifecycle.
78125
126+
This function allows you to define a SQL view based on an existing table, as well as
127+
handle operations like creation and deletion.
128+
129+
For an example implementation, see this Wiki page:
130+
https://github.com/sqlalchemy/sqlalchemy/wiki/Views
131+
132+
133+
Parameters
134+
----------
135+
name : str
136+
The name of the view to be created.
137+
metadata : MetaData
138+
The MetaData object that contains the schema and is used to listen for
139+
the `after_create` and `before_drop` events.
140+
selectable : Select[Any]
141+
A table that defines the content of the view.
142+
"""
143+
# Create the view after all tables are created:.
79144
sqlalchemy.event.listen(
80145
metadata,
81146
"after_create",
82147
CreateView(name, selectable).execute_if(callable_=view_doesnt_exist),
83148
)
149+
150+
# Ensure the view is dropped before any tables in the database are dropped.
84151
sqlalchemy.event.listen(metadata, "before_drop", DropView(name).execute_if(callable_=view_exists))
85-
return view

0 commit comments

Comments
 (0)