|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Combined migration for acts_as_taggable_on setup |
| 4 | +# Combines migrations 1-7 from acts_as_taggable_on_engine |
| 5 | +class SetupActsAsTaggableOn < ActiveRecord::Migration[7.1] |
| 6 | + def up |
| 7 | + # Create tags table |
| 8 | + unless table_exists?(ActsAsTaggableOn.tags_table) |
| 9 | + create_table ActsAsTaggableOn.tags_table do |t| |
| 10 | + t.string :name |
| 11 | + t.integer :taggings_count, default: 0 |
| 12 | + t.timestamps |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + # Add unique index on tag name |
| 17 | + unless index_exists?(ActsAsTaggableOn.tags_table, :name) |
| 18 | + add_index ActsAsTaggableOn.tags_table, :name, unique: true |
| 19 | + end |
| 20 | + |
| 21 | + # Create taggings table |
| 22 | + unless table_exists?(ActsAsTaggableOn.taggings_table) |
| 23 | + create_table ActsAsTaggableOn.taggings_table do |t| |
| 24 | + t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table } |
| 25 | + t.references :taggable, polymorphic: true |
| 26 | + t.references :tagger, polymorphic: true |
| 27 | + t.string :context, limit: 128 |
| 28 | + t.string :tenant, limit: 128 |
| 29 | + t.datetime :created_at |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # Add all indexes |
| 34 | + add_index ActsAsTaggableOn.taggings_table, |
| 35 | + %i[tag_id taggable_id taggable_type context tagger_id tagger_type], |
| 36 | + unique: true, name: "taggings_idx" unless index_exists?(ActsAsTaggableOn.taggings_table, "taggings_idx", name: true) |
| 37 | + |
| 38 | + add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context], |
| 39 | + name: "taggings_taggable_context_idx" unless index_exists?(ActsAsTaggableOn.taggings_table, "taggings_taggable_context_idx", name: true) |
| 40 | + |
| 41 | + add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tag_id) |
| 42 | + add_index ActsAsTaggableOn.taggings_table, :taggable_id unless index_exists?(ActsAsTaggableOn.taggings_table, :taggable_id) |
| 43 | + add_index ActsAsTaggableOn.taggings_table, :taggable_type unless index_exists?(ActsAsTaggableOn.taggings_table, :taggable_type) |
| 44 | + add_index ActsAsTaggableOn.taggings_table, :tagger_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tagger_id) |
| 45 | + add_index ActsAsTaggableOn.taggings_table, :context unless index_exists?(ActsAsTaggableOn.taggings_table, :context) |
| 46 | + add_index ActsAsTaggableOn.taggings_table, %i[tagger_id tagger_type] unless index_exists?(ActsAsTaggableOn.taggings_table, %i[tagger_id tagger_type]) |
| 47 | + add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type tagger_id context], |
| 48 | + name: "taggings_idy" unless index_exists?(ActsAsTaggableOn.taggings_table, "taggings_idy", name: true) |
| 49 | + add_index ActsAsTaggableOn.taggings_table, :tenant unless index_exists?(ActsAsTaggableOn.taggings_table, :tenant) |
| 50 | + |
| 51 | + # Change collation for MySQL |
| 52 | + return unless ActsAsTaggableOn::Utils.using_mysql? |
| 53 | + return if table_exists?(ActsAsTaggableOn.tags_table) && column_exists?(ActsAsTaggableOn.tags_table, :name) |
| 54 | + |
| 55 | + execute("ALTER TABLE #{ActsAsTaggableOn.tags_table} MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;") |
| 56 | + end |
| 57 | + |
| 58 | + def down |
| 59 | + # Depdending on your needs, you might want to implement the down method. |
| 60 | + # Uncomment the lines below for new installations where rollback is necessary. |
| 61 | + # drop_table ActsAsTaggableOn.taggings_table |
| 62 | + # drop_table ActsAsTaggableOn.tags_table |
| 63 | + raise ActiveRecord::IrreversibleMigration |
| 64 | + end |
| 65 | +end |
0 commit comments