Skip to content

Commit

Permalink
Move SchemaMigration model from ManageIQ to ManageIQ::Schema plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Feb 28, 2018
1 parent b67301c commit fec46b1
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions app/models/schema_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class SchemaMigration < ActiveRecord::Base
def self.up_to_date?
begin
migrations = missing_db_migrations
files = missing_file_migrations
db_ver = schema_version
rescue => err
return [false, err]
end

return [false, "database schema is not up to date. Schema version is [#{db_ver}]. Missing migrations: [#{migrations.join(", ")}]",
"database should be migrated to the latest version"] unless migrations.empty?
return [false, "database schema is from a newer version of the product and may be incompatible. Schema version is [#{db_ver}]. Missing files: [#{files.join(", ")}]",
"appliance should be updated to match database version"] unless files.empty?

[true, "database schema version #{db_ver} is up to date"]
end

def self.db_migration_list
@db_migration_list ||= SchemaMigration.all.collect { |s| s.version.to_i }.sort
end

def self.file_migration_list
@file_migration_list ||= Dir.glob(ManageIQ::Schema::Engine.root.join("db", "migrate", "*.rb")).collect do |f|
File.basename(f).split("_")[0].to_i
end.sort
end

def self.missing_db_migrations
file_migration_list - db_migration_list
end

def self.missing_file_migrations
# Ignore migrations prior to the collapsed initial migration
db_migration_list.reject { |m| m < initial_migration } - file_migration_list
end

def self.schema_version
db_migration_list.last
end

def self.initial_migration
file_migration_list.first
end

def self.latest_migration
file_migration_list.last
end
end

0 comments on commit fec46b1

Please sign in to comment.