-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(robot-server): add protocol_kind to the protocols endpoint and p…
…ersistance layer. (#15353)
- Loading branch information
Showing
27 changed files
with
671 additions
and
17 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
robot-server/robot_server/persistence/_migrations/v4_to_v5.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Migrate the persistence directory from schema 4 to 5. | ||
Summary of changes from schema 4: | ||
- Adds a new "protocol_kind" column to protocols table | ||
""" | ||
|
||
from pathlib import Path | ||
from contextlib import ExitStack | ||
import shutil | ||
from typing import Any | ||
|
||
import sqlalchemy | ||
|
||
from ..database import sql_engine_ctx | ||
from ..tables import schema_5 | ||
from .._folder_migrator import Migration | ||
|
||
_DB_FILE = "robot_server.db" | ||
|
||
|
||
class Migration4to5(Migration): # noqa: D101 | ||
def migrate(self, source_dir: Path, dest_dir: Path) -> None: | ||
"""Migrate the persistence directory from schema 4 to 5.""" | ||
# Copy over all existing directories and files to new version | ||
for item in source_dir.iterdir(): | ||
if item.is_dir(): | ||
shutil.copytree(src=item, dst=dest_dir / item.name) | ||
else: | ||
shutil.copy(src=item, dst=dest_dir / item.name) | ||
dest_db_file = dest_dir / _DB_FILE | ||
|
||
# Append the new column to existing protocols in v4 database | ||
with ExitStack() as exit_stack: | ||
dest_engine = exit_stack.enter_context(sql_engine_ctx(dest_db_file)) | ||
schema_5.metadata.create_all(dest_engine) | ||
|
||
def add_column( | ||
engine: sqlalchemy.engine.Engine, | ||
table_name: str, | ||
column: Any, | ||
) -> None: | ||
column_type = column.type.compile(engine.dialect) | ||
engine.execute( | ||
f"ALTER TABLE {table_name} ADD COLUMN {column.key} {column_type}" | ||
) | ||
|
||
add_column( | ||
dest_engine, | ||
schema_5.protocol_table.name, | ||
schema_5.protocol_table.c.protocol_kind, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
robot-server/robot_server/persistence/tables/schema_5.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
"""v5 of our SQLite schema.""" | ||
|
||
import sqlalchemy | ||
|
||
from robot_server.persistence._utc_datetime import UTCDateTime | ||
|
||
metadata = sqlalchemy.MetaData() | ||
|
||
protocol_table = sqlalchemy.Table( | ||
"protocol", | ||
metadata, | ||
sqlalchemy.Column( | ||
"id", | ||
sqlalchemy.String, | ||
primary_key=True, | ||
), | ||
sqlalchemy.Column( | ||
"created_at", | ||
UTCDateTime, | ||
nullable=False, | ||
), | ||
sqlalchemy.Column("protocol_key", sqlalchemy.String, nullable=True), | ||
sqlalchemy.Column("protocol_kind", sqlalchemy.String, nullable=True), | ||
) | ||
|
||
analysis_table = sqlalchemy.Table( | ||
"analysis", | ||
metadata, | ||
sqlalchemy.Column( | ||
"id", | ||
sqlalchemy.String, | ||
primary_key=True, | ||
), | ||
sqlalchemy.Column( | ||
"protocol_id", | ||
sqlalchemy.String, | ||
sqlalchemy.ForeignKey("protocol.id"), | ||
index=True, | ||
nullable=False, | ||
), | ||
sqlalchemy.Column( | ||
"analyzer_version", | ||
sqlalchemy.String, | ||
nullable=False, | ||
), | ||
sqlalchemy.Column( | ||
"completed_analysis", | ||
# Stores a JSON string. See CompletedAnalysisStore. | ||
sqlalchemy.String, | ||
nullable=False, | ||
), | ||
# column added in schema v4 | ||
sqlalchemy.Column( | ||
"run_time_parameter_values_and_defaults", | ||
sqlalchemy.String, | ||
nullable=True, | ||
), | ||
) | ||
|
||
run_table = sqlalchemy.Table( | ||
"run", | ||
metadata, | ||
sqlalchemy.Column( | ||
"id", | ||
sqlalchemy.String, | ||
primary_key=True, | ||
), | ||
sqlalchemy.Column( | ||
"created_at", | ||
UTCDateTime, | ||
nullable=False, | ||
), | ||
sqlalchemy.Column( | ||
"protocol_id", | ||
sqlalchemy.String, | ||
sqlalchemy.ForeignKey("protocol.id"), | ||
nullable=True, | ||
), | ||
# column added in schema v1 | ||
sqlalchemy.Column( | ||
"state_summary", | ||
sqlalchemy.String, | ||
nullable=True, | ||
), | ||
# column added in schema v1 | ||
sqlalchemy.Column("engine_status", sqlalchemy.String, nullable=True), | ||
# column added in schema v1 | ||
sqlalchemy.Column("_updated_at", UTCDateTime, nullable=True), | ||
# column added in schema v4 | ||
sqlalchemy.Column( | ||
"run_time_parameters", | ||
# Stores a JSON string. See RunStore. | ||
sqlalchemy.String, | ||
nullable=True, | ||
), | ||
) | ||
|
||
action_table = sqlalchemy.Table( | ||
"action", | ||
metadata, | ||
sqlalchemy.Column( | ||
"id", | ||
sqlalchemy.String, | ||
primary_key=True, | ||
), | ||
sqlalchemy.Column("created_at", UTCDateTime, nullable=False), | ||
sqlalchemy.Column("action_type", sqlalchemy.String, nullable=False), | ||
sqlalchemy.Column( | ||
"run_id", | ||
sqlalchemy.String, | ||
sqlalchemy.ForeignKey("run.id"), | ||
nullable=False, | ||
), | ||
) | ||
|
||
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 | ||
), | ||
sqlalchemy.Column("index_in_run", sqlalchemy.Integer, nullable=False), | ||
sqlalchemy.Column("command_id", sqlalchemy.String, nullable=False), | ||
sqlalchemy.Column("command", sqlalchemy.String, nullable=False), | ||
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", | ||
"index_in_run", | ||
unique=True, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.