Module: PgSaurus::ConnectionAdapters::PostgreSQLAdapter::SchemaMethods

Included in:
PgSaurus::ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb

Overview

Provides methods to extend ActiveRecord::ConnectionAdapters::PostgreSQLAdapter to support schemas feature.

Instance Method Summary collapse

Instance Method Details

#create_schema(schema_name) ⇒ Object

Creates new schema in DB.



6
7
8
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 6

def create_schema(schema_name)
  ::PgSaurus::Tools.create_schema(schema_name)
end

#create_schema_if_not_exists(schema_name) ⇒ Object

Create schema if it does not exist yet.



26
27
28
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 26

def create_schema_if_not_exists(schema_name)
  ::PgSaurus::Tools.create_schema_if_not_exists(schema_name)
end

#current_schema_search_pathObject

Reads the current schema search path (it may have been altered from the initial value used when creating the connection)



92
93
94
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 92

def current_schema_search_path
  select_value("SHOW search_path;")
end

#drop_schema(schema_name) ⇒ Object

Drops schema in DB.



12
13
14
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 12

def drop_schema(schema_name)
  ::PgSaurus::Tools.drop_schema(schema_name)
end

#drop_schema_if_exists(schema_name) ⇒ Object

Drop schema if it exists.



33
34
35
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 33

def drop_schema_if_exists(schema_name)
  ::PgSaurus::Tools.drop_schema_if_exists(schema_name)
end

#drop_table(table_name, options = {}) ⇒ Object

Provide :schema option to drop_table method.



38
39
40
41
42
43
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 38

def drop_table(table_name, options = {})
  options     = options.dup
  schema_name = options.delete(:schema)
  table_name  = "#{schema_name}.#{table_name}" if schema_name
  super(table_name, options)
end

#in_schema(schema_name) ⇒ Object

Execute operations in the context of the schema



80
81
82
83
84
85
86
87
88
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 80

def in_schema(schema_name)
  search_path = current_schema_search_path
  begin
    execute("SET search_path TO '%s'" % schema_name)
    yield
  ensure
    execute("SET search_path TO #{search_path};")
  end
end

#move_table_to_schema(table, schema) ⇒ Object

Move table to another schema



19
20
21
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 19

def move_table_to_schema(table, schema)
  ::PgSaurus::Tools.move_table_to_schema(table, schema)
end

#rename_table(table_name, new_name, options = {}) ⇒ Object

Provide :schema option to rename_table method.



68
69
70
71
72
73
74
75
76
77
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 68

def rename_table(table_name, new_name, options = {})
  schema_name = options[:schema]
  if schema_name
    in_schema schema_name do
      super(table_name, new_name)
    end
  else
    super(table_name, new_name)
  end
end

#tables(*args) ⇒ Array<String>

Note:

Tables from public schema have no “public.” prefix. It’s done for compatibility with other libraries that relies on a table name. Tables from other schemas has appropriate prefix with schema name. See: github.com/TMXCredit/pg_power/pull/42

Make method tables return tables not only from public schema.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/schema_methods.rb', line 54

def tables(*args)
  public_tables = super(*args)

  non_public_tables =
    query("      SELECT schemaname || '.' || tablename AS table\n      FROM pg_tables\n      WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'public')\n    SQL\n\n  public_tables + non_public_tables\nend\n", 'SCHEMA').map { |row| row[0] }