pax_global_header00006660000000000000000000000064124255002000014500gustar00rootroot0000000000000052 comment=71cacaa4b571e64b4bf14d80709b374b390c5e5e yaml_db-0.3.0/000077500000000000000000000000001242550020000131075ustar00rootroot00000000000000yaml_db-0.3.0/.gitignore000066400000000000000000000000431242550020000150740ustar00rootroot00000000000000*.gem /.bundle/ /pkg/ Gemfile.lock yaml_db-0.3.0/.rspec000066400000000000000000000000361242550020000142230ustar00rootroot00000000000000--color --require spec_helper yaml_db-0.3.0/.travis.yml000066400000000000000000000011531242550020000152200ustar00rootroot00000000000000language: ruby rvm: - 1.8.7 - 1.9.2 - 1.9.3 - 2.0.0 - 2.1 env: - RAILS_VERSION='~> 3.0.0' - RAILS_VERSION='~> 3.1.0' - RAILS_VERSION='~> 3.2.0' - RAILS_VERSION='~> 4.0.0' - RAILS_VERSION='~> 4.1.0' - RAILS_VERSION='~> 4.2.0.beta' matrix: fast_finish: true exclude: - env: RAILS_VERSION='~> 4.0.0' rvm: 1.8.7 - env: RAILS_VERSION='~> 4.0.0' rvm: 1.9.2 - env: RAILS_VERSION='~> 4.1.0' rvm: 1.8.7 - env: RAILS_VERSION='~> 4.1.0' rvm: 1.9.2 - env: RAILS_VERSION='~> 4.2.0.beta' rvm: 1.8.7 - env: RAILS_VERSION='~> 4.2.0.beta' rvm: 1.9.2 yaml_db-0.3.0/Gemfile000066400000000000000000000001121242550020000143740ustar00rootroot00000000000000source 'https://rubygems.org' gemspec gem 'rails', ENV['RAILS_VERSION'] yaml_db-0.3.0/README.md000066400000000000000000000046011242550020000143670ustar00rootroot00000000000000# YamlDb YamlDb is a database-independent format for dumping and restoring data. It complements the the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml. This can be used as a replacement for mysqldump or pg_dump, but only for the databases typically used by Rails apps. Users, permissions, schemas, triggers, and other advanced database features are not supported - by design. Any database that has an ActiveRecord adapter should work. This gem is now Rails 3 only. For Rails 2, clone and checkout the Rails2 branch. [![Build Status](https://travis-ci.org/yamldb/yaml_db.svg?branch=master)](https://travis-ci.org/yamldb/yaml_db) ## Installation Simply add to your Gemfile: gem 'yaml_db' All rake tasks will then be available to you. ## Usage rake db:data:dump -> Dump contents of Rails database to db/data.yml rake db:data:load -> Load contents of db/data.yml into the database Further, there are tasks db:dump and db:load which do the entire database (the equivalent of running db:schema:dump followed by db:data:load). Also, there are other tasks recently added that allow the export of the database contents to/from multiple files (each one named after the table being dumped or loaded). rake db:data:dump_dir -> Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml) rake db:data:load_dir -> Load contents of db/data_dir into database In addition, we have plugins whereby you can export your database to/from various formats. We only deal with yaml and csv right now, but you can easily write tools for your own formats (such as Excel or XML). To use another format, just load setting the "class" parameter to the class you are using. This defaults to "YamlDb::Helper" which is a refactoring of the old yaml_db code. We'll shorten this to use class nicknames in a little bit. ## Examples One common use would be to switch your data from one database backend to another. For example, let's say you wanted to switch from SQLite to MySQL. You might execute the following steps: 1. rake db:dump 2. Edit config/database.yml and change your adapter to mysql, set up database params 3. mysqladmin create [database name] 4. rake db:load ## Credits Created by Orion Henry and Adam Wiggins. Major updates by Ricardo Chimal, Jr. Patches contributed by Michael Irwin, Tom Locke, and Tim Galeckas. yaml_db-0.3.0/Rakefile000066400000000000000000000002051242550020000145510ustar00rootroot00000000000000require 'rake' require "rspec/core/rake_task" require "bundler/gem_tasks" RSpec::Core::RakeTask.new(:spec) task :default => :spec yaml_db-0.3.0/init.rb000066400000000000000000000000221242550020000143710ustar00rootroot00000000000000require 'yaml_db' yaml_db-0.3.0/lib/000077500000000000000000000000001242550020000136555ustar00rootroot00000000000000yaml_db-0.3.0/lib/csv_db.rb000066400000000000000000000032601242550020000154430ustar00rootroot00000000000000#require 'FasterCSV' module CsvDb module Helper def self.loader Load end def self.dumper Dump end def self.extension "csv" end end class Load < SerializationHelper::Load def self.load_documents(io, truncate = true) tables = {} curr_table = nil io.each do |line| if /BEGIN_CSV_TABLE_DECLARATION(.+)END_CSV_TABLE_DECLARATION/ =~ line curr_table = $1 tables[curr_table] = {} else if tables[curr_table]["columns"] tables[curr_table]["records"] << FasterCSV.parse(line)[0] else tables[curr_table]["columns"] = FasterCSV.parse(line)[0] tables[curr_table]["records"] = [] end end end tables.each_pair do |table_name, contents| load_table(table_name, contents, truncate) end end end class Dump < SerializationHelper::Dump def self.before_table(io,table) io.write "BEGIN_CSV_TABLE_DECLARATION#{table}END_CSV_TABLE_DECLARATION\n" end def self.dump(io) tables.each do |table| before_table(io, table) dump_table(io, table) after_table(io, table) end end def self.after_table(io,table) io.write "" end def self.dump_table_columns(io, table) io.write(table_column_names(table).to_csv) end def self.dump_table_records(io, table) column_names = table_column_names(table) each_table_page(table) do |records| rows = SerializationHelper::Utils.unhash_records(records, column_names) records.each do |record| io.write(record.to_csv) end end end end endyaml_db-0.3.0/lib/serialization_helper.rb000066400000000000000000000126071242550020000204240ustar00rootroot00000000000000module SerializationHelper class Base attr_reader :extension def initialize(helper) @dumper = helper.dumper @loader = helper.loader @extension = helper.extension end def dump(filename) disable_logger @dumper.dump(File.new(filename, "w")) reenable_logger end def dump_to_dir(dirname) Dir.mkdir(dirname) tables = @dumper.tables tables.each do |table| io = File.new "#{dirname}/#{table}.#{@extension}", "w" @dumper.before_table(io, table) @dumper.dump_table io, table @dumper.after_table(io, table) end end def load(filename, truncate = true) disable_logger @loader.load(File.new(filename, "r"), truncate) reenable_logger end def load_from_dir(dirname, truncate = true) Dir.entries(dirname).each do |filename| if filename =~ /^[.]/ next end @loader.load(File.new("#{dirname}/#{filename}", "r"), truncate) end end def disable_logger @@old_logger = ActiveRecord::Base.logger ActiveRecord::Base.logger = nil end def reenable_logger ActiveRecord::Base.logger = @@old_logger end end class Load def self.load(io, truncate = true) ActiveRecord::Base.connection.transaction do load_documents(io, truncate) end end def self.truncate_table(table) begin ActiveRecord::Base.connection.execute("TRUNCATE #{SerializationHelper::Utils.quote_table(table)}") rescue Exception ActiveRecord::Base.connection.execute("DELETE FROM #{SerializationHelper::Utils.quote_table(table)}") end end def self.load_table(table, data, truncate = true) column_names = data['columns'] if truncate truncate_table(table) end load_records(table, column_names, data['records']) reset_pk_sequence!(table) end 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 def self.reset_pk_sequence!(table_name) if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!) ActiveRecord::Base.connection.reset_pk_sequence!(table_name) end end end module Utils def self.unhash(hash, keys) keys.map { |key| hash[key] } end def self.unhash_records(records, keys) records.each_with_index do |record, index| records[index] = unhash(record, keys) end records end def self.convert_booleans(records, columns) records.each do |record| columns.each do |column| next if is_boolean(record[column]) record[column] = convert_boolean(record[column]) end end records end def self.convert_boolean(value) ['t', '1', true, 1].include?(value) end def self.boolean_columns(table) columns = ActiveRecord::Base.connection.columns(table).reject { |c| silence_warnings { c.type != :boolean } } columns.map { |c| c.name } end def self.is_boolean(value) value.kind_of?(TrueClass) or value.kind_of?(FalseClass) end def self.quote_table(table) ActiveRecord::Base.connection.quote_table_name(table) end end class Dump def self.before_table(io, table) end def self.dump(io) tables.each do |table| before_table(io, table) dump_table(io, table) after_table(io, table) end end def self.after_table(io, table) end def self.tables ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) } end def self.dump_table(io, table) return if table_record_count(table).zero? dump_table_columns(io, table) dump_table_records(io, table) end def self.table_column_names(table) ActiveRecord::Base.connection.columns(table).map { |c| c.name } end def self.each_table_page(table, records_per_page=1000) total_count = table_record_count(table) pages = (total_count.to_f / records_per_page).ceil - 1 id = table_column_names(table).first boolean_columns = SerializationHelper::Utils.boolean_columns(table) quoted_table_name = SerializationHelper::Utils.quote_table(table) (0..pages).to_a.each do |page| query = Arel::Table.new(table, ActiveRecord::Base).order(id).skip(records_per_page*page).take(records_per_page).project(Arel.sql('*')) records = ActiveRecord::Base.connection.select_all(query.to_sql) records = SerializationHelper::Utils.convert_booleans(records, boolean_columns) yield records end end def self.table_record_count(table) ActiveRecord::Base.connection.select_one("SELECT COUNT(*) FROM #{SerializationHelper::Utils.quote_table(table)}").values.first.to_i end end end yaml_db-0.3.0/lib/tasks/000077500000000000000000000000001242550020000150025ustar00rootroot00000000000000yaml_db-0.3.0/lib/tasks/yaml_db_tasks.rake000066400000000000000000000032131242550020000204610ustar00rootroot00000000000000namespace :db do desc "Dump schema and data to db/schema.rb and db/data.yml" task(:dump => [ "db:schema:dump", "db:data:dump" ]) desc "Load schema and data from db/schema.rb and db/data.yml" task(:load => [ "db:schema:load", "db:data:load" ]) namespace :data do def db_dump_data_file (extension = "yml") "#{dump_dir}/data.#{extension}" end def dump_dir(dir = "") "#{Rails.root}/db#{dir}" end desc "Dump contents of database to db/data.extension (defaults to yaml)" task :dump => :environment do format_class = ENV['class'] || "YamlDb::Helper" helper = format_class.constantize SerializationHelper::Base.new(helper).dump db_dump_data_file helper.extension end desc "Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml)" task :dump_dir => :environment do format_class = ENV['class'] || "YamlDb::Helper" dir = ENV['dir'] || "#{Time.now.to_s.gsub(/ /, '_')}" SerializationHelper::Base.new(format_class.constantize).dump_to_dir dump_dir("/#{dir}") end desc "Load contents of db/data.extension (defaults to yaml) into database" task :load => :environment do format_class = ENV['class'] || "YamlDb::Helper" helper = format_class.constantize SerializationHelper::Base.new(helper).load(db_dump_data_file helper.extension) end desc "Load contents of db/data_dir into database" task :load_dir => :environment do dir = ENV['dir'] || "base" format_class = ENV['class'] || "YamlDb::Helper" SerializationHelper::Base.new(format_class.constantize).load_from_dir dump_dir("/#{dir}") end end end yaml_db-0.3.0/lib/yaml_db.rb000066400000000000000000000031101242550020000156040ustar00rootroot00000000000000require 'rubygems' require 'yaml' require 'active_record' require 'serialization_helper' require 'active_support/core_ext/kernel/reporting' require 'rails/railtie' require 'yaml_db/version' module YamlDb module Helper def self.loader YamlDb::Load end def self.dumper YamlDb::Dump end def self.extension "yml" end end module Utils def self.chunk_records(records) yaml = [ records ].to_yaml yaml.sub!(/---\s\n|---\n/, '') yaml.sub!('- - -', ' - -') yaml end end class Dump < SerializationHelper::Dump def self.dump_table_columns(io, table) io.write("\n") io.write({ table => { 'columns' => table_column_names(table) } }.to_yaml) end def self.dump_table_records(io, table) table_record_header(io) column_names = table_column_names(table) each_table_page(table) do |records| rows = SerializationHelper::Utils.unhash_records(records.to_a, column_names) io.write(YamlDb::Utils.chunk_records(rows)) end end def self.table_record_header(io) io.write(" records: \n") end end class Load < SerializationHelper::Load def self.load_documents(io, truncate = true) YAML.load_documents(io) do |ydoc| ydoc.keys.each do |table_name| next if ydoc[table_name].nil? load_table(table_name, ydoc[table_name], truncate) end end end end class Railtie < Rails::Railtie rake_tasks do load File.expand_path('../tasks/yaml_db_tasks.rake', __FILE__) end end end yaml_db-0.3.0/lib/yaml_db/000077500000000000000000000000001242550020000152645ustar00rootroot00000000000000yaml_db-0.3.0/lib/yaml_db/version.rb000066400000000000000000000000461242550020000172760ustar00rootroot00000000000000module YamlDb VERSION = "0.3.0" end yaml_db-0.3.0/spec/000077500000000000000000000000001242550020000140415ustar00rootroot00000000000000yaml_db-0.3.0/spec/integration_spec.rb000066400000000000000000000014641242550020000177300ustar00rootroot00000000000000require 'active_record/railtie' # connect to in-memory SQLite database ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ':memory:' ) # define a dummy User model class User < ActiveRecord::Base end # create the users table ActiveRecord::Schema.define do self.verbose = false create_table :users, :force => true do |t| t.string :username end end # add some users User.create([ {:username => 'alice'}, {:username => 'bob'} ]) RSpec.describe "with real ActiveRecord," do it "contains two users" do expect(User.count).to eq(2) end it "dumps the user records" do @io = StringIO.new YamlDb::Dump.dump_table_records(@io, 'users') @io.rewind expect(@io.read).to eq(< 'a', :type => :string), double('b', :name => 'b', :type => :string) ]) allow(ActiveRecord::Base.connection).to receive(:select_one).and_return({"count"=>"2"}) allow(ActiveRecord::Base.connection).to receive(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) allow(SerializationHelper::Utils).to receive(:quote_table).with('mytable').and_return('mytable') end before(:each) do allow(File).to receive(:new).with('dump.yml', 'w').and_return(StringIO.new) @io = StringIO.new end it "should return a list of column names" do expect(SerializationHelper::Dump.table_column_names('mytable')).to eq([ 'a', 'b' ]) end it "should return a list of tables without the rails schema table" do expect(SerializationHelper::Dump.tables).to eq(['mytable']) end it "should return the total number of records in a table" do expect(SerializationHelper::Dump.table_record_count('mytable')).to eq(2) end it "should return all records from the database and return them when there is only 1 page" do SerializationHelper::Dump.each_table_page('mytable') do |records| expect(records).to eq([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) end end it "should paginate records from the database and return them" do allow(ActiveRecord::Base.connection).to receive(:select_all).and_return([ { 'a' => 1, 'b' => 2 } ], [ { 'a' => 3, 'b' => 4 } ]) records = [ ] SerializationHelper::Dump.each_table_page('mytable', 1) do |page| expect(page.size).to eq(1) records.concat(page) end expect(records).to eq([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) end it "should dump a table's contents to yaml" do expect(SerializationHelper::Dump).to receive(:dump_table_columns) expect(SerializationHelper::Dump).to receive(:dump_table_records) SerializationHelper::Dump.dump_table(@io, 'mytable') end it "should not dump a table's contents when the record count is zero" do allow(SerializationHelper::Dump).to receive(:table_record_count).with('mytable').and_return(0) expect(SerializationHelper::Dump).not_to receive(:dump_table_columns) expect(SerializationHelper::Dump).not_to receive(:dump_table_records) SerializationHelper::Dump.dump_table(@io, 'mytable') end end yaml_db-0.3.0/spec/serialization_helper_load_spec.rb000066400000000000000000000105061242550020000226150ustar00rootroot00000000000000RSpec.describe SerializationHelper::Load do before do allow(SerializationHelper::Utils).to receive(:quote_table).with('mytable').and_return('mytable') allow(ActiveRecord::Base).to receive(:connection).and_return(double('connection').as_null_object) allow(ActiveRecord::Base.connection).to receive(:transaction).and_yield @io = StringIO.new end it "should truncate the table" do allow(ActiveRecord::Base.connection).to receive(:execute).with("TRUNCATE mytable").and_return(true) expect(ActiveRecord::Base.connection).not_to receive(:execute).with("DELETE FROM mytable") SerializationHelper::Load.truncate_table('mytable') end it "should delete the table if truncate throws an exception" do expect(ActiveRecord::Base.connection).to receive(:execute).with("TRUNCATE mytable").and_raise() expect(ActiveRecord::Base.connection).to receive(:execute).with("DELETE FROM mytable").and_return(true) SerializationHelper::Load.truncate_table('mytable') end it "should call reset pk sequence if the connection adapter is postgres" do expect(ActiveRecord::Base.connection).to receive(:respond_to?).with(:reset_pk_sequence!).and_return(true) expect(ActiveRecord::Base.connection).to receive(:reset_pk_sequence!).with('mytable') SerializationHelper::Load.reset_pk_sequence!('mytable') end it "should not call reset pk sequence for other adapters" do expect(ActiveRecord::Base.connection).to receive(:respond_to?).with(:reset_pk_sequence!).and_return(false) expect(ActiveRecord::Base.connection).not_to receive(:reset_pk_sequence!) SerializationHelper::Load.reset_pk_sequence!('mytable') end it "should insert records into a table" do mca = double('a',:name => 'a') mcb = double('b', :name => 'b') allow(ActiveRecord::Base.connection).to receive(:columns).with('mytable').and_return([mca , mcb ]) allow(ActiveRecord::Base.connection).to receive(:quote_column_name).with('a').and_return('a') allow(ActiveRecord::Base.connection).to receive(:quote_column_name).with('b').and_return('b') allow(ActiveRecord::Base.connection).to receive(:quote).with(1, mca).and_return("'1'") allow(ActiveRecord::Base.connection).to receive(:quote).with(2, mcb).and_return("'2'") allow(ActiveRecord::Base.connection).to receive(:quote).with(3, mca).and_return("'3'") allow(ActiveRecord::Base.connection).to receive(:quote).with(4, mcb).and_return("'4'") expect(ActiveRecord::Base.connection).to receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('1','2')") expect(ActiveRecord::Base.connection).to receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('3','4')") SerializationHelper::Load.load_records('mytable', ['a', 'b'], [[1, 2], [3, 4]]) end it "should quote column names that correspond to sql keywords" do mca = double('a',:name => 'a') mccount = double('count', :name => 'count') allow(ActiveRecord::Base.connection).to receive(:columns).with('mytable').and_return([mca , mccount ]) allow(ActiveRecord::Base.connection).to receive(:quote_column_name).with('a').and_return('a') allow(ActiveRecord::Base.connection).to receive(:quote_column_name).with('count').and_return('"count"') allow(ActiveRecord::Base.connection).to receive(:quote).with(1, mca).and_return("'1'") allow(ActiveRecord::Base.connection).to receive(:quote).with(2, mccount).and_return("'2'") allow(ActiveRecord::Base.connection).to receive(:quote).with(3, mca).and_return("'3'") allow(ActiveRecord::Base.connection).to receive(:quote).with(4, mccount).and_return("'4'") expect(ActiveRecord::Base.connection).to receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('1','2')") expect(ActiveRecord::Base.connection).to receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('3','4')") SerializationHelper::Load.load_records('mytable', ['a', 'count'], [[1, 2], [3, 4]]) end it "should truncate the table and then load the records into the table" do expect(SerializationHelper::Load).to receive(:truncate_table).with('mytable') expect(SerializationHelper::Load).to receive(:load_records).with('mytable', ['a', 'b'], [[1, 2], [3, 4]]) expect(SerializationHelper::Load).to receive(:reset_pk_sequence!).with('mytable') SerializationHelper::Load.load_table('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] }) end end yaml_db-0.3.0/spec/serialization_utils_spec.rb000066400000000000000000000042451242550020000215020ustar00rootroot00000000000000RSpec.describe SerializationHelper::Utils, " convert records utility method" do before do allow(ActiveRecord::Base).to receive(:connection).and_return(double('connection').as_null_object) end it "returns an array of hash values using an array of ordered keys" do expect(SerializationHelper::Utils.unhash({ 'a' => 1, 'b' => 2 }, [ 'b', 'a' ])).to eq([ 2, 1 ]) end it "should unhash each hash an array using an array of ordered keys" do expect(SerializationHelper::Utils.unhash_records([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ], [ 'b', 'a' ])).to eq([ [ 2, 1 ], [ 4, 3 ] ]) end it "should return true if it is a boolean type" do expect(SerializationHelper::Utils.is_boolean(true)).to be true expect(SerializationHelper::Utils.is_boolean('true')).to be false end it "should return an array of boolean columns" do allow(ActiveRecord::Base.connection).to receive(:columns).with('mytable').and_return([ double('a',:name => 'a',:type => :string), double('b', :name => 'b',:type => :boolean) ]) expect(SerializationHelper::Utils.boolean_columns('mytable')).to eq(['b']) end it "should quote the table name" do expect(ActiveRecord::Base.connection).to receive(:quote_table_name).with('values').and_return('`values`') expect(SerializationHelper::Utils.quote_table('values')).to eq('`values`') end it "should convert ruby booleans to true and false" do expect(SerializationHelper::Utils.convert_boolean(true)).to be true expect(SerializationHelper::Utils.convert_boolean(false)).to be false end it "should convert ruby string t and f to true and false" do expect(SerializationHelper::Utils.convert_boolean('t')).to be true expect(SerializationHelper::Utils.convert_boolean('f')).to be false end it "should convert ruby string 1 and 0 to true and false" do expect(SerializationHelper::Utils.convert_boolean('1')).to be true expect(SerializationHelper::Utils.convert_boolean('0')).to be false end it "should convert ruby integer 1 and 0 to true and false" do expect(SerializationHelper::Utils.convert_boolean(1)).to be true expect(SerializationHelper::Utils.convert_boolean(0)).to be false end end yaml_db-0.3.0/spec/spec_helper.rb000066400000000000000000000055011242550020000166600ustar00rootroot00000000000000require 'yaml_db' # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| config.expect_with :rspec do |expectations| # This option will default to `true` in RSpec 4. It makes the `description` # and `failure_message` of custom matchers include text for helper methods # defined using `chain`, e.g.: # be_bigger_than(2).and_smaller_than(4).description # # => "be bigger than 2 and smaller than 4" # ...rather than: # # => "be bigger than 2" #expectations.include_chain_clauses_in_custom_matcher_descriptions = true end config.mock_with :rspec do |mocks| # Prevents you from mocking or stubbing a method that does not exist on # a real object. This is generally recommended, and will default to # `true` in RSpec 4. #mocks.verify_partial_doubles = true end # These two settings work together to allow you to limit a spec run # to individual examples or groups you care about by tagging them with # `:focus` metadata. When nothing is tagged with `:focus`, all examples # get run. config.filter_run :focus config.run_all_when_everything_filtered = true # Limits the available syntax to the non-monkey patched syntax that is recommended. # For more details, see: # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching config.disable_monkey_patching! # This setting enables warnings. It's recommended, but in some cases may # be too noisy due to issues in dependencies. config.warnings = true # Many RSpec users commonly either run the entire suite or an individual # file, and it's useful to allow more verbose output when running an # individual spec file. if config.files_to_run.one? # Use the documentation formatter for detailed output, # unless a formatter has already been configured # (e.g. via a command-line flag). config.default_formatter = 'doc' end # Print the 10 slowest examples and example groups at the # end of the spec run, to help surface which specs are running # particularly slow. #config.profile_examples = 10 # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = :random # Seed global randomization in this process using the `--seed` CLI option. # Setting this allows you to use `--seed` to deterministically reproduce # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed end yaml_db-0.3.0/spec/yaml_dump_spec.rb000066400000000000000000000034351242550020000173740ustar00rootroot00000000000000RSpec.describe YamlDb::Dump do before do allow(ActiveRecord::Base).to receive(:connection).and_return(double('connection').as_null_object) allow(ActiveRecord::Base.connection).to receive(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ]) allow(ActiveRecord::Base.connection).to receive(:columns).with('mytable').and_return([ double('a',:name => 'a', :type => :string), double('b', :name => 'b', :type => :string) ]) allow(ActiveRecord::Base.connection).to receive(:select_one).and_return({"count"=>"2"}) allow(ActiveRecord::Base.connection).to receive(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) allow(YamlDb::Utils).to receive(:quote_table).with('mytable').and_return('mytable') end before(:each) do allow(File).to receive(:new).with('dump.yml', 'w').and_return(StringIO.new) @io = StringIO.new end it "should return a formatted string" do YamlDb::Dump.table_record_header(@io) @io.rewind expect(@io.read).to eq(" records: \n") end it "should return a yaml string that contains a table header and column names" do allow(YamlDb::Dump).to receive(:table_column_names).with('mytable').and_return([ 'a', 'b' ]) YamlDb::Dump.dump_table_columns(@io, 'mytable') @io.rewind if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9.3') expect(@io.read).to eq(< { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] } } ) expect(YamlDb::Load).to receive(:load_table).with('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] },true) YamlDb::Load.load(@io) end it "should not call load structure when the document in the file contains no records" do expect(YAML).to receive(:load_documents).with(@io).and_yield({ 'mytable' => nil }) expect(YamlDb::Load).not_to receive(:load_table) YamlDb::Load.load(@io) end end yaml_db-0.3.0/spec/yaml_utils_spec.rb000066400000000000000000000006501242550020000175630ustar00rootroot00000000000000RSpec.describe YamlDb::Utils, " convert records utility method" do it "turns an array with one record into a yaml chunk" do expect(YamlDb::Utils.chunk_records([ %w(a b) ])).to eq(<= 3.0", "< 4.3" s.add_runtime_dependency "rake", ">= 0.8.7" s.add_development_dependency "bundler", "~> 1.0" s.add_development_dependency "rspec", "~> 3.0" s.add_development_dependency "sqlite3", "~> 1.3" end