Class: CopyDb::LoadDb

Inherits:
Object
  • Object
show all
Defined in:
lib/copydb.rb

Instance Method Summary collapse

Instance Method Details

#loadObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/copydb.rb', line 83

def load
  if FileTest.exists?(File.expand_path('db/copydb_dumped_data.yml'))

    yml = YAML.load_file(File.expand_path('db/copydb_dumped_data.yml'))

    yml.each_with_index do |entry,i|
      if i == 0
        unless entry.to_s == self.schema_version.to_s
          puts "ERROR: schema version mismatch"
        end
        next
      end
      unless entry[1].nil?
        ActiveRecord::Base.connection.begin_db_transaction
        columns = entry[1].each_key.to_a
        quoted_column_names = columns.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
        
        for i in 1..(entry.length-1)
          entries = entry[i].each_value.to_a
          quoted_column_values = entries.map { |record| ActiveRecord::Base.connection.quote(record) }.join(',')

   ActiveRecord::Base.connection.execute("ALTER TABLE #{entry[0]} DISABLE TRIGGER ALL;")
          
          sql_string = "INSERT INTO #{entry[0]} (#{quoted_column_names}) VALUES (#{quoted_column_values});"
          ActiveRecord::Base.connection.execute(sql_string)

   ActiveRecord::Base.connection.execute("ALTER TABLE #{entry[0]} ENABLE TRIGGER ALL;")
        end            
 ActiveRecord::Base.connection.commit_db_transaction
      end
    end
  else
    puts "ERROR: dump file not found"
    exit 1
  end
end

#schema_versionObject



120
121
122
# File 'lib/copydb.rb', line 120

def schema_version
   ActiveRecord::Migrator.current_version
end