Class: DBConnection

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

Class Method Summary collapse

Class Method Details

.add_to_version(file) ⇒ Object



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

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

.app_nameObject



8
9
10
# File 'lib/db_connection.rb', line 8

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

.columns(table_name) ⇒ Object



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

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



37
38
39
40
41
42
43
44
# File 'lib/db_connection.rb', line 37

def self.ensure_version_table
    execute(<<-SQL)
      CREATE TABLE IF NOT EXISTS version (
        id SERIAL PRIMARY KEY,
        name VARCHAR(255) NOT NULL
      );
    SQL
end

.execute(query, params = []) ⇒ Object



46
47
48
49
50
# File 'lib/db_connection.rb', line 46

def self.execute(query, params = [])
  query = number_placeholders(query)
  print_query(query, params)
  instance.exec(query, params)
end

.instanceObject



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

def self.instance
  @db || open
end

.migrateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/db_connection.rb', line 56

def self.migrate
  ensure_version_table
  pending_migrations = MIGRATIONS.reject { |file| migrated?(file) }
  pending_migrations.each do |file|
    add_to_version(file)
    if (ENV['DATABASE_URL'])
      uri = URI.parse(ENV['DATABASE_URL'])
      db_name = uri.path[1..-1]
    else
      db_name = app_name
    end
    `psql -d #{db_name} -a -f #{file}`
  end
end

.migrated?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.number_placeholders(query_string) ⇒ Object



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

def self.number_placeholders(query_string)
  count = 0
  query_string.chars.map do |char|
    if char == "?"
      count += 1
      "$#{count}"
    else
      char
    end
  end.join("")
end

.openObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/db_connection.rb', line 113

def self.open
  if (ENV['DATABASE_URL'])
    uri = URI.parse(ENV['DATABASE_URL'])

    @db = PG::Connection.new(
       user: uri.user,
       password: uri.password,
       host: uri.host,
       port: uri.port,
       dbname: uri.path[1..-1],
    )
  else
    @db = PG::Connection.new(
      dbname: app_name,
      port: 5432
    )
  end
end

.parse_migration_file(file) ⇒ Object



84
85
86
87
88
# File 'lib/db_connection.rb', line 84

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


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

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/db_connection.rb', line 132

def self.reset
  if (ENV['DATABASE_URL'])
    uri = URI.parse(ENV['DATABASE_URL'])

    commands = [
      "dropdb #{uri.path[1..-1]}",
      "createdb #{uri.path[1..-1]}"
    ]

  else
    commands = [
      "dropdb #{app_name}",
      "createdb #{app_name}"
    ]
  end

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