Class: DBCode::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/dbcode/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, connection:) ⇒ Schema

Returns a new instance of Schema.



29
30
31
# File 'lib/dbcode/schema.rb', line 29

def initialize(name:, connection:)
  @name, @connection = name, connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



28
29
30
# File 'lib/dbcode/schema.rb', line 28

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/dbcode/schema.rb', line 28

def name
  @name
end

Instance Method Details

#append_path!(config) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/dbcode/schema.rb', line 63

def append_path!(config)
  #update all future connections
  config.merge! schema_search_path: search_path.append(name)
  #update all active connections
  connection.pool.connections.each do |connection|
    connection.schema_search_path = config[:schema_search_path]
  end
end

#digestObject



48
49
50
51
52
53
54
# File 'lib/dbcode/schema.rb', line 48

def digest
  comment = connection.select_one "    select pg_catalog.obj_description(n.oid, 'pg_namespace') as md5\n    from pg_catalog.pg_namespace n where n.nspname = '\#@name'\n  SQL\n  comment && comment['md5'] =~ /^dbcode_md5:(.+)$/ && $1\nend\n"

#digest=(digest) ⇒ Object



42
43
44
45
46
# File 'lib/dbcode/schema.rb', line 42

def digest=(digest)
  execute "    comment on schema \#@name is 'dbcode_md5:\#{digest}'\n  SQL\nend\n"

#reset!Object



35
36
37
38
39
40
# File 'lib/dbcode/schema.rb', line 35

def reset!
  execute "    drop schema if exists \#@name cascade;\n    create schema \#@name;\n  SQL\nend\n"

#within_schema(&block) ⇒ Object



56
57
58
59
60
61
# File 'lib/dbcode/schema.rb', line 56

def within_schema(&block)
  old_path = search_path
  connection.schema_search_path = old_path.prepend(name).to_s
  connection.transaction(&block)
  connection.schema_search_path = old_path.to_s
end