Class: TGauge::DBConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/db/db_connection.rb

Class Method Summary collapse

Class Method Details

.app_nameObject



10
11
12
# File 'lib/db/db_connection.rb', line 10

def self.app_name
  YAML.load_file(Dir.pwd + '/config/database.yml')['database']
end

.execute(*args) ⇒ Object



45
46
47
48
# File 'lib/db/db_connection.rb', line 45

def self.execute(*args)
  print_query(*args)
  instance.execute(*args)
end

.instanceObject



39
40
41
42
43
# File 'lib/db/db_connection.rb', line 39

def self.instance
  open if @db.nil?

  @db
end

.last_insert_row_idObject



50
51
52
# File 'lib/db/db_connection.rb', line 50

def self.last_insert_row_id
  instance.last_insert_row_id
end

.migrateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/db/db_connection.rb', line 21

def self.migrate
  ensure_migrations_table

  MIGRATION_FILES.each do |file|
    filename = file.match(/([\w|-]*)\.sql$/)[1]

    unless migrated_files.include?(filename)
      instance.exec(File.read(file))
      instance.exec("        INSERT INTO\n        migrations (filename)\n        VALUES\n        ('\#{filename}')\n      SQL\n    end\n  end\nend\n")

.openObject



14
15
16
17
18
19
# File 'lib/db/db_connection.rb', line 14

def self.open
  @postgres = PG::Connection.new(
    dbname: app_name,
    port: 5432
  )
end

.resetObject



54
55
56
57
58
59
60
# File 'lib/db/db_connection.rb', line 54

def self.reset
  commands = [
    "dropdb #{app_name}",
    "createdb #{app_name}"
  ]
  commands.each { |command| `#{command}` }
end