diff --git a/robot-server/robot_server/persistence/_tables/schema_3.py b/robot-server/robot_server/persistence/_tables/schema_3.py index 5d27079ab79..e292af26b6b 100644 --- a/robot-server/robot_server/persistence/_tables/schema_3.py +++ b/robot-server/robot_server/persistence/_tables/schema_3.py @@ -125,6 +125,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 ), @@ -137,7 +138,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 36dc2ac52aa..b27ba5def6c 100644 --- a/robot-server/tests/persistence/test_tables.py +++ b/robot-server/tests/persistence/test_tables.py @@ -79,15 +79,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) """, ] @@ -198,4 +202,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)