-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(robot-server): Avoid features that will be removed in SQLAlchemy 2.0 #16926
base: EXEC-655-store-commands-error-list-in-db
Are you sure you want to change the base?
Changes from 3 commits
cf88696
afbe61e
f6b61b7
aa720f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
"""Shared helpers for migrations.""" | ||
|
||
import sqlalchemy | ||
|
||
import shutil | ||
import typing | ||
from pathlib import Path | ||
|
||
import sqlalchemy | ||
|
||
from ..database import sqlite_rowid | ||
|
||
|
||
|
@@ -54,3 +55,40 @@ def copytree_if_exists(src: Path, dst: Path) -> None: | |
shutil.copytree(src=src, dst=dst) | ||
except FileNotFoundError: | ||
pass | ||
|
||
|
||
def add_column( | ||
engine: sqlalchemy.engine.Engine, | ||
table_name: str, | ||
column: typing.Any, | ||
) -> None: | ||
"""Add a column to an existing SQL table, with an `ALTER TABLE` statement. | ||
|
||
Params: | ||
engine: A SQLAlchemy engine to connect to the database. | ||
table_name: The SQL name of the parent table. | ||
column: The SQLAlchemy column object. | ||
|
||
Known limitations: | ||
|
||
- This does not currently support indexes. | ||
- This does not currently support constraints. | ||
- The column will always be added as nullable. Adding non-nullable columns in | ||
SQLite requires an elaborate and sensitive dance that we do not wish to attempt. | ||
https://www.sqlite.org/lang_altertable.html#making_other_kinds_of_table_schema_changes | ||
|
||
To avoid those limitations, instead of this function, consider this: | ||
|
||
1. Start with an empty database, or drop or rename the current table. | ||
2. Use SQLAlchemy's `metadata.create_all()` to create an empty table with the new | ||
schema, including the new column. | ||
3. Copy rows from the old table to the new one, populating the new column | ||
however you please. | ||
""" | ||
column_type = column.type.compile(engine.dialect) | ||
with engine.begin() as transaction: | ||
transaction.execute( | ||
sqlalchemy.text( | ||
f"ALTER TABLE {table_name} ADD COLUMN {column.key} {column_type}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we use schemas here? Most of the SqlAlchemy code I've seen before passes around a tuple of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure this is what you mean by What would passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry, the word "schema" is way too overloaded in the programming world. For organizing big projects, databases like PostgreSQL let you create folders/directories/namespaces/whatever-you-want-to-call-them to divide up your data. PostgreSQL calls these folders "schemas" (which have nothing to do with the colloquial use of "schema" to refer to the shape of a table). And tables, enum definitions, server-side functions, etc., all live inside a schema. So to refer to a table in PostgreSQL, you would need its fully qualified name, like: The practical upshot is that in the code I worked on, whenever you pass a table name around, you would also need to pass the schema name around. https://docs.sqlalchemy.org/en/20/core/metadata.html#specifying-the-schema-name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aaaaah, gotcha, thank you. No, we don't use that kind of schema. Our .db file has only one, implicit, "main" schema. In SQLite, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oh neat! Then as a sidenote, I think that would let us solve the TODO in your |
||
) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, if you want to try something cute, folks online seem to recommend constructing the statement like this:
to have SqlAlchemy's compiler generate the column definition for you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, this is what Alembic is for.
But I wonder if there's a way to use Alembic as a fancy SQL generator for diffs without buying into the full Alembic ecosystem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is what we thought, but I looked into it when working on this, and I was underwhelmed. Alembic does implement this dance, and that is appealing. But:
First, like you said, there's a full Alembic ecosystem that we'd have to contend with. In particular, we would probably want to run Alembic "inside" our existing migration system. (It can't completely replace our own migration system, because we need to account for migrating regular files, and Alembic can only help with the schema inside the .db file.) Embedding it like that...seems messy? Like, one hypothetical way for it to work would be if Alembic gave us a standalone util function like
alembic.add_all_column_constraints(command_table.c.command_status)
, and we'd call that from within our existing system, but it doesn't seem like Alembic works like that.Second, as far as I can tell, Alembic leaves us on our own for the data part of these migrations, e.g. populating the new non-nullable column. They recommend some general patterns in sqlalchemy/alembic#972 (reply in thread) and https://alembic.sqlalchemy.org/en/latest/cookbook.html#data-migrations-general-techniques. Those patterns strike me as their own dances that are only marginally better. Especially if we need to rearchitect to fit into the Alembic ecosystem just for the privilege of using them.
But I could definitely have big misconceptions about all of this. I've never actually used Alembic for real. If you want to make a sketch or proof of concept to show what it would look like, I'd definitely love something better than what we have now.
Yeah. I'm not sure if this what you're getting at, but there is https://alembic.sqlalchemy.org/en/latest/autogenerate.html, and we might be able to combine that with https://alembic.sqlalchemy.org/en/latest/offline.html. So one option is to run Alembic once on our laptops to autogenerate the
ALTER TABLE
dance, and then manually integrate that with our data migrations. Is that what you have in mind?