From 0f23794e23038aa2ef1d79eed6b4cd1fef8d743c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 8 Sep 2026 04:37:53 +0000 Subject: [PATCH] Fix tests to allow non psql database support --- .../InvenTree/stock/test_migrations.py | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/backend/InvenTree/stock/test_migrations.py b/src/backend/InvenTree/stock/test_migrations.py index 15c61bea8b..0a4bebb8ce 100644 --- a/src/backend/InvenTree/stock/test_migrations.py +++ b/src/backend/InvenTree/stock/test_migrations.py @@ -456,19 +456,23 @@ class TestCreationDateMigration(MigratorTestCase): Raw SQL also leaves updated=NULL (no DB-level default for auto_now), which 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: - cursor.execute( - """ - 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, '', '') - RETURNING id - """, - [part.pk], - ) - pk = cursor.fetchone()[0] + # MySQL has no RETURNING support at all (not even a syntax error + # workaround) - fall back to cursor.lastrowid there, and use + # RETURNING elsewhere since psycopg's cursor has no lastrowid. + if connection.features.can_return_rows_from_bulk_insert: + cursor.execute(insert_sql + 'RETURNING id', [part.pk]) + pk = cursor.fetchone()[0] + else: + cursor.execute(insert_sql, [part.pk]) + pk = cursor.lastrowid if stocktake_date is not None: cursor.execute( 'UPDATE stock_stockitem SET stocktake_date = %s WHERE id = %s', @@ -617,19 +621,24 @@ class TestRemoveMpttFieldsMigration(MigratorTestCase): def make_item(quantity, parent_id=None): """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: - cursor.execute( - """ - 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) - RETURNING id - """, - [part.pk, quantity, parent_id], - ) - return cursor.fetchone()[0] + # MySQL has no RETURNING support at all - fall back to + # cursor.lastrowid there, and use RETURNING elsewhere since + # psycopg's cursor has no lastrowid. + if connection.features.can_return_rows_from_bulk_insert: + cursor.execute( + insert_sql + 'RETURNING id', [part.pk, quantity, parent_id] + ) + return cursor.fetchone()[0] + cursor.execute(insert_sql, [part.pk, quantity, parent_id]) + return cursor.lastrowid # Root stock item, with no parent root_pk = make_item(100)