Skip to content

Commit

Permalink
chore: remove legacy migration syntax (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros authored Dec 5, 2021
1 parent e0f859e commit 6fbd9d1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 53 deletions.
12 changes: 5 additions & 7 deletions db/migrate/1_acts_as_taggable_on_migration.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]; end
else
class ActsAsTaggableOnMigration < ActiveRecord::Migration; end
end
ActsAsTaggableOnMigration.class_eval do
# frozen_string_literal: true

class ActsAsTaggableOnMigration < ActiveRecord::Migration[6.0]
def self.up
create_table ActsAsTaggableOn.tags_table do |t|
t.string :name
Expand All @@ -26,7 +23,8 @@ def self.up
end

add_index ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context],
name: 'taggings_taggable_context_idx'
end

def self.down
Expand Down
14 changes: 6 additions & 8 deletions db/migrate/2_add_missing_unique_indices.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingUniqueIndices < ActiveRecord::Migration[4.2]; end
else
class AddMissingUniqueIndices < ActiveRecord::Migration; end
end
AddMissingUniqueIndices.class_eval do
# frozen_string_literal: true

class AddMissingUniqueIndices < ActiveRecord::Migration[6.0]
def self.up
add_index ActsAsTaggableOn.tags_table, :name, unique: true

remove_index ActsAsTaggableOn.taggings_table, :tag_id if index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
add_index ActsAsTaggableOn.taggings_table,
[:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
%i[tag_id taggable_id taggable_type context tagger_id tagger_type],
unique: true, name: 'taggings_idx'
end

Expand All @@ -20,6 +17,7 @@ def self.down
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_idx'

add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context],
name: 'taggings_taggable_context_idx'
end
end
9 changes: 3 additions & 6 deletions db/migrate/3_add_taggings_counter_cache_to_tags.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration[4.2]; end
else
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration; end
end
AddTaggingsCounterCacheToTags.class_eval do
# frozen_string_literal: true

class AddTaggingsCounterCacheToTags < ActiveRecord::Migration[6.0]
def self.up
add_column ActsAsTaggableOn.tags_table, :taggings_count, :integer, default: 0

Expand Down
12 changes: 5 additions & 7 deletions db/migrate/4_add_missing_taggable_index.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingTaggableIndex < ActiveRecord::Migration[4.2]; end
else
class AddMissingTaggableIndex < ActiveRecord::Migration; end
end
AddMissingTaggableIndex.class_eval do
# frozen_string_literal: true

class AddMissingTaggableIndex < ActiveRecord::Migration[6.0]
def self.up
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context],
name: 'taggings_taggable_context_idx'
end

def self.down
Expand Down
10 changes: 4 additions & 6 deletions db/migrate/5_change_collation_for_tag_names.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# frozen_string_literal: true

# This migration is added to circumvent issue #623 and have special characters
# work properly
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class ChangeCollationForTagNames < ActiveRecord::Migration[4.2]; end
else
class ChangeCollationForTagNames < ActiveRecord::Migration; end
end
ChangeCollationForTagNames.class_eval do

class ChangeCollationForTagNames < ActiveRecord::Migration[6.0]
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE #{ActsAsTaggableOn.tags_table} MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
Expand Down
28 changes: 15 additions & 13 deletions db/migrate/6_add_missing_indexes_on_taggings.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddMissingIndexesOnTaggings < ActiveRecord::Migration[4.2]; end
else
class AddMissingIndexesOnTaggings < ActiveRecord::Migration; end
end
AddMissingIndexesOnTaggings.class_eval do
# frozen_string_literal: true

class AddMissingIndexesOnTaggings < ActiveRecord::Migration[6.0]
def change
add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists? ActsAsTaggableOn.taggings_table, :tag_id
add_index ActsAsTaggableOn.taggings_table, :taggable_id unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_id
add_index ActsAsTaggableOn.taggings_table, :taggable_type unless index_exists? ActsAsTaggableOn.taggings_table, :taggable_type
add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists? ActsAsTaggableOn.taggings_table, :tagger_id
add_index ActsAsTaggableOn.taggings_table, :taggable_id unless index_exists? ActsAsTaggableOn.taggings_table,
:taggable_id
add_index ActsAsTaggableOn.taggings_table, :taggable_type unless index_exists? ActsAsTaggableOn.taggings_table,
:taggable_type
add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists? ActsAsTaggableOn.taggings_table,
:tagger_id
add_index ActsAsTaggableOn.taggings_table, :context unless index_exists? ActsAsTaggableOn.taggings_table, :context

unless index_exists? ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
add_index ActsAsTaggableOn.taggings_table, [:tagger_id, :tagger_type]
unless index_exists? ActsAsTaggableOn.taggings_table, %i[tagger_id tagger_type]
add_index ActsAsTaggableOn.taggings_table, %i[tagger_id tagger_type]
end

unless index_exists? ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :tagger_id, :context], name: 'taggings_idy'
unless index_exists? ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type tagger_id context],
name: 'taggings_idy'
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type tagger_id context],
name: 'taggings_idy'
end
end
end
9 changes: 3 additions & 6 deletions db/migrate/7_add_tenant_to_taggings.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
class AddTenantToTaggings < ActiveRecord::Migration[4.2]; end
else
class AddTenantToTaggings < ActiveRecord::Migration; end
end
AddTenantToTaggings.class_eval do
# frozen_string_literal: true

class AddTenantToTaggings < ActiveRecord::Migration[6.0]
def self.up
add_column ActsAsTaggableOn.taggings_table, :tenant, :string, limit: 128
add_index ActsAsTaggableOn.taggings_table, :tenant unless index_exists? ActsAsTaggableOn.taggings_table, :tenant
Expand Down

0 comments on commit 6fbd9d1

Please sign in to comment.