Class: DBConnection

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

Class Method Summary collapse

Class Method Details

.columns(table_name) ⇒ Object



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

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

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

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



33
34
35
36
37
# File 'lib/laris/larisrecord/db_connection.rb', line 33

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

.migrateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/laris/larisrecord/db_connection.rb', line 14

def self.migrate
  ensure_migrations_table

  migrations = Dir[File.join(Laris::ROOT, "/db/migrations/*.sql")]
  migrations.each do |file|
    filename = file.match(/([\w|-]*)\.sql$/)[1]

    unless migrated_files.include?(filename)
      instance.exec(File.read(file))
      instance.exec(<<-SQL)
        INSERT INTO
          migrations (filename)
        VALUES
          ('#{filename}')
        SQL
    end
  end
end

.openObject



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/laris/larisrecord/db_connection.rb', line 2

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

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