avm-eac_postgresql_base0 0.2.3 → 0.3.0

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: 84e0a14809137d8b08980f06cb62e81f4ea0142bd3d7c0f3d646a906f49be832
4
- data.tar.gz: 8e3f20b69e7af8cc0e4a1445438d0a3696c60bc8debdf9654e180a1c1f51662e
3
+ metadata.gz: f8c272a517744e8283d0f09ef9a1551f1270ebc0a7304bdd52545f1281b40f50
4
+ data.tar.gz: ac0e819f4a3efcb8797456d2e650a9157dfd15ae4dadcadee919682b4858db3d
5
5
  SHA512:
6
- metadata.gz: 520e4c244648f74bd1ba2f870abe3e6e3f4c58d22bf06f4c0b04e6dd926fc401f1e6d7492bfaa37c2a77d52683c85b522c4d74ed60b8824fa99823ca762885c3
7
- data.tar.gz: 6c47c1cb2dec4acd690ce7ff7750f38f29b11c5714e2191ceff4cba4601c5da7669ed7f4c1b1058da87a0e5eecd35f2de70e841bee92032f0393ad205b3191a2
6
+ metadata.gz: 4354892b67aab47fd790da261be0adfdefe914225a10bd7036a056612760674082623a08b59d9036ad410703ce491216927060ff02ebcddde529e454d9003037
7
+ data.tar.gz: 3e518942ed62e30b44b1f7f5178f45632591e5d1b449fd86674d1e0a37b68e65416b1204fafcf8b85ff33facf5b11cadca291e85b53dbd194e6d265071a8160c
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'avm/eac_postgresql_base0/instance/data_unit'
4
+ require 'eac_ruby_utils/core_ext'
5
+
6
+ module Avm
7
+ module EacPostgresqlBase0
8
+ class Instance
9
+ module Commands
10
+ DUMP_EXCLUDE_PATTERN = '^(CREATE|COMMENT ON) EXTENSION'
11
+
12
+ # @return [EacRubyUtils::Envs::Command]
13
+ def dump_command
14
+ env.command('pg_dump', '--no-privileges', '--no-owner', *common_command_args)
15
+ .envvar('PGPASSWORD', password)
16
+ .pipe(remove_extensions_ddl)
17
+ end
18
+
19
+ # @return [EacRubyUtils::Envs::Command]
20
+ def dump_gzip_command
21
+ dump_command.pipe(gzip_compress_command)
22
+ end
23
+
24
+ # @return [EacRubyUtils::Envs::Command]
25
+ def gzip_compress_command
26
+ env.command('gzip', '-9', '-c')
27
+ end
28
+
29
+ # @return [EacRubyUtils::Envs::Command]
30
+ def gzip_decompress_command
31
+ env.command('gzip', '-d')
32
+ end
33
+
34
+ # @return [EacRubyUtils::Envs::Command]
35
+ def load_gzip_command
36
+ gzip_decompress_command.pipe(psql_command)
37
+ end
38
+
39
+ # @return [String]
40
+ def password_command_argument
41
+ "@ESC_PGPASSWORD=#{password}"
42
+ end
43
+
44
+ def psql_command(database = true)
45
+ env.command(password_command_argument, 'psql', '--variable', 'ON_ERROR_STOP=t',
46
+ *common_command_args(database))
47
+ end
48
+
49
+ def psql_command_command(sql, database = true)
50
+ psql_command(database).append(['--quiet', '--tuples-only', '--command', sql])
51
+ end
52
+
53
+ def root_psql_command(sql = nil)
54
+ args = ['sudo', '-u', 'postgres', 'psql']
55
+ args += ['--quiet', '--tuples-only', '--command', sql] if sql.present?
56
+ env.command(*args)
57
+ end
58
+
59
+ def common_command_args(database = true)
60
+ ['--host', host, '--username', user, '--port', port,
61
+ (database ? name : MAINTENANCE_DATABASE)]
62
+ end
63
+
64
+ private
65
+
66
+ # @return [EacRubyUtils::Envs::Command]
67
+ def remove_extensions_ddl
68
+ env.command('grep', '--invert-match', '--extended-regexp', DUMP_EXCLUDE_PATTERN)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -7,7 +7,10 @@ module Avm
7
7
  class Instance
8
8
  class DataUnit < ::Avm::Instances::Data::Unit
9
9
  EXTENSION = '.pgdump.gz'
10
- TABLES_SQL = 'select tablename from pg_tables where schemaname = \'public\''
10
+ SCHEMA_VAR = '%%SCHEMA%%'
11
+ TABLE_PARTS_SEPARATOR = '/'
12
+ TABLES_SQL = "select schemaname || '#{TABLE_PARTS_SEPARATOR}' || tablename from " \
13
+ "pg_tables where schemaname = '#{SCHEMA_VAR}'"
11
14
 
12
15
  before_load :clear_database
13
16
 
@@ -21,6 +24,12 @@ module Avm
21
24
 
22
25
  private
23
26
 
27
+ # @param table_list [Array<String>]
28
+ # @return [String]
29
+ def drop_tables_sql(table_list)
30
+ 'drop table ' + table_list.map(&:to_s).join(', ') + ' cascade'
31
+ end
32
+
24
33
  def clear_database
25
34
  info 'Clearing database (Dropping all tables)...'
26
35
  ts = tables
@@ -28,16 +37,30 @@ module Avm
28
37
  info 'Database has no tables'
29
38
  else
30
39
  info "Removing #{ts.count} table(s)..."
31
- run_sql('drop table ' + ts.map { |t| "\"#{t}\"" }.join(', ') + ' cascade')
40
+ run_sql(drop_tables_sql(ts))
32
41
  end
33
42
  end
34
43
 
44
+ # @param parts [Array<String>, Strings]
45
+ # @return [String]
46
+ def join_table_parts(parts)
47
+ parts = parts.to_s.split(TABLE_PARTS_SEPARATOR) unless parts.is_a?(::Enumerable)
48
+ parts.map { |p| "\"#{p}\"" }.join('.')
49
+ end
50
+
35
51
  def run_sql(sql)
36
52
  instance.psql_command_command(sql).execute!
37
53
  end
38
54
 
55
+ # @return [Array<String>]
39
56
  def tables
40
- run_sql(TABLES_SQL).each_line.map(&:strip).reject(&:blank?)
57
+ run_sql(tables_sql).each_line.map(&:strip).reject(&:blank?)
58
+ .map { |line| join_table_parts(line) }
59
+ end
60
+
61
+ # @return [String]
62
+ def tables_sql
63
+ TABLES_SQL.gsub(SCHEMA_VAR, instance.schema)
41
64
  end
42
65
  end
43
66
  end
@@ -6,8 +6,11 @@ require 'eac_ruby_utils/core_ext'
6
6
  module Avm
7
7
  module EacPostgresqlBase0
8
8
  class Instance
9
- require_sub __FILE__
9
+ require_sub __FILE__, include_modules: true
10
10
 
11
+ DEFAULT_HOSTNAME = '127.0.0.1'
12
+ DEFAULT_PORT = 5432
13
+ DEFAULT_SCHEMA = 'public'
11
14
  MAINTENANCE_DATABASE = 'postgres'
12
15
 
13
16
  common_constructor :env, :connection_params do
@@ -22,61 +25,12 @@ module Avm
22
25
  ::Avm::EacPostgresqlBase0::Instance::DataUnit.new(self)
23
26
  end
24
27
 
25
- def dump_command
26
- env.command('pg_dump', '--no-privileges', '--clean', '--no-owner', *common_command_args)
27
- .envvar('PGPASSWORD', password)
28
- end
29
-
30
- # @return [EacRubyUtils::Envs::Command]
31
- def dump_gzip_command
32
- dump_command.pipe(gzip_compress_command)
33
- end
34
-
35
- # @return [EacRubyUtils::Envs::Command]
36
- def gzip_compress_command
37
- env.command('gzip', '-9', '-c')
38
- end
39
-
40
- # @return [EacRubyUtils::Envs::Command]
41
- def gzip_decompress_command
42
- env.command('gzip', '-d')
43
- end
44
-
45
- # @return [EacRubyUtils::Envs::Command]
46
- def load_gzip_command
47
- gzip_decompress_command.pipe(psql_command)
48
- end
49
-
50
- # @return [String]
51
- def password_command_argument
52
- "@ESC_PGPASSWORD=#{password}"
53
- end
54
-
55
- def psql_command(database = true)
56
- env.command(password_command_argument, 'psql', *common_command_args(database))
57
- end
58
-
59
- def psql_command_command(sql, database = true)
60
- psql_command(database).append(['--quiet', '--tuples-only', '--command', sql])
61
- end
62
-
63
- def root_psql_command(sql = nil)
64
- args = ['sudo', '-u', 'postgres', 'psql']
65
- args += ['--quiet', '--tuples-only', '--command', sql] if sql.present?
66
- env.command(*args)
67
- end
68
-
69
- def common_command_args(database = true)
70
- ['--host', host, '--username', user, '--port', port,
71
- (database ? name : MAINTENANCE_DATABASE)]
72
- end
73
-
74
28
  def host
75
- connection_params[:host] || '127.0.0.1'
29
+ connection_params[:host] || DEFAULT_HOSTNAME
76
30
  end
77
31
 
78
32
  def port
79
- connection_params[:port] || '5432'
33
+ connection_params[:port] || DEFAULT_PORT
80
34
  end
81
35
 
82
36
  def user
@@ -90,6 +44,10 @@ module Avm
90
44
  def name
91
45
  connection_params.fetch(:name)
92
46
  end
47
+
48
+ def schema
49
+ connection_params[:schema] || DEFAULT_SCHEMA
50
+ end
93
51
  end
94
52
  end
95
53
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Avm
4
4
  module EacPostgresqlBase0
5
- VERSION = '0.2.3'
5
+ VERSION = '0.3.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.2.3
4
+ version: 0.3.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: 2022-12-16 00:00:00.000000000 Z
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avm
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.57'
19
+ version: '0.68'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.57'
26
+ version: '0.68'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: eac_ruby_utils
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.109'
33
+ version: '0.112'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.109'
40
+ version: '0.112'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: eac_ruby_gem_support
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -61,6 +61,7 @@ files:
61
61
  - lib/avm/eac_postgresql_base0.rb
62
62
  - lib/avm/eac_postgresql_base0/instance.rb
63
63
  - lib/avm/eac_postgresql_base0/instance/assert.rb
64
+ - lib/avm/eac_postgresql_base0/instance/commands.rb
64
65
  - lib/avm/eac_postgresql_base0/instance/data_unit.rb
65
66
  - lib/avm/eac_postgresql_base0/instance_with.rb
66
67
  - lib/avm/eac_postgresql_base0/version.rb