Class: Prodder::PG
- Inherits:
-
Object
- Object
- Prodder::PG
- Defined in:
- lib/prodder/pg.rb
Constant Summary collapse
- PGDumpError =
Class.new(StandardError)
- ACL_GRANT =
From pg_dump
/^GRANT /- ACL_REVOKE =
/^REVOKE /- DEFAULT_PRIVILEGES =
/^ALTER DEFAULT PRIVILEGES /- SET_OBJECT_OWNERSHIP =
/.* OWNER TO /- SEARCH_PATH =
/SET search_path = .*/
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
Instance Method Summary collapse
- #create_db(db_name, sql = nil) ⇒ Object
- #create_role(role, opts = []) ⇒ Object
- #drop_db(db_name) ⇒ Object
- #drop_role(role) ⇒ Object
- #dump_db_access_control(db_name, user_list, options) ⇒ Object
- #dump_permissions(db_name, filename, options = {}) ⇒ Object
- #dump_settings(db_name, filename) ⇒ Object
- #dump_structure(db_name, filename, options = {}) ⇒ Object
- #dump_tables(db_name, tables, filename) ⇒ Object
-
#initialize(credentials = {}) ⇒ PG
constructor
A new instance of PG.
- #psql(db_name, sql) ⇒ Object
Constructor Details
#initialize(credentials = {}) ⇒ PG
Returns a new instance of PG.
10 11 12 |
# File 'lib/prodder/pg.rb', line 10 def initialize(credentials = {}) @credentials = credentials end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
8 9 10 |
# File 'lib/prodder/pg.rb', line 8 def credentials @credentials end |
Instance Method Details
#create_db(db_name, sql = nil) ⇒ Object
31 32 33 34 |
# File 'lib/prodder/pg.rb', line 31 def create_db(db_name, sql = nil) run ['createdb', db_name] psql db_name, sql if sql end |
#create_role(role, opts = []) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/prodder/pg.rb', line 14 def create_role(role, opts = []) arguments = [ 'createuser', '--no-password', '--no-superuser', '--no-createrole', '--no-createdb' ] arguments.push *opts, role run arguments end |
#drop_db(db_name) ⇒ Object
36 37 38 |
# File 'lib/prodder/pg.rb', line 36 def drop_db(db_name) run ['dropdb', db_name] end |
#drop_role(role) ⇒ Object
27 28 29 |
# File 'lib/prodder/pg.rb', line 27 def drop_role(role) run ['dropuser', role] end |
#dump_db_access_control(db_name, user_list, options) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/prodder/pg.rb', line 135 def dump_db_access_control(db_name, user_list, ) perm_out_sql = "" arguments = [ '--schema-only', '--host', credentials['host'], '--username', credentials['user'] ] if [:exclude_schemas].respond_to? :map arguments.concat [:exclude_schemas].map { |schema| ['--exclude-schema', schema] }.flatten end if [:exclude_tables].respond_to? :map arguments.concat [:exclude_tables].map { |table| ['--exclude-table', table] }.flatten end arguments.push db_name white_list = [:included_users] || [] irrelevant_login_roles = irrelevant_login_roles(db_name, white_list).map { |user| user['rolname'] } run ['pg_dump', *arguments] do |out, err, success| out.each_line do |line| if line.match(ACL_GRANT) || line.match(ACL_REVOKE) || line.match(DEFAULT_PRIVILEGES) || line.match(SET_OBJECT_OWNERSHIP)|| line.match(SEARCH_PATH) unless irrelevant_login_roles.include?(line.match(/ (\S*);$/)[1]) perm_out_sql << line user_list << (line.match(/ (\S*);$/)[1]).gsub(/"/, '') end end end raise PGDumpError.new(err) if !success end user_list.uniq! perm_out_sql end |
#dump_permissions(db_name, filename, options = {}) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/prodder/pg.rb', line 110 def (db_name, filename, = {}) perm_out_sql = "" user_list = [] perm_out_sql << dump_db_access_control(db_name, user_list, ) perm_out_sql.prepend pg_dumpall db_name, user_list, perm_out_sql.prepend(alter_role_function) perm_out_sql.prepend(create_role_function) perm_out_sql.prepend(granted_by_function) perm_out_sql.prepend("SET client_min_messages TO WARNING;\n") perm_out_sql << drop_role_create_function perm_out_sql << drop_role_alter_function perm_out_sql << drop_granted_by_function File.open(filename, 'w') { |f| f.write perm_out_sql } end |
#dump_settings(db_name, filename) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/prodder/pg.rb', line 44 def dump_settings(db_name, filename) sql = <<-SQL select unnest(setconfig) from pg_catalog.pg_db_role_setting join pg_database on pg_database.oid = setdatabase -- 0 = default, for all users where setrole = 0 and datname = '#{db_name}' SQL arguments = [ '--host', credentials['host'], '--username', credentials['user'], '-t', '-c', sql ] run ['psql', *arguments.push(db_name)] do |out, err, success| raise PGDumpError.new(err) if !success File.open(filename, 'w') do |f| out.each_line do |setting| setting.gsub!(/\s+/, '') unless setting.empty? setting.chomp! setting += "''" if setting.match(/=$/) # using the magic of psql variables through :DBNAME f.puts "ALTER DATABASE :DBNAME SET #{setting};" end end end end end |
#dump_structure(db_name, filename, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/prodder/pg.rb', line 77 def dump_structure(db_name, filename, = {}) arguments = [ '--schema-only', '--no-privileges', '--no-owner', '--host', credentials['host'], '--username', credentials['user'] ] if [:exclude_schemas].respond_to? :map arguments.concat [:exclude_schemas].map { |schema| ['--exclude-schema', schema] }.flatten end if [:exclude_tables].respond_to? :map arguments.concat [:exclude_tables].map { |table| ['--exclude-table', table] }.flatten end pg_dump filename, arguments.push(db_name) end |
#dump_tables(db_name, tables, filename) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/prodder/pg.rb', line 97 def dump_tables(db_name, tables, filename) pg_dump filename, [ '--data-only', '--no-privileges', '--no-owner', '--disable-triggers', '--host', credentials['host'], '--username', credentials['user'], *tables.map { |table| ['--table', table] }.flatten, db_name ] end |
#psql(db_name, sql) ⇒ Object
40 41 42 |
# File 'lib/prodder/pg.rb', line 40 def psql(db_name, sql) run ['psql', db_name], sql end |