Module: Multitenant::SchemaUtils

Defined in:
lib/multitenant/schema_utils.rb

Class Method Summary collapse

Class Method Details

.add_schema_to_path(schema_name) ⇒ Object



17
18
19
# File 'lib/multitenant/schema_utils.rb', line 17

def add_schema_to_path(schema_name)
  connection.execute "SET search_path TO #{schema_name}"
end

.all_schemasObject



67
68
69
# File 'lib/multitenant/schema_utils.rb', line 67

def all_schemas
  connection.select_values("SELECT * FROM pg_namespace WHERE nspname != 'information_schema' AND nspname NOT LIKE 'pg%'")
end

.create_schema(schema_name) ⇒ Object



30
31
32
33
34
35
# File 'lib/multitenant/schema_utils.rb', line 30

def create_schema(schema_name)
  raise "#{schema_name} already exists" if schema_exists?(schema_name)

  ActiveRecord::Base.logger.info "Create #{schema_name}"
  connection.execute "CREATE SCHEMA #{schema_name}"
end

.current_search_pathObject



26
27
28
# File 'lib/multitenant/schema_utils.rb', line 26

def current_search_path
  connection.select_value "SHOW search_path"
end

.drop_schema(schema_name) ⇒ Object



37
38
39
40
41
42
# File 'lib/multitenant/schema_utils.rb', line 37

def drop_schema(schema_name)
  raise "#{schema_name} does not exists" unless schema_exists?(schema_name)

  ActiveRecord::Base.logger.info "Drop schema #{schema_name}"
  connection.execute "DROP SCHEMA #{schema_name} CASCADE"
end

.load_schema_into_schema(schema_name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/multitenant/schema_utils.rb', line 50

def load_schema_into_schema(schema_name)
  ActiveRecord::Base.logger.info "Enter schema #{schema_name}."
  with_schema(schema_name) do
    file = "#{Rails.root}/db/schema.rb"
    if File.exists?(file)
      ActiveRecord::Base.logger.info "Load the schema #{file}"
      load(file)
    else
      raise "#{file} desn't exist yet. It's possible that you just ran a migration!"
    end
  end
end

.migrate_schema(schema_name, version = nil) ⇒ Object



44
45
46
47
48
# File 'lib/multitenant/schema_utils.rb', line 44

def migrate_schema(schema_name, version = nil)
  with_schema(schema_name) do
    ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, version ? version.to_i : nil)
  end
end

.reset_search_pathObject



21
22
23
24
# File 'lib/multitenant/schema_utils.rb', line 21

def reset_search_path
  connection.execute "SET search_path TO #{connection.schema_search_path}"
  ActiveRecord::Base.connection.reset!
end

.schema_exists?(schema_name) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/multitenant/schema_utils.rb', line 63

def schema_exists?(schema_name)
  all_schemas.include?(schema_name)
end

.with_all_schemasObject



71
72
73
74
75
76
77
# File 'lib/multitenant/schema_utils.rb', line 71

def with_all_schemas
  all_schemas.each do |schema_name|
    with_schema(schema_name) do
      yield
    end
  end
end

.with_schema(schema_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/multitenant/schema_utils.rb', line 6

def with_schema(schema_name)
  old_search_path = connection.schema_search_path
  add_schema_to_path(schema_name)
  connection.schema_search_path = schema_name
  result = yield

  connection.schema_search_path = old_search_path
  reset_search_path
  result
end