You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The conversion to SQLAlchemy 2.0 standards continues with a focus on upgrading the old queries to the new way of doing things. Thanks to the DAO principles used in this project, this entire change exists in basically 3 areas:
The app/dao directory, which contains the query code, isolated from the main project, which is extraordinarily convenient for our needs.
The tests directory, where some tests are using queries directly and need updating.
The migrations directory, which has some migrations files using the old queries and will need updating.
Note: SQLAlchemy 2.0 really leans heavily on explicit rather than implicit query structures. As such, you will likely need to explicitly define each join in queries rather than resting on the ORM components providing these for you implicitly.
For an example of a function already converted, go to: app/dao/fact_notification_status_dao.py, the fetch_notification_status_for_service_for_today_and_7_previous_days function.
The process to peform this upgrade is as follows:
Step 1: Refactor SELECT Statements
In SQLAlchemy 2.0, select is used instead of the deprecated query object. This applies to any SELECT operations in the DAOs.
Example: Refactor app/dao Module
Suppose you have a DAO method that retrieves a record based on its ID. The old way using query needs to be updated to the SQLAlchemy 2.0 syntax.
SQLAlchemy 2.0 encourages the use of explicit transactions when performing multiple operations. If needed, wrap multiple operations in a transaction block.
Example:
fromsqlalchemy.excimportIntegrityErrordefcreate_and_update_notifications(notification_data, notification_id, status):
try:
withdb.session.begin(): # This starts a transaction block# Create a new notificationstmt_insert=insert(Notification).values(notification_data)
db.session.execute(stmt_insert)
# Update an existing notificationstmt_update=update(Notification).where(Notification.id==notification_id).values(status=status)
db.session.execute(stmt_update)
exceptIntegrityError:
db.session.rollback()
raise
Step-by-Step Summary:
Replace query: Use select statements for querying/getting records.
Refactor Inserts: Use insert statements for adding records.
Refactor Updates: Use update for modifying records.
Refactor Deletes: Use delete statements for removing records.
Utility Functions: Any utility code interacting with the database should be upgraded to avoid the old query methods.
Transactions: Ensure correct use of transactions using db.session.begin() when performing multiple database operations.
The text was updated successfully, but these errors were encountered:
The conversion to SQLAlchemy 2.0 standards continues with a focus on upgrading the old queries to the new way of doing things. Thanks to the DAO principles used in this project, this entire change exists in basically 3 areas:
app/dao
directory, which contains the query code, isolated from the main project, which is extraordinarily convenient for our needs.tests
directory, where some tests are using queries directly and need updating.migrations
directory, which has some migrations files using the old queries and will need updating.Note: SQLAlchemy 2.0 really leans heavily on explicit rather than implicit query structures. As such, you will likely need to explicitly define each join in queries rather than resting on the ORM components providing these for you implicitly.
For an example of a function already converted, go to:
app/dao/fact_notification_status_dao.py
, thefetch_notification_status_for_service_for_today_and_7_previous_days
function.The process to peform this upgrade is as follows:
Step 1: Refactor
SELECT
StatementsIn SQLAlchemy 2.0,
select
is used instead of the deprecatedquery
object. This applies to anySELECT
operations in the DAOs.Example: Refactor
app/dao
ModuleSuppose you have a DAO method that retrieves a record based on its ID. The old way using
query
needs to be updated to the SQLAlchemy 2.0 syntax.Old Code:
Updated Code:
Explanation:
select
: Constructs a query.db.session.execute(stmt)
: Executes the query..scalars()
: Extracts the scalar values from the result..one_or_none()
: Fetches a single row or returnsNone
.Step 2: Refactor
INSERT
StatementsInserting new records now uses the
insert
function, where you directly create the insert statement and execute it.Old Code:
Updated Code:
Explanation:
insert(Notification)
: Creates an insert statement for theNotification
model..values(notification_data)
: Populates the insert statement with the provided data.Step 3: Refactor
UPDATE
StatementsTo update records, use the
update
statement and replace the old query-based updates.Old Code:
Updated Code:
Explanation:
update(Notification)
: Specifies that an update will be applied to theNotification
model..where(Notification.id == notification_id)
: Filters the update to only affect the row(s) with the specifiednotification_id
..values(status=status)
: Specifies the new value for thestatus
column.Step 4: Refactor
DELETE
StatementsFor deletions, use the
delete
statement.Old Code:
Updated Code:
Explanation:
delete(Notification)
: Constructs a delete statement for theNotification
model..where(Notification.id == notification_id)
: Ensures that only the record with the matching ID is deleted.Step 5: Refactor Utility Functions
If there are utility functions that interact with the database using
query
, those need to be refactored as well.Old Utility Code:
Updated Utility Code:
Step 6: Use Transactions Correctly
SQLAlchemy 2.0 encourages the use of explicit transactions when performing multiple operations. If needed, wrap multiple operations in a transaction block.
Example:
Step-by-Step Summary:
query
: Useselect
statements for querying/getting records.insert
statements for adding records.update
for modifying records.delete
statements for removing records.query
methods.db.session.begin()
when performing multiple database operations.The text was updated successfully, but these errors were encountered: