Skip to content

Commit 5b91cbc

Browse files
committed
Add tagging migrations
1 parent 1e55324 commit 5b91cbc

7 files changed

+156
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
# This migration comes from acts_as_taggable_on_engine (originally 1)
4+
class ActsAsTaggableOnMigration < ActiveRecord::Migration[6.0]
5+
def self.up
6+
create_table ActsAsTaggableOn.tags_table do |t|
7+
t.string :name
8+
t.timestamps
9+
end
10+
11+
create_table ActsAsTaggableOn.taggings_table do |t|
12+
t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table }
13+
14+
# You should make sure that the column created is
15+
# long enough to store the required class names.
16+
t.references :taggable, polymorphic: true
17+
t.references :tagger, polymorphic: true
18+
19+
# Limit is created to prevent MySQL error on index
20+
# length for MyISAM table type: http://bit.ly/vgW2Ql
21+
t.string :context, limit: 128
22+
23+
t.datetime :created_at
24+
end
25+
26+
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context],
27+
name: 'taggings_taggable_context_idx'
28+
end
29+
30+
def self.down
31+
drop_table ActsAsTaggableOn.taggings_table
32+
drop_table ActsAsTaggableOn.tags_table
33+
end
34+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# This migration comes from acts_as_taggable_on_engine (originally 2)
4+
class AddMissingUniqueIndices < ActiveRecord::Migration[6.0]
5+
def self.up
6+
add_index ActsAsTaggableOn.tags_table, :name, unique: true
7+
8+
remove_index ActsAsTaggableOn.taggings_table, :tag_id if index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
9+
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
10+
add_index ActsAsTaggableOn.taggings_table,
11+
%i[tag_id taggable_id taggable_type context tagger_id tagger_type],
12+
unique: true, name: 'taggings_idx'
13+
end
14+
15+
def self.down
16+
remove_index ActsAsTaggableOn.tags_table, :name
17+
18+
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_idx'
19+
20+
add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
21+
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context],
22+
name: 'taggings_taggable_context_idx'
23+
end
24+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# This migration comes from acts_as_taggable_on_engine (originally 3)
4+
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration[6.0]
5+
def self.up
6+
add_column ActsAsTaggableOn.tags_table, :taggings_count, :integer, default: 0
7+
8+
ActsAsTaggableOn::Tag.reset_column_information
9+
ActsAsTaggableOn::Tag.find_each do |tag|
10+
ActsAsTaggableOn::Tag.reset_counters(tag.id, ActsAsTaggableOn.taggings_table)
11+
end
12+
end
13+
14+
def self.down
15+
remove_column ActsAsTaggableOn.tags_table, :taggings_count
16+
end
17+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# This migration comes from acts_as_taggable_on_engine (originally 4)
4+
class AddMissingTaggableIndex < ActiveRecord::Migration[6.0]
5+
def self.up
6+
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context],
7+
name: 'taggings_taggable_context_idx'
8+
end
9+
10+
def self.down
11+
remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# This migration comes from acts_as_taggable_on_engine (originally 5)
4+
# This migration is added to circumvent issue #623 and have special characters
5+
# work properly
6+
7+
class ChangeCollationForTagNames < ActiveRecord::Migration[6.0]
8+
def up
9+
if ActsAsTaggableOn::Utils.using_mysql?
10+
execute("ALTER TABLE #{ActsAsTaggableOn.tags_table} MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
11+
end
12+
end
13+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# This migration comes from acts_as_taggable_on_engine (originally 6)
4+
class AddMissingIndexesOnTaggings < ActiveRecord::Migration[6.0]
5+
def change
6+
add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists? ActsAsTaggableOn.taggings_table, :tag_id
7+
add_index ActsAsTaggableOn.taggings_table, :taggable_id unless index_exists? ActsAsTaggableOn.taggings_table,
8+
:taggable_id
9+
add_index ActsAsTaggableOn.taggings_table, :taggable_type unless index_exists? ActsAsTaggableOn.taggings_table,
10+
:taggable_type
11+
add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists? ActsAsTaggableOn.taggings_table,
12+
:tagger_id
13+
add_index ActsAsTaggableOn.taggings_table, :context unless index_exists? ActsAsTaggableOn.taggings_table, :context
14+
15+
unless index_exists? ActsAsTaggableOn.taggings_table, %i[tagger_id tagger_type]
16+
add_index ActsAsTaggableOn.taggings_table, %i[tagger_id tagger_type]
17+
end
18+
19+
unless index_exists? ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type tagger_id context],
20+
name: 'taggings_idy'
21+
add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type tagger_id context],
22+
name: 'taggings_idy'
23+
end
24+
end
25+
end

db/schema.rb

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)