avm-eac_postgresql_base0 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21a34c355a48e71fa5a12b91b27238a98844495a5742b30ac79b277f7e56fec4
4
- data.tar.gz: 9e5b87bbca77cb621b8cb207c1ed220321b6bbfddddf548025461af03fa25aec
3
+ metadata.gz: 231e086f7da14eed7a6cc7ca494c5f6f2f2f95e4a5f140f4b4117e1796d8e963
4
+ data.tar.gz: 6d6a062291acb635d8e348341316641988981eb9af3fe335046cd966fa688a83
5
5
  SHA512:
6
- metadata.gz: f8edfd9211f650e24d3e93503ceda6f6cce0637abd4111289a3bdf7c2c253d35417b95a497a26a4d3721db8ed711df5ad46ff751f6324b2bf29e34130965bcbc
7
- data.tar.gz: e1ab05d6d11d6adbe238e902b787c0fb0994527d951ba2d44f79a686e4138291a9e2f1241caeb6933dc9edfee3c40b41e65ff1322c1ff26e79a77142c32408d6
6
+ metadata.gz: 1a99015236ad00d72abe000e6525cdaf283511789ca6a479e33258d991ce22fe59ce49205ce255393897e3eff83df49480b61ea1c9051ff919b1321726bff490
7
+ data.tar.gz: 61420029ee36af6cb8a25f97d778b18ec164cc8ca4e13c592128cd101df62bb6de5fb111fc29bf782dc5e5af1cabc5b8c21a669ebbcc59ab0067c0e0b29b4aac
@@ -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,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'avm/data/instance/unit'
3
+ require 'avm/instances/data/unit'
4
4
 
5
5
  module Avm
6
6
  module EacPostgresqlBase0
7
7
  class Instance
8
- class DataUnit < ::Avm::Data::Instance::Unit
8
+ class DataUnit < ::Avm::Instances::Data::Unit
9
9
  EXTENSION = '.pgdump.gz'
10
10
 
11
11
  before_load :clear_database
@@ -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.1'
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.1
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-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avm
@@ -16,34 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.12'
19
+ version: '0.56'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.12.1
22
+ version: 0.56.1
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0.12'
29
+ version: '0.56'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.12.1
32
+ version: 0.56.1
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: eac_ruby_utils
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0.82'
39
+ version: '0.107'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.107.1
40
43
  type: :runtime
41
44
  prerelease: false
42
45
  version_requirements: !ruby/object:Gem::Requirement
43
46
  requirements:
44
47
  - - "~>"
45
48
  - !ruby/object:Gem::Version
46
- version: '0.82'
49
+ version: '0.107'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.107.1
47
53
  - !ruby/object:Gem::Dependency
48
54
  name: eac_ruby_gem_support
49
55
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +72,7 @@ extra_rdoc_files: []
66
72
  files:
67
73
  - lib/avm/eac_postgresql_base0.rb
68
74
  - lib/avm/eac_postgresql_base0/instance.rb
75
+ - lib/avm/eac_postgresql_base0/instance/assert.rb
69
76
  - lib/avm/eac_postgresql_base0/instance/data_unit.rb
70
77
  - lib/avm/eac_postgresql_base0/instance_with.rb
71
78
  - lib/avm/eac_postgresql_base0/version.rb