Fix tests to allow non psql database support

This commit is contained in:
Oliver Walters
2026-09-08 04:37:53 +00:00
parent bd40007ba5
commit 0f23794e23
+33 -24
View File
@@ -456,19 +456,23 @@ class TestCreationDateMigration(MigratorTestCase):
Raw SQL also leaves updated=NULL (no DB-level default for auto_now), which Raw SQL also leaves updated=NULL (no DB-level default for auto_now), which
makes Scenario 6 a clean "no date sources available" case. makes Scenario 6 a clean "no date sources available" case.
""" """
insert_sql = """
INSERT INTO stock_stockitem
(part_id, quantity, level, tree_id, lft, rght,
status, delete_on_deplete, review_needed, is_building,
link, serial_int, barcode_data, barcode_hash)
VALUES (%s, 1, 0, 0, 0, 0, 10, false, false, false, '', 0, '', '')
"""
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute( # MySQL has no RETURNING support at all (not even a syntax error
""" # workaround) - fall back to cursor.lastrowid there, and use
INSERT INTO stock_stockitem # RETURNING elsewhere since psycopg's cursor has no lastrowid.
(part_id, quantity, level, tree_id, lft, rght, if connection.features.can_return_rows_from_bulk_insert:
status, delete_on_deplete, review_needed, is_building, cursor.execute(insert_sql + 'RETURNING id', [part.pk])
link, serial_int, barcode_data, barcode_hash) pk = cursor.fetchone()[0]
VALUES (%s, 1, 0, 0, 0, 0, 10, false, false, false, '', 0, '', '') else:
RETURNING id cursor.execute(insert_sql, [part.pk])
""", pk = cursor.lastrowid
[part.pk],
)
pk = cursor.fetchone()[0]
if stocktake_date is not None: if stocktake_date is not None:
cursor.execute( cursor.execute(
'UPDATE stock_stockitem SET stocktake_date = %s WHERE id = %s', 'UPDATE stock_stockitem SET stocktake_date = %s WHERE id = %s',
@@ -617,19 +621,24 @@ class TestRemoveMpttFieldsMigration(MigratorTestCase):
def make_item(quantity, parent_id=None): def make_item(quantity, parent_id=None):
"""Insert via raw SQL to avoid duplicate status_custom_key ORM bug.""" """Insert via raw SQL to avoid duplicate status_custom_key ORM bug."""
insert_sql = """
INSERT INTO stock_stockitem
(part_id, quantity, level, tree_id, lft, rght,
status, delete_on_deplete, is_building,
link, serial_int, barcode_data, barcode_hash, parent_id)
VALUES (%s, %s, 0, 0, 0, 0, 10, false, false, '', 0, '', '', %s)
"""
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute( # MySQL has no RETURNING support at all - fall back to
""" # cursor.lastrowid there, and use RETURNING elsewhere since
INSERT INTO stock_stockitem # psycopg's cursor has no lastrowid.
(part_id, quantity, level, tree_id, lft, rght, if connection.features.can_return_rows_from_bulk_insert:
status, delete_on_deplete, is_building, cursor.execute(
link, serial_int, barcode_data, barcode_hash, parent_id) insert_sql + 'RETURNING id', [part.pk, quantity, parent_id]
VALUES (%s, %s, 0, 0, 0, 0, 10, false, false, '', 0, '', '', %s) )
RETURNING id return cursor.fetchone()[0]
""", cursor.execute(insert_sql, [part.pk, quantity, parent_id])
[part.pk, quantity, parent_id], return cursor.lastrowid
)
return cursor.fetchone()[0]
# Root stock item, with no parent # Root stock item, with no parent
root_pk = make_item(100) root_pk = make_item(100)