Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dumping empty tables doesn't empty them when loading #49

Open
tovodeverett opened this issue Jun 8, 2013 · 0 comments
Open

Dumping empty tables doesn't empty them when loading #49

tovodeverett opened this issue Jun 8, 2013 · 0 comments

Comments

@tovodeverett
Copy link

It appears that yaml_db doesn't dump empty tables, which means that it doesn't know to truncate them when loading. This was definitely unexpected. It is fairly easy to resolve, but before I create a pull request (including extending the test suite to validate the behavior), I'd like to know if there's a consensus that this behavior should be changed. In case anyone else wants my monkey-patch in the interim, here is my monkey patch for 0.2.3:

module SerializationHelper
  class Load
    def self.load_records(table, column_names, records)
      if column_names.nil?
        return
      end
      columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}}
      quoted_column_names = column_names.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
      quoted_table_name = SerializationHelper::Utils.quote_table(table)
      (records || []).each do |record|
        quoted_values = record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',')
        ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})")
      end
    end
  end

  class Dump
    def self.dump_table(io, table)
      dump_table_columns(io, table)
      dump_table_records(io, table)
    end
  end
end

The modification to SerializationHelper::Load::load_records is to substitute an empty array for records if the latter is missing. The modification to SerializationHelper::Dump.dump_table is to eliminate the first line that returns if the table has no records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant