Created
February 16, 2018 11:54
-
-
Save jacobwalkr/be1f760a41fd975c215855ab117fa7df to your computer and use it in GitHub Desktop.
Converts an ActsAsTaggableOn database structure for use with Gutentag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# adapted from Gutentag's install migrations | |
# https://github.com/pat/gutentag | |
superclass = ActiveRecord::VERSION::MAJOR < 5 ? | |
ActiveRecord::Migration : ActiveRecord::Migration[4.2] | |
class ConvertToGutentag < superclass | |
def up | |
## taggings | |
# delete indexes | |
ActiveRecord::Base.connection.indexes(:taggings).map(&:name).uniq.each do |index| | |
remove_index(:taggings, name: index) unless index == 'PRIMARY' | |
end | |
rename_table :taggings, :gutentag_taggings | |
# Gutentag uses t.timestamps, while AATO just uses created_at | |
add_column :gutentag_taggings, :updated_at, :datetime, null: false | |
Gutentag::Tagging.update_all('updated_at = created_at') | |
# no contexts or taggers in Gutentag | |
remove_column :gutentag_taggings, :tagger_id | |
remove_column :gutentag_taggings, :tagger_type | |
remove_column :gutentag_taggings, :context | |
# not set in AATO | |
change_column_null :gutentag_taggings, :tag_id, false | |
change_column_null :gutentag_taggings, :taggable_id, false | |
change_column_null :gutentag_taggings, :taggable_type, false | |
# rebuild the indexes Gutentag expects | |
add_index :gutentag_taggings, :tag_id | |
add_index :gutentag_taggings, [:taggable_type, :taggable_id] | |
add_index :gutentag_taggings, [:taggable_type, :taggable_id, :tag_id], unique: true, name: 'unique_taggings' | |
## tags | |
remove_index :tags, :name | |
rename_table :tags, :gutentag_tags | |
change_column_null :gutentag_tags, :name, false | |
change_column_null :gutentag_tags, :taggings_count, false | |
# no timestamps at all on AATO tags | |
add_timestamps :gutentag_tags, null: false | |
add_index :gutentag_tags, :name, unique: true | |
add_index :gutentag_tags, :taggings_count | |
end | |
def down | |
raise ActiveRecord::IrreversibleMigration | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment