Skip to content
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

Add embed columns to preview cards #5775

Merged
merged 1 commit into from
Dec 7, 2017
Merged

Conversation

akihikodaki
Copy link
Contributor

This resolves the problem described with #5763 (comment).

Note that the migration included in this change rebuilds the entire preview_cards table and takes long.
I'm not sure it is tolerated, but it is unlikely. If not, I will introduce additional changes.

@akihikodaki
Copy link
Contributor Author

codeclimate is complaining too many lines in a method, but this change just adds newlines and does not add statements. It is fine to ignore.

@@ -1,3 +1,3 @@
# frozen_string_literal: true

StrongMigrations.start_after = 20170924022025
StrongMigrations.start_after = 20171120000000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

end
end

add_column :preview_cards, :embed_url, :string, default: '', null: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should use Mastodon::MigrationHelpers#add_column_with_default that can add columns without locking.

end

dir.down do
execute 'UPDATE preview_cards SET url=embed_url WHERE embed_url IS NOT NULL'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since embed_url is not null, it seems that all urls will be changed.

end

add_column :preview_cards, :embed_url, :string, default: '', null: false
rename_column :preview_cards, :html, :embed_html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for changing column names? If you change the column name, saving the preview card will fail until restarting the process.

@akihikodaki
Copy link
Contributor Author

akihikodaki commented Nov 24, 2017

c692082600268e2bd12665dd5928e6a263900993 eliminates blocking when migrating in up direction.

@akihikodaki
Copy link
Contributor Author

deed5c1a79cadc9d1d8dac4c480f9eaa7e0e8492: rebased to e84fecb.

cleanup_concurrent_column_rename :preview_cards, :html, :embed_html
end

status_ids = Status.joins(:preview_cards)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to join Status. I think that this query is enough.

PreviewCard.where(type: 'photo').joins('JOIN preview_cards_statuses ON preview_cards_statuses.preview_card_id = preview_cards.id').pluck(:status_id)
SELECT "status_id" FROM "preview_cards" JOIN preview_cards_statuses ON preview_cards_statuses.preview_card_id = preview_cards.id WHERE "preview_cards"."type" = 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. However, I still prefer a query without any raw SQL code. It is not performance-critical and fine to have the little overhead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see.

cleanup_concurrent_column_rename :preview_cards, :height, :embed_height
cleanup_concurrent_column_rename :preview_cards, :width, :embed_width
cleanup_concurrent_column_rename :preview_cards, :html, :embed_html
execute 'UPDATE preview_cards SET url=embed_url WHERE embed_url IS NOT NULL'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since embed_url is not null, it seems that all urls will be changed. I think that should be limited to photo.

@Gargron
Copy link
Member

Gargron commented Nov 25, 2017

I understand embed_url (I think) but why rename html, width and height?

@akihikodaki
Copy link
Contributor Author

Because all of them are for embedding while other attributes are not but kind of summary.

@akihikodaki
Copy link
Contributor Author

akihikodaki commented Nov 27, 2017

My explanation may be a little insufficient so here is a bit more:
PostgreSQL does not allow to have an object-oriented representation (that is reasonable; it is an RDBMS), so we need to utilize the identifiers for the purpose.
Here, PreviewCard ideally can have these accessors: id, url, title, description, image, author, provider, created_at, updated_at, and embed which is added with this pull request.
author and provider is an object with two attributes. embed is polymorphic, and should be an object whose super class has width and height.
We are obligated to clarify such an intention especially when the underlying system is not that sophisticated.

@Gargron
Copy link
Member

Gargron commented Dec 6, 2017

We are obligated to clarify such an intention especially when the underlying system is not that sophisticated.

Okay, I understand your reasoning, but I still believe that adding a complicated long-running migration just to improve developer-visible column naming is unnecessary. Personally, embed_width and width makes no difference to me especially when there's only one of them in the end. You could just add a code comment instead.

safety_assured do
add_column_with_default :preview_cards, :embed_url, :string, default: '', allow_null: false

rename_column_concurrently :preview_cards, :html, :embed_html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to get rid of the column renames.

@akihikodaki
Copy link
Contributor Author

86ac0b047abd0b0263b8ee19cd1bad90ab617182: removed column renames and rebased to 9302369.

@Gargron Gargron merged commit c083816 into mastodon:master Dec 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants