Class: DataMapper::Adapters::PostgresAdapter

Inherits:
DataObjectsAdapter show all
Defined in:
lib/database_cleaner/data_mapper/truncation.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#disable_referential_integrity(repository = :default) ⇒ Object

FIXME copied unchanged from activerecord



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/database_cleaner/data_mapper/truncation.rb', line 162

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

#storage_names(repository = :default) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/database_cleaner/data_mapper/truncation.rb', line 133

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

#supports_disable_referential_integrity?Boolean

FIXME copied from activerecord

Returns:

  • (Boolean)


153
154
155
156
157
158
# File 'lib/database_cleaner/data_mapper/truncation.rb', line 153

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

#truncate_table(table_name) ⇒ Object



141
142
143
# File 'lib/database_cleaner/data_mapper/truncation.rb', line 141

def truncate_table(table_name)
  execute("TRUNCATE TABLE #{quote_name(table_name)} RESTART IDENTITY CASCADE;")
end

#truncate_tables(table_names) ⇒ Object

override to use a single statement



146
147
148
149
# File 'lib/database_cleaner/data_mapper/truncation.rb', line 146

def truncate_tables(table_names)
  quoted_names = table_names.collect { |n| quote_name(n) }.join(', ')
  execute("TRUNCATE TABLE #{quoted_names} RESTART IDENTITY;")
end