avm-eac_postgresql_base0 0.2.4 → 0.4.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: 03a08a9048593955e5cc62a604c6dfc94e21abe79e995d1933a3de00b908f18a
4
- data.tar.gz: d29c537ded1c570897830df081f2c36f5101f5e3f508392e7279e21743465709
3
+ metadata.gz: 87fd0b8fc37e185451163ffce33b07b4e9dae7ac8a4c9b0e8b9b345abe31bd92
4
+ data.tar.gz: aa7ca433255e681a839e3d66f94b3cc15f59316aa5358f1d342c1068bc1c6ea4
5
5
  SHA512:
6
- metadata.gz: 55b5a9708048a4d3fc467db99a3960e0dca5c505fdc8e07d3933c542fe0665532f5006ba3faa8bdd3f58ae0cdeedac3bea1c210056a59e7ec45a759d9c8de69d
7
- data.tar.gz: e3d579fc45bda1bea8666ca036989b6bd684348668edebdffb5b699add72e38e20e15a55d0b911d52d5d4fc9fdc084d3d9512fcceacb99edfe8da43353515d46
6
+ metadata.gz: 79a4baf2e58a6d56b7046badcc10a8b6d2456f2e42c459c8356668942fa104f256d18f3a80996a54bc8f706624be4b6e21002871e83632119165903bb55fca5c
7
+ data.tar.gz: f73ad27f21f5a99fb84782504b891e20f3f659cbea4c4afc793709f37c855c25f46ba20158524811623e7980ecac5e608836ae92858270381115c0fec6e362d5
@@ -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,9 +7,23 @@ 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
- before_load :clear_database
15
+ before_load :clear
16
+
17
+ def clear
18
+ info 'Clearing database (Dropping all tables)...'
19
+ ts = tables
20
+ if ts.empty?
21
+ info 'Database has no tables'
22
+ else
23
+ info "Removing #{ts.count} table(s)..."
24
+ run_sql(drop_tables_sql(ts))
25
+ end
26
+ end
13
27
 
14
28
  def dump_command
15
29
  instance.dump_gzip_command
@@ -21,23 +35,32 @@ module Avm
21
35
 
22
36
  private
23
37
 
24
- def clear_database
25
- info 'Clearing database (Dropping all tables)...'
26
- ts = tables
27
- if ts.empty?
28
- info 'Database has no tables'
29
- else
30
- info "Removing #{ts.count} table(s)..."
31
- run_sql('drop table if exists ' + ts.map { |t| "\"#{t}\"" }.join(', ') + ' cascade')
32
- end
38
+ # @param table_list [Array<String>]
39
+ # @return [String]
40
+ def drop_tables_sql(table_list)
41
+ 'drop table ' + table_list.map(&:to_s).join(', ') + ' cascade'
42
+ end
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('.')
33
49
  end
34
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.4'
5
+ VERSION = '0.4.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.4
4
+ version: 0.4.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: 2023-04-14 00:00:00.000000000 Z
11
+ date: 2023-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: avm
@@ -16,48 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.66'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 0.66.1
19
+ version: '0.69'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '0.66'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 0.66.1
26
+ version: '0.69'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: eac_ruby_utils
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
31
  - - "~>"
38
32
  - !ruby/object:Gem::Version
39
- version: '0.112'
33
+ version: '0.113'
40
34
  type: :runtime
41
35
  prerelease: false
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
38
  - - "~>"
45
39
  - !ruby/object:Gem::Version
46
- version: '0.112'
40
+ version: '0.113'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: eac_ruby_gem_support
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
45
  - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: '0.4'
47
+ version: 0.5.1
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
52
  - - "~>"
59
53
  - !ruby/object:Gem::Version
60
- version: '0.4'
54
+ version: 0.5.1
61
55
  description:
62
56
  email:
63
57
  executables: []
@@ -67,6 +61,7 @@ files:
67
61
  - lib/avm/eac_postgresql_base0.rb
68
62
  - lib/avm/eac_postgresql_base0/instance.rb
69
63
  - lib/avm/eac_postgresql_base0/instance/assert.rb
64
+ - lib/avm/eac_postgresql_base0/instance/commands.rb
70
65
  - lib/avm/eac_postgresql_base0/instance/data_unit.rb
71
66
  - lib/avm/eac_postgresql_base0/instance_with.rb
72
67
  - lib/avm/eac_postgresql_base0/version.rb