module DatabaseRewinder
Constants
- VERSION
Attributes
database_configuration[W]
Set your DB configuration here if you’d like to use something else than the AR configuration
Public Class Methods
[](connection)
click to toggle source
# File lib/database_rewinder.rb, line 24 def [](connection) @cleaners.detect {|c| c.connection_name == connection} || create_cleaner(connection) end
all=(v)
click to toggle source
# File lib/database_rewinder.rb, line 28 def all=(v) @clean_all = v end
all_table_names(connection)
click to toggle source
cache AR connection.tables
# File lib/database_rewinder.rb, line 75 def all_table_names(connection) cache_key = get_cache_key(connection.pool) #NOTE connection.tables warns on AR 5 with some adapters tables = ActiveSupport::Deprecation.silence { connection.tables } schema_migraion_table_name = if ActiveRecord::SchemaMigration.respond_to?(:table_name) ActiveRecord::SchemaMigration.table_name else ActiveRecord::SchemaMigration.new(connection).table_name # AR >= 7.1 end @table_names_cache[cache_key] ||= tables.reject do |t| (t == schema_migraion_table_name) || (ActiveRecord::Base.respond_to?(:internal_metadata_table_name) && (t == ActiveRecord::Base.internal_metadata_table_name)) end end
clean(multiple: true)
click to toggle source
# File lib/database_rewinder.rb, line 62 def clean(multiple: true) if @clean_all clean_all multiple: multiple else cleaners.each {|c| c.clean multiple: multiple} end end
clean_all(multiple: true)
click to toggle source
# File lib/database_rewinder.rb, line 70 def clean_all(multiple: true) cleaners.each {|c| c.clean_all multiple: multiple} end
cleaners()
click to toggle source
# File lib/database_rewinder.rb, line 32 def cleaners create_cleaner 'test' if @cleaners.empty? @cleaners end
create_cleaner(connection_name)
click to toggle source
# File lib/database_rewinder.rb, line 18 def create_cleaner(connection_name) config = configuration_hash_for(connection_name) or raise %Q[Database configuration named "#{connection_name}" is not configured.] Cleaner.new(config: config, connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c} end
database_configuration()
click to toggle source
# File lib/database_rewinder.rb, line 14 def database_configuration @database_configuration || ActiveRecord::Base.configurations end
database_configuration_for(connection_name)
click to toggle source
# File lib/database_rewinder.rb, line 114 def database_configuration_for(connection_name) traditional_configuration_for(connection_name) || multiple_database_configuration_for(connection_name) end
init()
click to toggle source
# File lib/database_rewinder.rb, line 10 def init @cleaners, @table_names_cache, @clean_all, @only, @except, @database_configuration = [], {}, false end
multiple_database_configuration_for(connection_name)
click to toggle source
# File lib/database_rewinder.rb, line 122 def multiple_database_configuration_for(connection_name) if (ActiveRecord::VERSION::MAJOR >= 7) || ((ActiveRecord::VERSION::MAJOR >= 6) && (ActiveRecord::VERSION::MINOR >= 1)) database_configuration.configs_for(name: connection_name) else database_configuration.configs_for(spec_name: connection_name) end end
record_inserted_table(connection, sql)
click to toggle source
# File lib/database_rewinder.rb, line 37 def record_inserted_table(connection, sql) config = connection.instance_variable_get(:'@config') database = config[:database] #NOTE What's the best way to get the app dir besides Rails.root? I know Dir.pwd here might not be the right solution, but it should work in most cases... root_dir = defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : Dir.pwd cleaner = cleaners.detect do |c| if (config[:adapter] == 'sqlite3') && (config[:database] != ':memory:') File.expand_path(c.db, root_dir) == File.expand_path(database, root_dir) else c.db == database end end or return sql.split(';').each do |statement| match = statement.match(/\A\s*INSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"(]+)[`"]?)*/i) next unless match table = match[1] if table cleaner.inserted_tables << table unless cleaner.inserted_tables.include? table cleaner.pool ||= connection.pool end end end
traditional_configuration_for(connection_name)
click to toggle source
# File lib/database_rewinder.rb, line 118 def traditional_configuration_for(connection_name) database_configuration.configs_for(env_name: connection_name).first end
Private Class Methods
configuration_hash_for(connection_name)
click to toggle source
# File lib/database_rewinder.rb, line 99 def configuration_hash_for(connection_name) if database_configuration.respond_to?(:configs_for) hash_config = database_configuration_for(connection_name) if hash_config if hash_config.respond_to?(:configuration_hash) hash_config.configuration_hash.stringify_keys else hash_config.config end end else database_configuration[connection_name] end end
get_cache_key(connection_pool)
click to toggle source
# File lib/database_rewinder.rb, line 91 def get_cache_key(connection_pool) if connection_pool.respond_to?(:db_config) # ActiveRecord >= 6.1 connection_pool.db_config.configuration_hash else connection_pool.spec.config end end