Class: Population::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/population/database.rb

Class Method Summary collapse

Class Method Details

.mysql?Boolean

Check if we’re dealing with a MySQL database.

Returns:

  • (Boolean)


35
36
37
# File 'lib/population/database.rb', line 35

def self.mysql?
  ActiveRecord::Base.connection.adapter_name == 'MySQL'
end

.postgres?Boolean

Check if we’re dealing with a PostgreSQL database.

Returns:

  • (Boolean)


30
31
32
# File 'lib/population/database.rb', line 30

def self.postgres?
  ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
end

.reset_command_for(klass) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/population/database.rb', line 12

def self.reset_command_for(klass)
  table_name = klass.name.tableize

  if sqlite?
    "delete from sqlite_sequence where name='#{table_name}';" 
  elsif mysql?
    "ALTER TABLE #{table_name} AUTO_INCREMENT = 1;" 
  elsif postgres?
    "SELECT setval('public.#{table_name}_id_seq', 1, false)" 
  end
end

.reset_table_belonging_to(klass) ⇒ Object

Destroy all records belonging to a class and reset the auto-increment ID to 1.



6
7
8
9
10
# File 'lib/population/database.rb', line 6

def self.reset_table_belonging_to(klass)
  klass.destroy_all
  reset_command = reset_command_for(klass)
  ActiveRecord::Base.connection.execute(reset_command)
end

.sqlite?Boolean

Check if we’re dealing with a SQLite database.

Returns:

  • (Boolean)


25
26
27
# File 'lib/population/database.rb', line 25

def self.sqlite?
  ActiveRecord::Base.connection.adapter_name == 'SQLite'
end