Make migrations reversible

While I don't use this feature, there are developers who do. It's useful
when running migrations and changing branches.

I'm raising an `ActiveRecord::IrreversibleMigration` exception in every
`drop_table` migration because these migrations were all done before
version 1.0.0, and so making all of them reversible would be too much
work for little benefit.
This commit is contained in:
Javi Martín
2019-10-24 00:21:50 +02:00
parent a9359fd570
commit df959b74f6
35 changed files with 106 additions and 57 deletions

View File

@@ -36,9 +36,6 @@ Rails/HasManyOrHasOneDependent:
Rails/InverseOf:
Enabled: true
Rails/ReversibleMigration:
Enabled: true
Rails/SkipsModelValidations:
Enabled: true

View File

@@ -220,6 +220,9 @@ Rails/RelativeDateConstant:
Rails/RequestReferer:
Enabled: true
Rails/ReversibleMigration:
Enabled: true
Rails/SafeNavigation:
Enabled: true
ConvertTry: true

View File

@@ -1,5 +1,5 @@
class RemoveExternalLinkFromDebates < ActiveRecord::Migration[4.2]
def change
remove_column :debates, :external_link
remove_column :debates, :external_link, :string
end
end

View File

@@ -2,17 +2,17 @@ class RefactorVerificationColumns < ActiveRecord::Migration[4.2]
def change
rename_column :users, :sms_verification_code, :sms_confirmation_code
remove_column :users, :sms_verified_at
remove_column :users, :email_verified_at
remove_column :users, :email_for_verification
remove_column :users, :verified_user_sms_verified_at
remove_column :users, :sms_verified_at, :datetime
remove_column :users, :email_verified_at, :datetime
remove_column :users, :email_for_verification, :string
remove_column :users, :verified_user_sms_verified_at, :datetime
add_column :users, :verified_at, :datetime
remove_column :users, :phone
remove_column :users, :phone, :string
add_column :users, :unconfirmed_phone, :string
add_column :users, :confirmed_phone, :string
remove_column :users, :letter_requested
remove_column :users, :letter_requested, :boolean, default: false
add_column :users, :letter_requested_at, :datetime
rename_column :users, :sms_tries, :sms_confirmation_tries

View File

@@ -1,6 +1,6 @@
class RemoveFlaggedAsInappropiateAtFromCommentsAndDebates < ActiveRecord::Migration[4.2]
def change
remove_column :debates, :flagged_as_inappropiate_at
remove_column :comments, :flagged_as_inappropiate_at
remove_column :debates, :flagged_as_inappropiate_at, :datetime
remove_column :comments, :flagged_as_inappropiate_at, :datetime
end
end

View File

@@ -1,9 +1,9 @@
class MergeActivitiesAndNotifications < ActiveRecord::Migration[4.2]
def change
change_table :notifications do |t|
t.remove :read
t.remove :activity_id
t.references :notifiable, polymorphic: true
end
remove_column :notifications, :read, :boolean, default: false
remove_index :notifications, column: [:activity_id]
remove_column :notifications, :activity_id, :integer
add_reference :notifications, :notifiable, polymorphic: true
end
end

View File

@@ -1,5 +1,5 @@
class RemovesExternalLinkFromDebates < ActiveRecord::Migration[4.2]
def change
remove_column :debates, :external_link
remove_column :debates, :external_link, :string
end
end

View File

@@ -1,5 +1,9 @@
class DestroyCaptchaTable < ActiveRecord::Migration[4.2]
def change
def up
drop_table :simple_captcha_data
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,7 +1,7 @@
class ReplaceGeozonesByHeadingsInBudgets < ActiveRecord::Migration[4.2]
def change
remove_column :budget_investments, :geozone_id
remove_column :budget_ballots, :geozone_id
remove_column :budget_investments, :geozone_id, :integer
remove_column :budget_ballots, :geozone_id, :integer
add_reference :budget_investments, :heading, index: true
add_reference :budget_ballots, :heading, index: true

View File

@@ -1,5 +1,5 @@
class DeletesHeadingIdFromBallot < ActiveRecord::Migration[4.2]
def change
remove_column :budget_ballots, :heading_id
remove_column :budget_ballots, :heading_id, :integer, index: true
end
end

View File

@@ -1,5 +1,5 @@
class RemoveQuestionFromPollQuestions < ActiveRecord::Migration[4.2]
def change
remove_column :poll_questions, :question
remove_column :poll_questions, :question, :string
end
end

View File

@@ -1,8 +1,9 @@
class DeleteOfficingBooths < ActiveRecord::Migration[4.2]
def self.up
def up
drop_table :poll_officing_booths
end
def self.down
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,5 @@
class RemovePhysicalVotesFromProposals < ActiveRecord::Migration[4.2]
def change
remove_column :proposals, :physical_votes
remove_column :proposals, :physical_votes, :integer, default: 0
end
end

View File

@@ -1,5 +1,5 @@
class RemoveGeozoneIdFromBudgetHeadings < ActiveRecord::Migration[4.2]
def change
remove_column :budget_headings, :geozone_id
remove_column :budget_headings, :geozone_id, :integer
end
end

View File

@@ -1,5 +1,5 @@
class MoveGeozonesFromPollQuestionsToPolls < ActiveRecord::Migration[4.2]
def change
def up
drop_table :geozones_poll_questions
create_table :geozones_polls do |t|
@@ -7,4 +7,9 @@ class MoveGeozonesFromPollQuestionsToPolls < ActiveRecord::Migration[4.2]
t.references :poll, index: true, foreign_key: true
end
end
def down
drop_table :geozones_polls
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,5 @@
class RemoveSummaryFromPollQuestion < ActiveRecord::Migration[4.2]
def change
remove_column :poll_questions, :summary
remove_column :poll_questions, :summary, :string
end
end

View File

@@ -1,5 +1,5 @@
class RemoveTolk < ActiveRecord::Migration[4.2]
def change
def up
remove_index :tolk_translations, column: [:phrase_id, :locale_id]
remove_index :tolk_locales, column: :name
@@ -7,4 +7,8 @@ class RemoveTolk < ActiveRecord::Migration[4.2]
drop_table :tolk_phrases
drop_table :tolk_locales
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,6 @@
class RemoveLocalCensusRecordUserId < ActiveRecord::Migration[4.2]
def change
remove_column :local_census_records, :user_id
remove_foreign_key :local_census_records, :users
remove_column :local_census_records, :user_id, :integer
end
end

View File

@@ -1,8 +1,12 @@
class RemovePollRecount < ActiveRecord::Migration[4.2]
def change
def up
remove_index :poll_recounts, column: [:booth_assignment_id]
remove_index :poll_recounts, column: [:officer_assignment_id]
drop_table :poll_recounts
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,5 @@
class RemovePollFinalRecounts < ActiveRecord::Migration[4.2]
def change
def up
remove_index :poll_final_recounts, column: :booth_assignment_id
remove_index :poll_final_recounts, column: :officer_assignment_id
@@ -8,4 +8,8 @@ class RemovePollFinalRecounts < ActiveRecord::Migration[4.2]
drop_table :poll_final_recounts
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,5 @@
class RemovePollWhiteNullTotalResults < ActiveRecord::Migration[4.2]
def change
def up
remove_index :poll_null_results, column: [:booth_assignment_id]
remove_index :poll_null_results, column: [:officer_assignment_id]
@@ -13,4 +13,8 @@ class RemovePollWhiteNullTotalResults < ActiveRecord::Migration[4.2]
drop_table :poll_total_results
drop_table :poll_white_results
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,7 +1,7 @@
class AddDefaultToRecountAmounts < ActiveRecord::Migration[4.2]
def change
change_column_default :poll_recounts, :white_amount, 0
change_column_default :poll_recounts, :null_amount, 0
change_column_default :poll_recounts, :total_amount, 0
change_column_default :poll_recounts, :white_amount, from: nil, to: 0
change_column_default :poll_recounts, :null_amount, from: nil, to: 0
change_column_default :poll_recounts, :total_amount, from: nil, to: 0
end
end

View File

@@ -1,5 +1,5 @@
class RemoveDescriptionFromPollQuestions < ActiveRecord::Migration[4.2]
def change
remove_column :poll_questions, :description
remove_column :poll_questions, :description, :text
end
end

View File

@@ -1,5 +1,5 @@
class RemovePollQuestionValidAnswers < ActiveRecord::Migration[4.2]
def change
remove_column :poll_questions, :valid_answers
remove_column :poll_questions, :valid_answers, :string
end
end

View File

@@ -1,6 +1,11 @@
class AddTimeZoneToDefaultDatetimes < ActiveRecord::Migration[4.2]
def change
change_column_default :users, :password_changed_at, DateTime.new(2015, 1, 1, 1, 1, 1, "+00:00")
change_column_default :locks, :locked_until, DateTime.new(2000, 1, 1, 1, 1, 1, "+00:00")
change_column_default :users, :password_changed_at,
from: Time.zone.local(2015, 1, 1, 1, 1, 1),
to: DateTime.new(2015, 1, 1, 1, 1, 1, "+00:00")
change_column_default :locks, :locked_until,
from: Time.zone.local(2000, 1, 1, 1, 1, 1),
to: DateTime.new(2000, 1, 1, 1, 1, 1, "+00:00")
end
end

View File

@@ -1,5 +1,5 @@
class RemoveRelatedContentsFlagsCount < ActiveRecord::Migration[4.2]
def change
remove_column :related_contents, :flags_count
remove_column :related_contents, :flags_count, :integer
end
end

View File

@@ -1,6 +1,6 @@
class RemoveImageAndStyleFromBanners < ActiveRecord::Migration[4.2]
def change
remove_column :banners, :image
remove_column :banners, :style
remove_column :banners, :image, :string
remove_column :banners, :style, :string
end
end

View File

@@ -1,5 +1,5 @@
class RemoveInternalCommentsFromInvestment < ActiveRecord::Migration[4.2]
def change
remove_column :budget_investments, :internal_comments
remove_column :budget_investments, :internal_comments, :text
end
end

View File

@@ -1,6 +1,6 @@
class EnableRecommendationsByDefault < ActiveRecord::Migration[4.2]
def change
change_column_default :users, :recommended_debates, true
change_column_default :users, :recommended_proposals, true
change_column_default :users, :recommended_debates, from: false, to: true
change_column_default :users, :recommended_proposals, from: false, to: true
end
end

View File

@@ -1,5 +1,9 @@
class DropAnnotationsTable < ActiveRecord::Migration[4.2]
def change
def up
drop_table :annotations
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,9 @@
class DropLegacyLegislationsTable < ActiveRecord::Migration[4.2]
def change
def up
drop_table :legacy_legislations
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,9 @@
class DestroySpendingProposals < ActiveRecord::Migration[4.2]
def change
def up
drop_table :spending_proposals
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,5 +1,9 @@
class DestroySpendingProposalValuations < ActiveRecord::Migration[4.2]
def change
def up
drop_table :valuation_assignments
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

View File

@@ -1,6 +1,7 @@
class DestroySpendingProposalAssociations < ActiveRecord::Migration[4.2]
def change
remove_column :tags, :spending_proposals_count
remove_column :valuators, :spending_proposals_count
remove_index :tags, :spending_proposals_count
remove_column :tags, :spending_proposals_count, :integer, default: 0
remove_column :valuators, :spending_proposals_count, :integer, default: 0
end
end

View File

@@ -1,8 +1,8 @@
class RemoveHtmlFieldsFormLegislationDraftVersion < ActiveRecord::Migration[5.0]
def change
remove_column :legislation_draft_versions, :body_html
remove_column :legislation_draft_version_translations, :body_html
remove_column :legislation_draft_versions, :toc_html
remove_column :legislation_draft_version_translations, :toc_html
remove_column :legislation_draft_versions, :body_html, :text
remove_column :legislation_draft_version_translations, :body_html, :text
remove_column :legislation_draft_versions, :toc_html, :text
remove_column :legislation_draft_version_translations, :toc_html, :text
end
end