Module: Goldberg::SchemaStatements

Defined in:
lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/migrator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



14
15
16
17
18
19
20
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/migrator.rb', line 14

def self.included(base) #:nodoc:
  base.class_eval do
    alias_method_chain :dump_schema_information, :plugins
    alias_method_chain :initialize_schema_migrations_table, :plugins
    alias_method_chain :assume_migrated_upto_version, :plugins
  end
end

Instance Method Details

#assume_migrated_upto_version_with_plugins(version) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/migrator.rb', line 61

def assume_migrated_upto_version_with_plugins(version)
  sm_table = Goldberg::Migrator.schema_migrations_table_name
  migration_path = "#{RAILS_ROOT}/vendor/plugins/#{Goldberg::Migrator.plugin_name}/db/migrate"
  
  migrated = select_values("SELECT version FROM #{sm_table} WHERE plugin_name=#{Goldberg::Migrator.plugin}").map(&:to_i)
  versions = Dir["#{migration_path}[0-9]*_*.rb"].map do |filename|
    filename.split('/').last.split('_').first.to_i
  end
  
  execute "INSERT INTO #{sm_table} (plugin_name, version) VALUES (#{Goldberg::Migrator.plugin}, '#{version}')" unless migrated.include?(version.to_i)
  (versions - migrated).select { |v| v < version.to_i }.each do |v|
    execute "INSERT INTO #{sm_table} (plugin_name, version) VALUES (#{Goldberg::Migrator.plugin}, '#{v}')"
  end
end

#dump_schema_information_with_pluginsObject

Dumps the plugin schema info table as well as information about the current plugin migrations



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/migrator.rb', line 24

def dump_schema_information_with_plugins
  begin
    sm_table = Goldberg::Migrator.schema_migrations_table_name
    migrated = select_all("SELECT version FROM #{sm_table}")
    migrated.map do |v|
      "INSERT INTO #{sm_table} (plugin_name, version) VALUES ('#{v['plugin_name']}', '#{v['version']}');"
    end.join("\n\n")
  rescue ActiveRecord::StatementInvalid 
    # No Schema Info
    ''
  end
end

#initialize_schema_migrations_table_with_pluginsObject

Creates the plugin schema info table



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/six-updater-web/vendor/plugins/goldberg/lib/goldberg/migrator.rb', line 38

def initialize_schema_migrations_table_with_plugins
  sm_table = Goldberg::Migrator.schema_migrations_table_name
  
  unless tables.detect { |t| t == sm_table }
    create_table(sm_table, :id => false) do |schema_migrations_table|
      schema_migrations_table.column :plugin_name, :string, :null => false
      schema_migrations_table.column :version, :string, :null => false
    end
    #add_index sm_table, [:plugin_name, :version], :unique => true,
    #:name => 'unique_schema_migrations'
    
    # Backwards-compatibility: if we find schema_info, assume we've
    # migrated up to that point:
    si_table = ActiveRecord::Base.table_name_prefix + 'plugin_schema_info' +
      ActiveRecord::Base.table_name_suffix
    if tables.detect { |t| t == si_table }
      old_version = select_value("SELECT version FROM #{quote_table_name(si_table)} WHERE plugin_name=#{Goldberg::Migrator.plugin}").to_i
      assume_migrated_upto_version(old_version)
      drop_table(si_table)
    end
  end
end