/usr/lib/ruby/vendor_ruby/database_cleaner/data_mapper/truncation.rb is in ruby-database-cleaner 1.5.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | require "database_cleaner/generic/truncation"
require 'database_cleaner/data_mapper/base'
module DataMapper
module Adapters
class DataObjectsAdapter
def storage_names(repository = :default)
raise NotImplementedError
end
def truncate_tables(table_names)
table_names.each do |table_name|
truncate_table table_name
end
end
end
class MysqlAdapter < DataObjectsAdapter
# taken from http://github.com/godfat/dm-mapping/tree/master
def storage_names(repository = :default)
select 'SHOW TABLES'
end
def truncate_table(table_name)
execute("TRUNCATE TABLE #{quote_name(table_name)};")
end
# copied from activerecord
def disable_referential_integrity
old = select("SELECT @@FOREIGN_KEY_CHECKS;")
begin
execute("SET FOREIGN_KEY_CHECKS = 0;")
yield
ensure
execute("SET FOREIGN_KEY_CHECKS = ?", *old)
end
end
end
module SqliteAdapterMethods
# taken from http://github.com/godfat/dm-mapping/tree/master
def storage_names(repository = :default)
# activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
sql = <<-SQL
SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
SQL
# activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 181
select(sql)
end
def truncate_table(table_name)
execute("DELETE FROM #{quote_name(table_name)};")
if uses_sequence?
execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
end
end
# this is a no-op copied from activerecord
# i didn't find out if/how this is possible
# activerecord also doesn't do more here
def disable_referential_integrity
yield
end
private
# Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
def uses_sequence?
sql = <<-SQL
SELECT name FROM sqlite_master
WHERE type='table' AND name='sqlite_sequence'
SQL
select(sql).first
end
end
class SqliteAdapter; include SqliteAdapterMethods; end
class Sqlite3Adapter; include SqliteAdapterMethods; end
# FIXME
# i don't know if this works
# i basically just copied activerecord code to get a rough idea what they do.
# i don't have postgres available, so i won't be the one to write this.
# maybe codes below gets some postgres/datamapper user going, though.
class PostgresAdapter < DataObjectsAdapter
# taken from http://github.com/godfat/dm-mapping/tree/master
def storage_names(repository = :default)
sql = <<-SQL
SELECT table_name FROM "information_schema"."tables"
WHERE table_schema = current_schema() and table_type = 'BASE TABLE'
SQL
select(sql)
end
def truncate_table(table_name)
execute("TRUNCATE TABLE #{quote_name(table_name)} RESTART IDENTITY CASCADE;")
end
# override to use a single statement
def truncate_tables(table_names)
quoted_names = table_names.collect { |n| quote_name(n) }.join(', ')
execute("TRUNCATE TABLE #{quoted_names} RESTART IDENTITY;")
end
# FIXME
# copied from activerecord
def supports_disable_referential_integrity?
version = select("SHOW server_version")[0][0].split('.')
(version[0].to_i >= 8 && version[1].to_i >= 1) ? true : false
rescue
return false
end
# FIXME
# copied unchanged from activerecord
def disable_referential_integrity(repository = :default)
if supports_disable_referential_integrity? then
execute(storage_names(repository).collect do |name|
"ALTER TABLE #{quote_name(name)} DISABLE TRIGGER ALL"
end.join(";"))
end
yield
ensure
if supports_disable_referential_integrity? then
execute(storage_names(repository).collect do |name|
"ALTER TABLE #{quote_name(name)} ENABLE TRIGGER ALL"
end.join(";"))
end
end
end
end
end
module DatabaseCleaner
module DataMapper
class Truncation
include ::DatabaseCleaner::DataMapper::Base
include ::DatabaseCleaner::Generic::Truncation
def clean(repository = self.db)
adapter = ::DataMapper.repository(repository).adapter
adapter.disable_referential_integrity do
adapter.truncate_tables(tables_to_truncate(repository))
end
end
private
def tables_to_truncate(repository = self.db)
(@only || ::DataMapper.repository(repository).adapter.storage_names(repository)) - @tables_to_exclude
end
# overwritten
def migration_storage_names
%w[migration_info]
end
end
end
end
|