Module: Vpd

Defined in:
lib/vpd/base.rb,
lib/vpd/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.activate(schema, verify_migration = false) ⇒ Object

prepends schema to the schema search path if verify_migration is true, will create the schema if required and ensure that it is migrated up-to-date



30
31
32
33
34
35
36
# File 'lib/vpd/base.rb', line 30

def self.activate(schema,verify_migration = false)
  base_path = self.initial_search_path
  self.create(schema) if verify_migration
  conn = ActiveRecord::Base.connection
  return false unless conn.schema_exists?(schema)
  conn.schema_search_path = [schema, base_path].compact.join(',')
end

.activate_defaultObject



38
39
40
# File 'lib/vpd/base.rb', line 38

def self.activate_default
  ActiveRecord::Base.connection.schema_search_path = self.initial_search_path
end

.create(schema, and_migrate = true) ⇒ Object

creates schema and optionally runs rails migrations to it



10
11
12
13
14
# File 'lib/vpd/base.rb', line 10

def self.create(schema,and_migrate = true)
  conn = ActiveRecord::Base.connection
  conn.execute("CREATE SCHEMA #{schema}") unless conn.schema_exists? schema
  self.migrate(schema) if and_migrate
end

.initial_search_pathObject

record the initial schema_search_path



4
5
6
7
# File 'lib/vpd/base.rb', line 4

def self.initial_search_path
  @@initial_search_path ||= YAML::load(File.open('config/database.yml'))[Rails.env]["schema_search_path"]
  @@initial_search_path ||= '"$user",public'
end

.migrate(schema, to_version = nil) ⇒ Object

runs rails migrations into schema preserves the current schema search path



18
19
20
21
22
23
24
25
# File 'lib/vpd/base.rb', line 18

def self.migrate(schema, to_version = nil)
  conn = ActiveRecord::Base.connection
  return false unless conn.schema_exists? schema
  current_search_path = conn.schema_search_path
  conn.schema_search_path = schema
  ActiveRecord::Migrator.migrate('db/migrate', to_version)
  conn.schema_search_path = current_search_path
end