From 98624e378906bca269e3ae64f0d0a9461b4ba959 Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Fri, 26 Jan 2024 13:02:27 -0500 Subject: [PATCH] Use a synthetic primary key. --- .../robot_server/persistence/_tables/schema_3.py | 8 +++++++- robot-server/tests/persistence/test_tables.py | 11 +++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/robot-server/robot_server/persistence/_tables/schema_3.py b/robot-server/robot_server/persistence/_tables/schema_3.py index 0f962c417d3b..b20b443b7540 100644 --- a/robot-server/robot_server/persistence/_tables/schema_3.py +++ b/robot-server/robot_server/persistence/_tables/schema_3.py @@ -113,6 +113,7 @@ run_command_table = sqlalchemy.Table( "run_command", metadata, + sqlalchemy.Column("row_id", sqlalchemy.Integer, primary_key=True), sqlalchemy.Column( "run_id", sqlalchemy.String, sqlalchemy.ForeignKey("run.id"), nullable=False ), @@ -125,7 +126,12 @@ sqlalchemy.PickleType(pickler=legacy_pickle, protocol=PICKLE_PROTOCOL_VERSION), nullable=False, ), - sqlalchemy.PrimaryKeyConstraint("run_id", "command_id"), + sqlalchemy.Index( + "ix_run_run_id_command_id", # An arbitrary name for the index. + "run_id", + "command_id", + unique=True, + ), sqlalchemy.Index( "ix_run_run_id_index_in_run", # An arbitrary name for the index. "run_id", diff --git a/robot-server/tests/persistence/test_tables.py b/robot-server/tests/persistence/test_tables.py index 68d92bca427f..c957da0e62ba 100644 --- a/robot-server/tests/persistence/test_tables.py +++ b/robot-server/tests/persistence/test_tables.py @@ -71,15 +71,19 @@ """, """ CREATE TABLE run_command ( + row_id INTEGER NOT NULL, run_id VARCHAR NOT NULL, index_in_run INTEGER NOT NULL, command_id VARCHAR NOT NULL, command BLOB NOT NULL, - PRIMARY KEY (run_id, command_id), + PRIMARY KEY (row_id), FOREIGN KEY(run_id) REFERENCES run (id) ) """, """ + CREATE UNIQUE INDEX ix_run_run_id_command_id ON run_command (run_id, command_id) + """, + """ CREATE UNIQUE INDEX ix_run_run_id_index_in_run ON run_command (run_id, index_in_run) """, ] @@ -190,4 +194,7 @@ def record_statement( normalized_actual = [_normalize_statement(s) for s in actual_statements] normalized_expected = [_normalize_statement(s) for s in expected_statements] - assert normalized_actual == normalized_expected + # Compare ignoring order. SQLAlchemy appears to emit CREATE INDEX statements in a + # nondeterministic order that varies across runs. Although statement order + # theoretically matters, it's unlikely to matter in practice for our purposes here. + assert set(normalized_actual) == set(normalized_expected)