- Minor refactoring of Package::Migration.

- Added Package::Migration.linked to execute migrations of linked addons.
- Added Package::Migration.linked to spec_helper to ensure that addon migrations are executed.
This commit is contained in:
Thorsten Eckel 2017-03-08 15:59:48 +01:00
parent bd690f6039
commit 32fe702e56
2 changed files with 24 additions and 4 deletions

View file

@ -480,10 +480,21 @@ execute all pending package migrations at once
end end
class Migration < ApplicationModel class Migration < ApplicationModel
@@root = Rails.root.to_s # rubocop:disable Style/ClassVars
def self.linked
szpm_files = []
Dir.chdir(root) do
szpm_files = Dir['*.szpm']
end
szpm_files.each do |szpm_file|
package = szpm_file.sub('.szpm', '')
migrate(package)
end
end
def self.migrate(package, direction = 'normal') def self.migrate(package, direction = 'normal')
location = "#{@@root}/db/addon/#{package.underscore}" location = "#{root}/db/addon/#{package.underscore}"
return true if !File.exist?(location) return true if !File.exist?(location)
migrations_done = Package::Migration.where(name: package.underscore) migrations_done = Package::Migration.where(name: package.underscore)
@ -523,7 +534,7 @@ execute all pending package migrations at once
logger.info "NOTICE: down package migration '#{migration}'" logger.info "NOTICE: down package migration '#{migration}'"
load "#{location}/#{migration}" load "#{location}/#{migration}"
classname = name.camelcase classname = name.camelcase
Kernel.const_get(classname).down classname.constantize.down
record = Package::Migration.find_by(name: package.underscore, version: version) record = Package::Migration.find_by(name: package.underscore, version: version)
if record if record
record.destroy record.destroy
@ -536,10 +547,14 @@ execute all pending package migrations at once
logger.info "NOTICE: up package migration '#{migration}'" logger.info "NOTICE: up package migration '#{migration}'"
load "#{location}/#{migration}" load "#{location}/#{migration}"
classname = name.camelcase classname = name.camelcase
Kernel.const_get(classname).up classname.constantize.up
Package::Migration.create(name: package.underscore, version: version) Package::Migration.create(name: package.underscore, version: version)
end end
} }
end end
def self.root
Rails.root
end
end end
end end

View file

@ -22,6 +22,11 @@ RSpec.configure do |config|
# Zammad specific helpers # Zammad specific helpers
config.include ZammadHelper config.include ZammadHelper
config.before(:suite) do
# make sure that all migrations of linked packages are executed
Package::Migration.linked
end
# skip Zammad helper functions in the stacktrace to lower noise # skip Zammad helper functions in the stacktrace to lower noise
config.backtrace_exclusion_patterns << %r{/spec/spec_helper/} config.backtrace_exclusion_patterns << %r{/spec/spec_helper/}