-
Notifications
You must be signed in to change notification settings - Fork 16
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
New column entry to separate source from project url #2568
Merged
sesheta
merged 8 commits into
thoth-station:master
from
mayaCostantini:correct-url-entries
Mar 18, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1c7d19a
New column entry to separate source from project url
mayaCostantini 37881df
Alembic migration and rename source column to label
mayaCostantini dbc9014
Remove instructions from migration
mayaCostantini 44f9d05
Handle data in the migration
mayaCostantini 510e536
Fix migration
720b8bd
Pre-commit
mayaCostantini 69a61cf
Drop has_metadata_project_url column
mayaCostantini b0a3823
Update python_package_metadata_project_url without dropping the table
mayaCostantini 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
75 changes: 75 additions & 0 deletions
75
thoth/storages/data/alembic/versions/2b787ddad4a4_separate_the_project_url_column_from_.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,75 @@ | ||
"""Separate the project_url column from the python_package_metadata_project_url table into two columns label and url | ||
|
||
Revision ID: 2b787ddad4a4 | ||
Revises: 930b47e27b6c | ||
Create Date: 2022-01-31 16:26:38.049747+00:00 | ||
|
||
""" | ||
from alembic import op | ||
from sqlalchemy import MetaData | ||
|
||
import sqlalchemy as sa | ||
from thoth.storages.graph.models import PythonPackageMetadataProjectUrl | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "2b787ddad4a4" | ||
down_revision = "930b47e27b6c" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("python_package_metadata_project_url", sa.Column("url", sa.Text(), nullable=True)) | ||
op.add_column("python_package_metadata_project_url", sa.Column("label", sa.Text(), nullable=True)) | ||
|
||
python_package_metadata_project_url_table = sa.Table( | ||
"python_package_metadata_project_url", | ||
MetaData(), | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("project_url", sa.Text(), nullable=False), | ||
sa.Column("url", sa.Text(), nullable=True), | ||
sa.Column("label", sa.Text(), nullable=True), | ||
) | ||
|
||
connection = op.get_bind() | ||
|
||
results = connection.execute( | ||
sa.select( | ||
[python_package_metadata_project_url_table.c.id, python_package_metadata_project_url_table.c.project_url] | ||
) | ||
).fetchall() | ||
|
||
for result in results: | ||
id_ = result[0] | ||
label_url = result[1].split(",") | ||
if len(label_url) == 2: | ||
label, url = label_url[0].strip(), label_url[1].strip() | ||
connection.execute( | ||
python_package_metadata_project_url_table.update() | ||
.where(python_package_metadata_project_url_table.c.id == id_) | ||
.values({"label": label, "url": url}) | ||
) | ||
|
||
op.drop_column("python_package_metadata_project_url", "project_url") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
connection = op.get_bind() | ||
op.add_column( | ||
"python_package_metadata_project_url", sa.Column("project_url", sa.TEXT(), autoincrement=False, nullable=True) | ||
) | ||
result = connection.execute("SELECT id, label, url FROM python_package_metadata_project_url").fetchall() | ||
op.drop_column("python_package_metadata_project_url", "label") | ||
op.drop_column("python_package_metadata_project_url", "url") | ||
concat_label_url = [] | ||
for r in result: | ||
id_row = r[0] | ||
values_row = r[1] | ||
concat_label_url.append( | ||
{"id": id_row, "project_url": values_row[1] if values_row[0] is None else values_row.join(",")} | ||
) | ||
op.batch_alter_table().bulk_insert(PythonPackageMetadataProjectUrl.__table__, concat_label_url) | ||
# ### end Alembic commands ### |
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
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 realized we need to add a last command:
op.drop_column("has_metadata_project_url", "id")
to be consistent with the rest of the database.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.
Ok, will do 👍