2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2019-09-27 07:22:16 +00:00
|
|
|
|
|
|
|
class Package::Migration < ApplicationModel
|
|
|
|
|
|
|
|
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')
|
|
|
|
location = "#{root}/db/addon/#{package.underscore}"
|
|
|
|
|
|
|
|
return true if !File.exist?(location)
|
|
|
|
|
|
|
|
# get existing migrations
|
|
|
|
migrations_existing = []
|
|
|
|
Dir.foreach(location) do |entry|
|
|
|
|
next if entry == '.'
|
|
|
|
next if entry == '..'
|
|
|
|
|
|
|
|
migrations_existing.push entry
|
|
|
|
end
|
|
|
|
|
|
|
|
# up
|
|
|
|
migrations_existing = migrations_existing.sort
|
|
|
|
|
|
|
|
# down
|
|
|
|
if direction == 'reverse'
|
|
|
|
migrations_existing = migrations_existing.reverse
|
|
|
|
end
|
|
|
|
|
|
|
|
migrations_existing.each do |migration|
|
2020-05-25 07:05:17 +00:00
|
|
|
next if !migration.end_with?('.rb')
|
2019-09-27 07:22:16 +00:00
|
|
|
|
|
|
|
version = nil
|
|
|
|
name = nil
|
2021-05-12 11:37:44 +00:00
|
|
|
if migration =~ %r{^(.+?)_(.*)\.rb$}
|
2019-09-27 07:22:16 +00:00
|
|
|
version = $1
|
|
|
|
name = $2
|
|
|
|
end
|
|
|
|
if !version || !name
|
|
|
|
raise "Invalid package migration '#{migration}'"
|
|
|
|
end
|
|
|
|
|
|
|
|
# down
|
|
|
|
done = Package::Migration.find_by(name: package.underscore, version: version)
|
|
|
|
if direction == 'reverse'
|
|
|
|
next if !done
|
|
|
|
|
|
|
|
logger.info "NOTICE: down package migration '#{migration}'"
|
|
|
|
load "#{location}/#{migration}"
|
|
|
|
classname = name.camelcase
|
|
|
|
classname.constantize.down
|
|
|
|
record = Package::Migration.find_by(name: package.underscore, version: version)
|
|
|
|
record&.destroy
|
|
|
|
|
|
|
|
# up
|
|
|
|
else
|
|
|
|
next if done
|
|
|
|
|
|
|
|
logger.info "NOTICE: up package migration '#{migration}'"
|
|
|
|
load "#{location}/#{migration}"
|
|
|
|
classname = name.camelcase
|
|
|
|
classname.constantize.up
|
|
|
|
Package::Migration.create(name: package.underscore, version: version)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.root
|
|
|
|
Rails.root
|
|
|
|
end
|
|
|
|
end
|