-
Notifications
You must be signed in to change notification settings - Fork 33
Change how the total ordering of Orders is determined #196
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2c8c41b
Change how the total ordering of Orders is determined
ldionne 4d00a37
Use single SQL statement for updating ordinal
ldionne 71f3ee2
Use a UNIQUE constraint. This requires not handling SQLite (it
ldionne 94236c9
Restore SQLite support
ldionne 4de29a7
Merge branch 'main' into review/improve-orders
ldionne b8caf65
Don't drop the Next and Previous order tables on SQLite.
ldionne 244a8dd
Unify Postgres and SQLite paths and simplify the implementation
ldionne a30338b
More simplifications: Enforce that Orders always have an ordinal asso…
ldionne 8b86fa8
Remove unused argument
ldionne 54f2edd
Merge branch 'main' into review/improve-orders
ldionne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,116 @@ | ||
| """Add an ordinal column to each test suite's Order table. | ||
|
|
||
| On Postgres, also drops the PreviousOrder and NextOrder linked-list columns. | ||
| On SQLite, those columns are left in place because SQLite cannot drop columns | ||
| that are referenced by foreign key constraints. | ||
|
|
||
| The ordinal column stores the position of each order in the total ordering, | ||
| determined by sorting order field values via convert_revision(). | ||
| """ | ||
|
|
||
| import sqlalchemy | ||
| from sqlalchemy import Column, Integer, text | ||
|
|
||
| from lnt.server.db.migrations.util import introspect_table | ||
| from lnt.server.db.util import add_column | ||
| from lnt.server.ui.util import convert_revision | ||
|
|
||
|
|
||
| def upgrade(engine): | ||
| is_sqlite = engine.dialect.name == 'sqlite' | ||
|
|
||
| # Find all test suites so we can migrate each Order table. | ||
| test_suite_table = introspect_table(engine, "TestSuite") | ||
| order_fields_table = introspect_table(engine, "TestSuiteOrderFields") | ||
|
|
||
| session = sqlalchemy.orm.sessionmaker(engine)() | ||
|
|
||
| test_suites = session.execute( | ||
| sqlalchemy.select([test_suite_table.c.DBKeyName]) | ||
| ).fetchall() | ||
|
|
||
| for (db_key_name,) in test_suites: | ||
| order_table_name = f'{db_key_name}_Order' | ||
|
|
||
| # 1. Add ordinal column (nullable initially so we can backfill). | ||
| ordinal_col = Column("ordinal", Integer) | ||
| add_column(engine, order_table_name, ordinal_col) | ||
|
|
||
| # 2. Load all orders with their order field values, sort them, | ||
| # and assign ordinals. | ||
| order_table = introspect_table(engine, order_table_name) | ||
|
|
||
| # Find the order field names for this test suite, sorted by their | ||
| # ordinal (the field ordinal, not our new column). | ||
| ts_id_query = sqlalchemy.select( | ||
| [test_suite_table.c.ID] | ||
| ).where(test_suite_table.c.DBKeyName == db_key_name) | ||
| ts_id = session.execute(ts_id_query).scalar() | ||
|
|
||
| field_rows = session.execute( | ||
| sqlalchemy.select([order_fields_table.c.Name]) | ||
| .where(order_fields_table.c.TestSuiteID == ts_id) | ||
| .order_by(order_fields_table.c.Ordinal) | ||
| ).fetchall() | ||
| field_names = [row[0] for row in field_rows] | ||
|
|
||
| # Load all orders. | ||
| orders = session.execute( | ||
| sqlalchemy.select([order_table]) | ||
| ).fetchall() | ||
|
|
||
| # Build a sort key for each order using convert_revision on each | ||
| # order field. | ||
| cache = {} | ||
|
|
||
| def sort_key(row): | ||
| return tuple( | ||
| convert_revision( | ||
| getattr(row, fname) or '', cache=cache | ||
| ) | ||
| for fname in field_names | ||
| ) | ||
|
|
||
| sorted_orders = sorted(orders, key=sort_key) | ||
|
|
||
| # Assign ordinals. | ||
| for i, row in enumerate(sorted_orders): | ||
| session.execute( | ||
| order_table.update() | ||
| .where(order_table.c.ID == row.ID) | ||
| .values(ordinal=i) | ||
| ) | ||
|
|
||
| session.commit() | ||
|
|
||
| # 3. On Postgres: drop PreviousOrder and NextOrder columns, set ordinal | ||
| # NOT NULL, and add a deferred unique constraint. | ||
| # On SQLite: skip all of this. SQLite cannot drop columns that are | ||
| # referenced by foreign key constraints, and doesn't support ALTER | ||
| # COLUMN SET NOT NULL or DEFERRABLE constraints. The orphaned columns | ||
| # are harmless (SQLAlchemy ignores unmapped columns), and application | ||
| # logic enforces ordinal uniqueness. | ||
| if not is_sqlite: | ||
| with engine.begin() as conn: | ||
| for col in ["NextOrder", "PreviousOrder"]: | ||
| conn.execute(text( | ||
| f'ALTER TABLE "{order_table_name}" ' | ||
| f'DROP COLUMN "{col}"' | ||
| )) | ||
| conn.execute(text( | ||
| f'ALTER TABLE "{order_table_name}" ' | ||
| f'ALTER COLUMN "ordinal" SET NOT NULL' | ||
| )) | ||
| conn.execute(text( | ||
| f'ALTER TABLE "{order_table_name}" ' | ||
| f'ADD CONSTRAINT "{order_table_name}_ordinal_unique" ' | ||
| f'UNIQUE ("ordinal") DEFERRABLE INITIALLY DEFERRED' | ||
| )) | ||
|
|
||
| # 4. Create an index on the ordinal column for query performance. | ||
| new_order_table = introspect_table(engine, order_table_name) | ||
| idx_name = f"ix_{db_key_name}_Order_ordinal" | ||
| idx = sqlalchemy.Index(idx_name, new_order_table.c.ordinal) | ||
| idx.create(engine) | ||
|
|
||
| session.close() | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm getting an error that the ordinal column already exists when running this on a clean database, are you able to recreate this?
I don't see anything immediately wrong with the migration code. This is with creating a brand new database with 2cba11d, then rebuilding and recreating the container with 8b86fa8.
I will try to triple check later that I'm not doing something wrong on my end
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.
You're right, this issue seems real. I don't know why it didn't hit us before.
The problem seems to be that multiple gunicorn workers are attempting to perform the migration concurrently (there's nothing to prevent them all from trying to upgrade the DB when they start), and the ordinal column gets created multiple times.
I don't know why none of the other migrations have hit the same problem -- perhaps they would have but it's been so long since older DB versions are not around that it's not an issue in practice. Either way, I'll make a separate patch to fix this.
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.
#211 should fix this