Class: HerokuSchemas::Database
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- HerokuSchemas::Database
show all
- Defined in:
- lib/heroku-schemas/database.rb
Class Method Summary
collapse
Class Method Details
.config_to_url(config) ⇒ Object
22
23
24
|
# File 'lib/heroku-schemas/database.rb', line 22
def config_to_url(config)
"postgres://#{config[:username]}:#{config[:password]}@#{config[:host]}:#{config[:port]}/#{config[:database]}"
end
|
.connect_to_url(url) ⇒ Object
4
5
6
7
8
|
# File 'lib/heroku-schemas/database.rb', line 4
def connect_to_url(url)
ActiveRecord::Base.configurations[connection_name] = url_to_config(url)
establish_connection(connection_name)
self
end
|
.create_schema(schema) ⇒ Object
54
55
56
|
# File 'lib/heroku-schemas/database.rb', line 54
def create_schema(schema)
execute("CREATE SCHEMA #{schema}")
end
|
.execute(sql) ⇒ Object
30
31
32
|
# File 'lib/heroku-schemas/database.rb', line 30
def execute(sql)
connection.execute(sql)
end
|
.existing_schemas ⇒ Object
50
51
52
|
# File 'lib/heroku-schemas/database.rb', line 50
def existing_schemas
select_values('SELECT schema_name FROM information_schema.schemata')
end
|
.rename_schema(from_schema, to_schema) ⇒ Object
58
59
60
|
# File 'lib/heroku-schemas/database.rb', line 58
def rename_schema(from_schema, to_schema)
execute("ALTER SCHEMA #{from_schema} RENAME TO #{to_schema}")
end
|
.schema_exists?(schema) ⇒ Boolean
38
39
40
|
# File 'lib/heroku-schemas/database.rb', line 38
def schema_exists?(schema)
existing_schemas.include?(schema)
end
|
.schema_tables(schema) ⇒ Object
46
47
48
|
# File 'lib/heroku-schemas/database.rb', line 46
def schema_tables(schema)
select_values("SELECT table_name FROM information_schema.tables WHERE table_schema = '#{schema}'")
end
|
.select_values(sql) ⇒ Object
34
35
36
|
# File 'lib/heroku-schemas/database.rb', line 34
def select_values(sql)
connection.select_values(sql)
end
|
.tables_exist?(schema) ⇒ Boolean
42
43
44
|
# File 'lib/heroku-schemas/database.rb', line 42
def tables_exist?(schema)
schema_tables(schema).present?
end
|
.url ⇒ Object
26
27
28
|
# File 'lib/heroku-schemas/database.rb', line 26
def url
config_to_url(connection.instance_variable_get(:@config))
end
|
.url_to_config(url) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/heroku-schemas/database.rb', line 10
def url_to_config(url)
db = URI.parse(url)
{
:adapter => 'postgresql',
:username => db.user,
:password => db.password,
:port => db.port,
:database => db.path.sub(%r(^/), ''),
:host => db.host
}
end
|