-
-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Install Gutentag migrations while installing Alchemy
For existing installations the upgrade task patches the Gutentag migration so they skip the tables if they already exist. While new installations run them.
- Loading branch information
Showing
8 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'tasks/harden_gutentag_migrations' | ||
|
||
module Alchemy | ||
class Upgrader::FivePointZero < Upgrader | ||
class << self | ||
def install_gutentag_migrations | ||
desc 'Install Gutentag migrations' | ||
`bundle exec rake gutentag:install:migrations` | ||
Alchemy::Upgrader::Tasks::HardenGutentagMigrations.new.patch_migrations | ||
`bundle exec rake db:migrate` | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'thor' | ||
|
||
module Alchemy::Upgrader::Tasks | ||
class HardenGutentagMigrations < Thor | ||
include Thor::Actions | ||
|
||
no_tasks do | ||
def patch_migrations | ||
sentinel = /def up/ | ||
|
||
migration_file = Dir.glob('db/migrate/*_gutentag_tables.gutentag.rb').first | ||
if migration_file | ||
inject_into_file migration_file, | ||
"\n # inserted by Alchemy CMS upgrader\n return if table_exists?(:gutentag_taggings)\n", | ||
{ after: sentinel, verbose: true } | ||
end | ||
|
||
migration_file = Dir.glob('db/migrate/*_gutentag_cache_counter.gutentag.rb').first | ||
if migration_file | ||
inject_into_file migration_file, | ||
"\n # inserted by Alchemy CMS upgrader\n return if column_exists?(:gutentag_tags, :taggings_count)\n", | ||
{ after: sentinel, verbose: true } | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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
35 changes: 35 additions & 0 deletions
35
spec/dummy/db/migrate/20191129235819_gutentag_tables.gutentag.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
# This migration comes from gutentag (originally 1) | ||
|
||
superclass = ActiveRecord::VERSION::MAJOR < 5 ? | ||
ActiveRecord::Migration : ActiveRecord::Migration[4.2] | ||
class GutentagTables < superclass | ||
def up | ||
# inserted by Alchemy CMS upgrader | ||
return if table_exists?(:gutentag_taggings) | ||
|
||
create_table :gutentag_taggings do |t| | ||
t.integer :tag_id, :null => false | ||
t.integer :taggable_id, :null => false | ||
t.string :taggable_type, :null => false | ||
t.timestamps :null => false | ||
end | ||
|
||
add_index :gutentag_taggings, :tag_id | ||
add_index :gutentag_taggings, %i[ taggable_type taggable_id ] | ||
add_index :gutentag_taggings, %i[ taggable_type taggable_id tag_id ], | ||
:unique => true, :name => "unique_taggings" | ||
|
||
create_table :gutentag_tags do |t| | ||
t.string :name, :null => false | ||
t.timestamps :null => false | ||
end | ||
|
||
add_index :gutentag_tags, :name, :unique => true | ||
end | ||
|
||
def down | ||
drop_table :gutentag_tags | ||
drop_table :gutentag_taggings | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
spec/dummy/db/migrate/20191129235820_gutentag_cache_counter.gutentag.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
# This migration comes from gutentag (originally 2) | ||
|
||
superclass = ActiveRecord::VERSION::MAJOR < 5 ? | ||
ActiveRecord::Migration : ActiveRecord::Migration[4.2] | ||
class GutentagCacheCounter < superclass | ||
def up | ||
# inserted by Alchemy CMS upgrader | ||
return if column_exists?(:gutentag_tags, :taggings_count) | ||
|
||
add_column :gutentag_tags, :taggings_count, :integer, :default => 0 | ||
add_index :gutentag_tags, :taggings_count | ||
|
||
Gutentag::Tag.reset_column_information | ||
Gutentag::Tag.pluck(:id).each do |tag_id| | ||
Gutentag::Tag.reset_counters tag_id, :taggings | ||
end | ||
end | ||
|
||
def down | ||
remove_column :gutentag_tags, :taggings_count | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
spec/dummy/db/migrate/20191129235821_no_null_counters.gutentag.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
# This migration comes from gutentag (originally 3) | ||
|
||
superclass = ActiveRecord::VERSION::MAJOR < 5 ? | ||
ActiveRecord::Migration : ActiveRecord::Migration[4.2] | ||
class NoNullCounters < superclass | ||
def up | ||
change_column :gutentag_tags, :taggings_count, :integer, | ||
:default => 0, | ||
:null => false | ||
end | ||
|
||
def down | ||
change_column :gutentag_tags, :taggings_count, :integer, | ||
:default => 0, | ||
:null => true | ||
end | ||
end |
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