avm-eac_postgresql_base0 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21a34c355a48e71fa5a12b91b27238a98844495a5742b30ac79b277f7e56fec4
4
- data.tar.gz: 9e5b87bbca77cb621b8cb207c1ed220321b6bbfddddf548025461af03fa25aec
3
+ metadata.gz: 5a75bf3a56aaa8d9940062215c2bb24c9b0b5e9c6953abaa1e1b6fa10a2a7102
4
+ data.tar.gz: acf8c3cd099779f780fa841dbf9b655cbb37149b6b3d5c72479de4d4b47b96d5
5
5
  SHA512:
6
- metadata.gz: f8edfd9211f650e24d3e93503ceda6f6cce0637abd4111289a3bdf7c2c253d35417b95a497a26a4d3721db8ed711df5ad46ff751f6324b2bf29e34130965bcbc
7
- data.tar.gz: e1ab05d6d11d6adbe238e902b787c0fb0994527d951ba2d44f79a686e4138291a9e2f1241caeb6933dc9edfee3c40b41e65ff1322c1ff26e79a77142c32408d6
6
+ metadata.gz: 1c2fd992697a8da42c92724649bb26cea3f376becee96a7fae81881b41459c06ff7cd5c749082632d8c64020e56ef1b71701a28e8350dd1e3a70ce9d70069227
7
+ data.tar.gz: 59bfb018e82084fb4f532285d2abbb14ab1f2be0286d8b87c05f5246ac11542bfe60b1a2c8bdbe8489b7b8c67e9fba40ddfc782ff45cfa933e551d94dfd24f23
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module Avm
6
+ module EacPostgresqlBase0
7
+ class Instance
8
+ class Assert
9
+ common_constructor :instance
10
+ delegate :name, :password, :user, to: :instance
11
+
12
+ def perform
13
+ create_user unless user_exist?
14
+ change_password unless password_ok?
15
+ create_database unless database_exist?
16
+ change_owner unless user_owner?
17
+ end
18
+
19
+ def change_owner
20
+ root_execute("ALTER DaTABASE \"#{name}\" OWNER TO \"#{user}\"")
21
+ end
22
+
23
+ def change_password
24
+ root_execute("ALTER USER \"#{user}\" WITH PASSWORD '#{password}'")
25
+ end
26
+
27
+ def create_user
28
+ root_execute("CREATE USER \"#{user}\" WITH PASSWORD '#{password}'")
29
+ end
30
+
31
+ def current_owner
32
+ root_query(<<~SQL
33
+ SELECT pg_catalog.pg_get_userbyid(datdba)
34
+ FROM pg_catalog.pg_database
35
+ WHERE datname = '#{name}'
36
+ SQL
37
+ )
38
+ end
39
+
40
+ def database_exist?
41
+ root_boolean_query("FROM pg_database WHERE datname='#{name}'")
42
+ end
43
+
44
+ def password_ok?
45
+ instance.psql_command_command('SELECT 1', false)
46
+ .execute!(exit_outputs: { 512 => 'login_failed' }).strip == '1'
47
+ end
48
+
49
+ def user_exist?
50
+ root_boolean_query("FROM pg_roles WHERE rolname='#{user}'")
51
+ end
52
+
53
+ def user_owner?
54
+ user == current_owner
55
+ end
56
+
57
+ def create_database
58
+ root_execute("CREATE DATABASE \"#{name}\" WITH OWNER \"#{user}\"")
59
+ end
60
+
61
+ private
62
+
63
+ def root_boolean_query(sql_after_projection)
64
+ root_query("SELECT 1 #{sql_after_projection}") == '1'
65
+ end
66
+
67
+ def root_execute(sql)
68
+ instance.root_psql_command(sql).execute!
69
+ end
70
+
71
+ def root_query(sql)
72
+ root_execute(sql).strip
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,13 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'avm/eac_postgresql_base0/instance/data_unit'
4
+ require 'eac_ruby_utils/core_ext'
4
5
 
5
6
  module Avm
6
7
  module EacPostgresqlBase0
7
8
  class Instance
8
- def initialize(env, connection_params)
9
- @env = env
10
- @connection_params = connection_params.with_indifferent_access
9
+ require_sub __FILE__
10
+
11
+ MAINTENANCE_DATABASE = 'postgres'
12
+
13
+ common_constructor :env, :connection_params do
14
+ self.connection_params = connection_params.with_indifferent_access
15
+ end
16
+
17
+ def assert
18
+ ::Avm::EacPostgresqlBase0::Instance::Assert.new(self).perform
11
19
  end
12
20
 
13
21
  def data_unit
@@ -23,20 +31,23 @@ module Avm
23
31
  dump_command.append(['@ESC_|', 'gzip', '-9', '-c'])
24
32
  end
25
33
 
26
- def psql_command
27
- env.command("@ESC_PGPASSWORD=#{password}", 'psql', *common_command_args)
34
+ def psql_command(database = true)
35
+ env.command("@ESC_PGPASSWORD=#{password}", 'psql', *common_command_args(database))
28
36
  end
29
37
 
30
- def psql_command_command(sql)
31
- psql_command.append(['--quiet', '--tuples-only', '--command', sql])
38
+ def psql_command_command(sql, database = true)
39
+ psql_command(database).append(['--quiet', '--tuples-only', '--command', sql])
32
40
  end
33
41
 
34
- private
35
-
36
- attr_reader :env, :connection_params
42
+ def root_psql_command(sql = nil)
43
+ args = ['sudo', '-u', 'postgres', 'psql']
44
+ args += ['--quiet', '--tuples-only', '--command', sql] if sql.present?
45
+ env.command(*args)
46
+ end
37
47
 
38
- def common_command_args
39
- ['--host', host, '--username', user, '--port', port, name]
48
+ def common_command_args(database = true)
49
+ ['--host', host, '--username', user, '--port', port,
50
+ (database ? name : MAINTENANCE_DATABASE)]
40
51
  end
41
52
 
42
53
  def host
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Avm
4
4
  module EacPostgresqlBase0
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avm-eac_postgresql_base0
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Put here the authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-11 00:00:00.000000000 Z
11
+ date: 2022-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avm
@@ -66,6 +66,7 @@ extra_rdoc_files: []
66
66
  files:
67
67
  - lib/avm/eac_postgresql_base0.rb
68
68
  - lib/avm/eac_postgresql_base0/instance.rb
69
+ - lib/avm/eac_postgresql_base0/instance/assert.rb
69
70
  - lib/avm/eac_postgresql_base0/instance/data_unit.rb
70
71
  - lib/avm/eac_postgresql_base0/instance_with.rb
71
72
  - lib/avm/eac_postgresql_base0/version.rb