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

option to use config file to whitelist tables #104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/yaml_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def self.dumper
Dump
end

def self.config
Config.instance
end

def self.extension
"yml"
end
Expand All @@ -33,6 +37,30 @@ def self.chunk_records(records)

end

class Config
include Singleton

attr_reader :settings

def initialize
if File.exists?(path)
puts "Found yaml_db config!"
@settings = YAML.load(open(path))
else
puts "No config found at #{path}. Using defaults."
@settings = {}
end
end

delegate :[], to: :settings

private

def path
"#{Rails.root}/db/yaml_db.yml"
end
end

class Dump < SerializationHelper::Dump

def self.dump_table_columns(io, table)
Expand Down
19 changes: 13 additions & 6 deletions lib/yaml_db/serialization_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Base
attr_reader :extension

def initialize(helper)
@config = helper.config
@dumper = helper.dumper
@loader = helper.loader
@extension = helper.extension
Expand All @@ -13,14 +14,14 @@ def initialize(helper)
def dump(filename)
disable_logger
File.open(filename, "w") do |file|
@dumper.dump(file)
@dumper.dump(file, @config)
end
reenable_logger
end

def dump_to_dir(dirname)
Dir.mkdir(dirname)
tables = @dumper.tables
tables = @dumper.tables(@config)
tables.each do |table|
File.open("#{dirname}/#{table}.#{@extension}", "w") do |io|
@dumper.before_table(io, table)
Expand Down Expand Up @@ -148,8 +149,8 @@ def self.before_table(io, table)

end

def self.dump(io)
tables.each do |table|
def self.dump(io, config = nil)
tables(config).each do |table|
before_table(io, table)
dump_table(io, table)
after_table(io, table)
Expand All @@ -160,8 +161,14 @@ def self.after_table(io, table)

end

def self.tables
ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) }
def self.tables(config = nil)
if config.present? && config['tables'].present?
whitelist = config['tables']
ActiveRecord::Base.connection.tables.find_all { |table| whitelist.include?(table) }
else
blacklist = %w(schema_info schema_migrations)
ActiveRecord::Base.connection.tables.reject { |table| blacklist.include?(table) }
end
end

def self.dump_table(io, table)
Expand Down
2 changes: 1 addition & 1 deletion lib/yaml_db/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module YamlDb
VERSION = "0.4.0"
VERSION = "0.4.1"
end