avm-eac_postgresql_base0 0.2.4 → 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: 03a08a9048593955e5cc62a604c6dfc94e21abe79e995d1933a3de00b908f18a
4
- data.tar.gz: d29c537ded1c570897830df081f2c36f5101f5e3f508392e7279e21743465709
3
+ metadata.gz: f8c272a517744e8283d0f09ef9a1551f1270ebc0a7304bdd52545f1281b40f50
4
+ data.tar.gz: ac0e819f4a3efcb8797456d2e650a9157dfd15ae4dadcadee919682b4858db3d
5
5
  SHA512:
6
- metadata.gz: 55b5a9708048a4d3fc467db99a3960e0dca5c505fdc8e07d3933c542fe0665532f5006ba3faa8bdd3f58ae0cdeedac3bea1c210056a59e7ec45a759d9c8de69d
7
- data.tar.gz: e3d579fc45bda1bea8666ca036989b6bd684348668edebdffb5b699add72e38e20e15a55d0b911d52d5d4fc9fdc084d3d9512fcceacb99edfe8da43353515d46
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 if exists ' + 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.4'
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.4
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: 2023-04-14 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,20 +16,14 @@ 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.68'
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.68'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: eac_ruby_utils
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -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