Class: Bezel::DBConnection

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

Class Method Summary collapse

Class Method Details

.add_to_version(file) ⇒ Object



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

def self.add_to_version(file)
  name = parse_migration_file(file)
  execute("    INSERT INTO\n      version (name)\n    VALUES\n      ($1);\n  SQL\nend\n", [name])

.app_nameObject



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

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

.columns(table_name) ⇒ Object



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

def self.columns(table_name)
  columns = instance.exec("    SELECT\n      attname\n    FROM\n      pg_attribute\n    WHERE\n      attrelid = '\#{table_name}'::regclass AND\n      attnum > 0 AND\n      NOT attisdropped\n  SQL\n\n  columns.map { |col| col['attname'].to_sym }\nend\n")

.ensure_version_tableObject



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

def self.ensure_version_table
  table = nil

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

.execute(*args) ⇒ Object



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

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

.instanceObject



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

def self.instance
  open if @db.nil?

  @db
end

.migrateObject



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

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)


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("    SELECT\n      *\n    FROM\n      version\n    WHERE\n      name = $1;\n  SQL\n  !!result.first\nend\n", [name])

.openObject



101
102
103
104
105
106
# File 'lib/db_connection.rb', line 101

def self.open
  @db = PG::Connection.new(
    dbname: app_name,
    port: 5432
  )
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


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

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



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

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

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