Class: DBConnection
- Inherits:
-
Object
- Object
- DBConnection
- Defined in:
- lib/db_connection.rb
Class Method Summary collapse
- .add_to_version(file) ⇒ Object
- .app_name ⇒ Object
- .columns(table_name) ⇒ Object
- .ensure_version_table ⇒ Object
- .execute(*args) ⇒ Object
- .instance ⇒ Object
- .migrate ⇒ Object
- .migrated?(file) ⇒ Boolean
- .open ⇒ Object
- .parse_migration_file(file) ⇒ Object
- .print_query(query, *interpolation_args) ⇒ Object
- .reset ⇒ Object
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_name ⇒ Object
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_table ⇒ Object
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(*args) ⇒ Object
46 47 48 49 |
# File 'lib/db_connection.rb', line 46 def self.execute(*args) print_query(*args) instance.exec(*args) end |
.instance ⇒ Object
51 52 53 |
# File 'lib/db_connection.rb', line 51 def self.instance @db || open end |
.migrate ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/db_connection.rb', line 55 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
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/db_connection.rb', line 70 def self.migrated?(file) name = parse_migration_file(file) result = execute(<<-SQL, [name]) SELECT * FROM version WHERE name = $1; SQL !!result.first end |
.open ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/db_connection.rb', line 100 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
83 84 85 86 87 |
# File 'lib/db_connection.rb', line 83 def self.parse_migration_file(file) filename = File.basename(file).split('.').first u_idx = filename.index('_') filename[0..u_idx - 1] end |
.print_query(query, *interpolation_args) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/db_connection.rb', line 89 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 |
.reset ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/db_connection.rb', line 119 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 |