forked from ManageIQ/manageiq-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_migration.rb
49 lines (40 loc) · 1.56 KB
/
schema_migration.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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