Class: Roomer::Schema

Inherits:
ActiveRecord::Migration
  • Object
show all
Extended by:
Helpers::PostgresHelper
Defined in:
lib/roomer/schema.rb

Overview

Roomer::Schema extends ActiveRecord::Schema Roomer::Schema is currently only supported by the Postgres database adapter

Class Method Summary collapse

Methods included from Helpers::PostgresHelper

create_schema, create_sequence, drop_schema, ensure_prefix, ensure_schema_migrations, ensuring_schema, schemas, shared_migrations_pending?, stored_procedures, view_definitions

Class Method Details

.current_schemaObject



64
65
66
# File 'lib/roomer/schema.rb', line 64

def self.current_schema
  ActiveRecord::Base.table_name_prefix.split('.').first
end

.current_tenantObject



68
69
70
71
# File 'lib/roomer/schema.rb', line 68

def self.current_tenant
  return nil if current_schema == Roomer.shared_schema_name.to_s
  Tenant.find_by_schema_name(current_schema)
end

.define(info = {}, &block) ⇒ Object



57
58
59
60
61
62
# File 'lib/roomer/schema.rb', line 57

def self.define(info={}, &block)
  instance_eval(&block)
  unless info[:version].blank?
    ActiveRecord::Base.connection.assume_migrated_upto_version(info[:version], migrations_path)
  end
end

.dump(scope = :tenanted) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/roomer/schema.rb', line 21

def self.dump(scope=:tenanted)
  unless Roomer.heroku_safe && ENV['HEROKU_UPID']
    case scope
      when :shared
        schema_name = Roomer.shared_schema_name.to_s
        filename = Roomer.shared_schema_filename
      when :tenanted
        tenant = Tenant.first
        return if tenant.blank?

        schema_name = Tenant.first.schema_name.to_s
        filename = Roomer.tenanted_schema_filename
    end
    FileUtils.mkdir_p(Roomer.schemas_directory) unless File.exists?(Roomer.schemas_directory)
    filepath = File.expand_path(File.join(Roomer.schemas_directory, filename))
    ActiveRecord::Base.connection.schema_search_path = schema_name
    ActiveRecord::Base.table_name_prefix = "#{schema_name}."
    Roomer::SchemaDumper.dump(ActiveRecord::Base.connection, File.new(filepath, "w"))
  end
end

.load(schema_name, scope = :tenanted) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/roomer/schema.rb', line 42

def self.load(schema_name, scope=:tenanted)
  ensuring_schema(schema_name) do
    filename = begin
      if scope == :shared
        Roomer.shared_schema_filename
      elsif scope == :tenanted
        Roomer.tenanted_schema_filename
      end
    end
    filepath = File.expand_path(File.join(Roomer.schemas_directory, filename))
    return unless File.exists?(filepath)
    Object.load(filepath) 
  end
end

.migrations_path(scope = :tenanted) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/roomer/schema.rb', line 10

def self.migrations_path(scope=:tenanted)
  case scope
    when :shared
      return Roomer.shared_migrations_directory
    when :tenanted
      return Roomer.tenanted_migrations_directory
    else
      raise 'Invalid scope parameter'
  end
end