Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install Gutentag migrations while installing Alchemy #1688

Merged
merged 1 commit into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/alchemy/upgrader/five_point_zero.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative 'tasks/harden_gutentag_migrations'
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved

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
29 changes: 29 additions & 0 deletions lib/alchemy/upgrader/tasks/harden_gutentag_migrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'thor'
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved

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
4 changes: 4 additions & 0 deletions lib/rails/generators/alchemy/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def copy_dragonfly_config
template "#{__dir__}/templates/dragonfly.rb.tt", "config/initializers/dragonfly.rb"
end

def install_gutentag_migrations
rake 'gutentag:install:migrations'
end

private

def config_path
Expand Down
15 changes: 15 additions & 0 deletions lib/tasks/alchemy/upgrade.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace :alchemy do

desc "Alchemy Upgrader: Prepares the database."
task database: [
'alchemy:upgrade:5.0:install_gutentag_migrations',
'alchemy:install:migrations',
'db:migrate',
'alchemy:db:seed'
Expand All @@ -27,5 +28,19 @@ namespace :alchemy do
task config: [:environment] do
Alchemy::Upgrader.copy_new_config_file
end

desc 'Upgrade Alchemy to v5.0'
task '5.0' => [
'alchemy:upgrade:prepare'
] do
Alchemy::Upgrader.display_todos
end

namespace '5.0' do
desc 'Install Gutentag migrations'
task install_gutentag_migrations: [:environment] do
Alchemy::Upgrader::FivePointZero.install_gutentag_migrations
end
end
end
end
35 changes: 35 additions & 0 deletions spec/dummy/db/migrate/20191129235819_gutentag_tables.gutentag.rb
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
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 spec/dummy/db/migrate/20191129235821_no_null_counters.gutentag.rb
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
4 changes: 2 additions & 2 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_10_29_212236) do
ActiveRecord::Schema.define(version: 2019_11_29_235821) do

create_table "alchemy_attachments", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -339,7 +339,7 @@

create_table "gutentag_tags", force: :cascade do |t|
t.string "name", null: false
t.integer "taggings_count", default: 0
t.integer "taggings_count", default: 0, null: false
t.index ["name"], name: "index_gutentag_tags_on_name", unique: true
t.index ["taggings_count"], name: "index_gutentag_tags_on_taggings_count"
end
Expand Down