Skip to content

Commit

Permalink
Use a synthetic primary key.
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxColoring committed Jan 26, 2024
1 parent 5c4b24c commit 0e8d735
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion robot-server/robot_server/persistence/_tables/schema_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand All @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions robot-server/tests/persistence/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
""",
]
Expand Down Expand Up @@ -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)

0 comments on commit 0e8d735

Please sign in to comment.