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 /
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.
11 12 13 |
# File 'lib/prodder/pg.rb', line 11 def initialize(credentials = {}) @credentials = credentials end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
9 10 11 |
# File 'lib/prodder/pg.rb', line 9 def credentials @credentials end |
Instance Method Details
#create_db(db_name, sql = nil) ⇒ Object
32 33 34 35 |
# File 'lib/prodder/pg.rb', line 32 def create_db(db_name, sql = nil) run ['createdb', db_name] psql db_name, sql if sql end |
#create_role(role, opts = []) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/prodder/pg.rb', line 15 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
37 38 39 |
# File 'lib/prodder/pg.rb', line 37 def drop_db(db_name) run ['dropdb', db_name] end |
#drop_role(role) ⇒ Object
28 29 30 |
# File 'lib/prodder/pg.rb', line 28 def drop_role(role) run ['dropuser', role] end |
#dump_db_access_control(db_name, user_list, **options) ⇒ Object
132 133 134 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 |
# File 'lib/prodder/pg.rb', line 132 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) 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/prodder/pg.rb', line 108 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
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 |
# File 'lib/prodder/pg.rb', line 45 def dump_settings(db_name, filename) db_settings = pg_conn(db_name) { |conn| conn.exec(<<-SQL).map { |setting| setting['config'] } } select unnest(setconfig) as config 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 File.open(filename, 'w') do |f| db_settings.each do |setting| # wipe out all spaces setting.gsub!(/\s+/, '') # if the setting is empty, ignore it unless setting.empty? # else, drop carriage returns/new lines setting.chomp! # and append an empty string if the setting was being assigned a value of nothing setting += "''" if setting.match(/=$/) # using the magic of psql variables through :DBNAME f.puts "ALTER DATABASE :DBNAME SET #{setting};" end end end end |
#dump_structure(db_name, filename, **options) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/prodder/pg.rb', line 73 def dump_structure(db_name, filename, **) arguments = [ '--schema-only', '--no-privileges', '--no-owner', '--restrict-key', 'prodder', '--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
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/prodder/pg.rb', line 94 def dump_tables(db_name, tables, filename) pg_dump filename, [ '--data-only', '--no-privileges', '--no-owner', '--disable-triggers', '--restrict-key', 'prodder', '--host', credentials['host'], '--username', credentials['user'], *tables.map { |table| ['--table', table] }.flatten, db_name ] end |
#psql(db_name, sql) ⇒ Object
41 42 43 |
# File 'lib/prodder/pg.rb', line 41 def psql(db_name, sql) run ['psql', db_name], sql end |