Class: Puffs::DBConnection

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

Overview

Connects to the Postgres DB.

Class Method Summary collapse

Class Method Details

.add_to_version(file) ⇒ Object



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

def self.add_to_version(file)
  name = parse_migration_file(file)
  execute(<<-SQL, [name])
    INSERT INTO
      version (name)
    VALUES
      ($1);
  SQL
end

.app_nameObject



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

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

.columns(table_name) ⇒ Object



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

def self.columns(table_name)
  columns = instance.exec(<<-SQL)
    SELECT
      attname
    FROM
      pg_attribute
    WHERE
      attrelid = '#{table_name}'::regclass AND
      attnum > 0 AND
      NOT attisdropped
  SQL

  columns.map { |col| col['attname'].to_sym }
end

.ensure_version_tableObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/db_connection.rb', line 39

def self.ensure_version_table
  # TODO: Find a reliable way to query db to see if version table exists.
  table = nil

  if table.nil?
    execute(<<-SQL)
      CREATE TABLE IF NOT EXISTS version (
        id SERIAL PRIMARY KEY,
        name VARCHAR(255) NOT NULL
      );
    SQL
  end
end

.execute(*args) ⇒ Object



53
54
55
56
# File 'lib/db_connection.rb', line 53

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

.instanceObject



58
59
60
61
62
# File 'lib/db_connection.rb', line 58

def self.instance
  open if @db.nil?

  @db
end

.migrateObject



64
65
66
67
68
69
70
71
# File 'lib/db_connection.rb', line 64

def self.migrate
  ensure_version_table
  to_migrate = MIGRATIONS.reject { |file| migrated?(file) }
  to_migrate.each do |file|
    add_to_version(file)
    `psql -d #{app_name} -a -f #{file}`
  end
end

.migrated?(file) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/db_connection.rb', line 73

def self.migrated?(file)
  name = parse_migration_file(file)
  result = execute(<<-SQL, [name])
    SELECT
      *
    FROM
      version
    WHERE
      name = $1;
  SQL
  !!result.first
end

.openObject



103
104
105
106
107
108
# File 'lib/db_connection.rb', line 103

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

.parse_migration_file(file) ⇒ Object



86
87
88
89
90
# File 'lib/db_connection.rb', line 86

def self.parse_migration_file(file)
  filename = File.basename(file).split('.').first
  u_idx = filename.index('_')
  filename[0..u_idx - 1]
end


92
93
94
95
96
97
98
99
100
101
# File 'lib/db_connection.rb', line 92

def self.print_query(query, *interpolation_args)
  return unless PRINT_QUERIES

  puts '--------------------'
  puts query
  unless interpolation_args.empty?
    puts "interpolate: #{interpolation_args.inspect}"
  end
  puts '--------------------'
end

.resetObject



110
111
112
113
114
115
116
117
# File 'lib/db_connection.rb', line 110

def self.reset
  commands = [
    "dropdb #{app_name}",
    "createdb #{app_name}"
  ]

  commands.each { |command| `#{command}` }
end