hanami-model 1.3.2 → 1.3.3
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/LICENSE.md +1 -1
- data/README.md +6 -3
- data/hanami-model.gemspec +25 -21
- data/lib/hanami-model.rb +3 -1
- data/lib/hanami/entity.rb +5 -3
- data/lib/hanami/entity/schema.rb +9 -7
- data/lib/hanami/model.rb +14 -12
- data/lib/hanami/model/association.rb +7 -7
- data/lib/hanami/model/associations/belongs_to.rb +3 -1
- data/lib/hanami/model/associations/dsl.rb +2 -2
- data/lib/hanami/model/associations/has_many.rb +10 -8
- data/lib/hanami/model/associations/has_one.rb +9 -7
- data/lib/hanami/model/associations/many_to_many.rb +9 -11
- data/lib/hanami/model/configuration.rb +15 -13
- data/lib/hanami/model/configurator.rb +5 -3
- data/lib/hanami/model/entity_name.rb +3 -1
- data/lib/hanami/model/error.rb +9 -7
- data/lib/hanami/model/mapped_relation.rb +4 -2
- data/lib/hanami/model/mapping.rb +3 -1
- data/lib/hanami/model/migration.rb +2 -0
- data/lib/hanami/model/migrator.rb +7 -5
- data/lib/hanami/model/migrator/adapter.rb +13 -11
- data/lib/hanami/model/migrator/connection.rb +10 -8
- data/lib/hanami/model/migrator/logger.rb +3 -1
- data/lib/hanami/model/migrator/mysql_adapter.rb +23 -13
- data/lib/hanami/model/migrator/postgres_adapter.rb +30 -28
- data/lib/hanami/model/migrator/sqlite_adapter.rb +7 -9
- data/lib/hanami/model/plugins.rb +5 -3
- data/lib/hanami/model/plugins/mapping.rb +2 -0
- data/lib/hanami/model/plugins/schema.rb +2 -0
- data/lib/hanami/model/plugins/timestamps.rb +2 -0
- data/lib/hanami/model/relation_name.rb +4 -2
- data/lib/hanami/model/sql.rb +9 -7
- data/lib/hanami/model/sql/console.rb +10 -8
- data/lib/hanami/model/sql/consoles/abstract.rb +3 -1
- data/lib/hanami/model/sql/consoles/mysql.rb +4 -2
- data/lib/hanami/model/sql/consoles/postgresql.rb +10 -8
- data/lib/hanami/model/sql/consoles/sqlite.rb +6 -4
- data/lib/hanami/model/sql/entity/schema.rb +6 -4
- data/lib/hanami/model/sql/types.rb +7 -5
- data/lib/hanami/model/sql/types/schema/coercions.rb +5 -4
- data/lib/hanami/model/types.rb +4 -4
- data/lib/hanami/model/version.rb +3 -1
- data/lib/hanami/repository.rb +20 -31
- metadata +34 -3
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rom/configuration"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Model
|
@@ -30,13 +32,13 @@ module Hanami
|
|
30
32
|
def initialize(configurator)
|
31
33
|
@backend = configurator.backend
|
32
34
|
@url = configurator.url
|
33
|
-
@migrations
|
34
|
-
@schema
|
35
|
-
@gateway_config
|
36
|
-
@logger
|
35
|
+
@migrations = configurator._migrations
|
36
|
+
@schema = configurator._schema
|
37
|
+
@gateway_config = configurator._gateway
|
38
|
+
@logger = configurator._logger
|
37
39
|
@migrations_logger = configurator.migrations_logger
|
38
|
-
@mappings
|
39
|
-
@entities
|
40
|
+
@mappings = {}
|
41
|
+
@entities = {}
|
40
42
|
end
|
41
43
|
|
42
44
|
# NOTE: This must be changed when we want to support several adapters at the time
|
@@ -136,10 +138,10 @@ module Hanami
|
|
136
138
|
# @api private
|
137
139
|
def rom
|
138
140
|
@rom ||= ROM::Configuration.new(@backend, @url, infer_relations: false)
|
139
|
-
rescue =>
|
140
|
-
raise UnknownDatabaseAdapterError.new(@url) if
|
141
|
+
rescue => exception
|
142
|
+
raise UnknownDatabaseAdapterError.new(@url) if exception.message =~ /adapters/
|
141
143
|
|
142
|
-
raise
|
144
|
+
raise exception
|
143
145
|
end
|
144
146
|
|
145
147
|
# @raise [Hanami::Model::UnknownDatabaseAdapterError] if `url` is blank,
|
@@ -147,7 +149,7 @@ module Hanami
|
|
147
149
|
#
|
148
150
|
# @since 1.0.0
|
149
151
|
# @api private
|
150
|
-
def load!(repositories, &blk)
|
152
|
+
def load!(repositories, &blk)
|
151
153
|
rom.setup.auto_registration(config.directory.to_s) unless config.directory.nil?
|
152
154
|
rom.instance_eval(&blk) if block_given?
|
153
155
|
configure_gateway
|
@@ -157,8 +159,8 @@ module Hanami
|
|
157
159
|
container = ROM.container(rom)
|
158
160
|
define_entities_mappings(container, repositories)
|
159
161
|
container
|
160
|
-
rescue =>
|
161
|
-
raise Hanami::Model::Error.for(
|
162
|
+
rescue => exception
|
163
|
+
raise Hanami::Model::Error.for(exception)
|
162
164
|
end
|
163
165
|
|
164
166
|
# @since 1.0.0
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Hanami
|
2
4
|
module Model
|
3
5
|
# Configuration DSL
|
@@ -42,7 +44,7 @@ module Hanami
|
|
42
44
|
# @since 1.0.0
|
43
45
|
# @api private
|
44
46
|
def migrations_logger(stream = $stdout)
|
45
|
-
require
|
47
|
+
require "hanami/model/migrator/logger"
|
46
48
|
@migrations_logger ||= Hanami::Model::Migrator::Logger.new(stream)
|
47
49
|
end
|
48
50
|
|
@@ -76,10 +78,10 @@ module Hanami
|
|
76
78
|
# @since 1.0.0
|
77
79
|
# @api private
|
78
80
|
def logger(stream, options = {})
|
79
|
-
require
|
81
|
+
require "hanami/logger"
|
80
82
|
|
81
83
|
opts = options.merge(stream: stream)
|
82
|
-
@_logger = Hanami::Logger.new(
|
84
|
+
@_logger = Hanami::Logger.new("hanami.model", **opts)
|
83
85
|
end
|
84
86
|
|
85
87
|
# @since 1.0.0
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Hanami
|
2
4
|
module Model
|
3
5
|
# Conventional name for entities.
|
@@ -18,7 +20,7 @@ module Hanami
|
|
18
20
|
# @since 0.7.0
|
19
21
|
# @api private
|
20
22
|
def initialize(name)
|
21
|
-
@name = name.sub(SUFFIX,
|
23
|
+
@name = name.sub(SUFFIX, "")
|
22
24
|
end
|
23
25
|
|
24
26
|
# @since 0.7.0
|
data/lib/hanami/model/error.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "concurrent"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Model
|
@@ -41,7 +43,7 @@ module Hanami
|
|
41
43
|
class InvalidCommandError < Error
|
42
44
|
# @since 0.5.0
|
43
45
|
# @api private
|
44
|
-
def initialize(message =
|
46
|
+
def initialize(message = "Invalid command")
|
45
47
|
super
|
46
48
|
end
|
47
49
|
end
|
@@ -52,7 +54,7 @@ module Hanami
|
|
52
54
|
class ConstraintViolationError < Error
|
53
55
|
# @since 0.7.0
|
54
56
|
# @api private
|
55
|
-
def initialize(message =
|
57
|
+
def initialize(message = "Constraint has been violated")
|
56
58
|
super
|
57
59
|
end
|
58
60
|
end
|
@@ -63,7 +65,7 @@ module Hanami
|
|
63
65
|
class UniqueConstraintViolationError < ConstraintViolationError
|
64
66
|
# @since 0.6.1
|
65
67
|
# @api private
|
66
|
-
def initialize(message =
|
68
|
+
def initialize(message = "Unique constraint has been violated")
|
67
69
|
super
|
68
70
|
end
|
69
71
|
end
|
@@ -74,7 +76,7 @@ module Hanami
|
|
74
76
|
class ForeignKeyConstraintViolationError < ConstraintViolationError
|
75
77
|
# @since 0.6.1
|
76
78
|
# @api private
|
77
|
-
def initialize(message =
|
79
|
+
def initialize(message = "Foreign key constraint has been violated")
|
78
80
|
super
|
79
81
|
end
|
80
82
|
end
|
@@ -85,7 +87,7 @@ module Hanami
|
|
85
87
|
class NotNullConstraintViolationError < ConstraintViolationError
|
86
88
|
# @since 0.6.1
|
87
89
|
# @api private
|
88
|
-
def initialize(message =
|
90
|
+
def initialize(message = "NOT NULL constraint has been violated")
|
89
91
|
super
|
90
92
|
end
|
91
93
|
end
|
@@ -96,7 +98,7 @@ module Hanami
|
|
96
98
|
class CheckConstraintViolationError < ConstraintViolationError
|
97
99
|
# @since 0.6.1
|
98
100
|
# @api private
|
99
|
-
def initialize(message =
|
101
|
+
def initialize(message = "Check constraint has been violated")
|
100
102
|
super
|
101
103
|
end
|
102
104
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Hanami
|
2
4
|
module Model
|
3
5
|
# Mapped proxy for ROM relations.
|
@@ -51,8 +53,8 @@ module Hanami
|
|
51
53
|
# end
|
52
54
|
def [](attribute)
|
53
55
|
@relation[attribute]
|
54
|
-
rescue KeyError =>
|
55
|
-
raise UnknownAttributeError.new(
|
56
|
+
rescue KeyError => exception
|
57
|
+
raise UnknownAttributeError.new(exception.message)
|
56
58
|
end
|
57
59
|
end
|
58
60
|
end
|
data/lib/hanami/model/mapping.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sequel"
|
4
|
+
require "sequel/extensions/migration"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
module Model
|
@@ -13,8 +15,8 @@ module Hanami
|
|
13
15
|
#
|
14
16
|
# @since 0.4.0
|
15
17
|
class Migrator
|
16
|
-
require
|
17
|
-
require
|
18
|
+
require "hanami/model/migrator/connection"
|
19
|
+
require "hanami/model/migrator/adapter"
|
18
20
|
|
19
21
|
# Create database defined by current configuration.
|
20
22
|
#
|
@@ -327,7 +329,7 @@ module Hanami
|
|
327
329
|
# @see Hanami::Model::Migrator.prepare
|
328
330
|
def prepare
|
329
331
|
drop
|
330
|
-
rescue # rubocop:disable Lint/
|
332
|
+
rescue # rubocop:disable Lint/SuppressedException
|
331
333
|
ensure
|
332
334
|
create
|
333
335
|
adapter.load
|
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
require "shellwords"
|
5
|
+
require "open3"
|
4
6
|
|
5
7
|
module Hanami
|
6
8
|
module Model
|
@@ -26,18 +28,18 @@ module Hanami
|
|
26
28
|
#
|
27
29
|
# @since 0.4.0
|
28
30
|
# @api private
|
29
|
-
def self.for(configuration)
|
31
|
+
def self.for(configuration)
|
30
32
|
connection = Connection.new(configuration)
|
31
33
|
|
32
34
|
case connection.database_type
|
33
35
|
when :sqlite
|
34
|
-
require
|
36
|
+
require "hanami/model/migrator/sqlite_adapter"
|
35
37
|
SQLiteAdapter
|
36
38
|
when :postgres
|
37
|
-
require
|
39
|
+
require "hanami/model/migrator/postgres_adapter"
|
38
40
|
PostgresAdapter
|
39
41
|
when :mysql
|
40
|
-
require
|
42
|
+
require "hanami/model/migrator/mysql_adapter"
|
41
43
|
MySQLAdapter
|
42
44
|
else
|
43
45
|
self
|
@@ -80,8 +82,8 @@ module Hanami
|
|
80
82
|
version = Integer(version) unless version.nil?
|
81
83
|
|
82
84
|
Sequel::Migrator.run(connection.raw, migrations, target: version, allow_missing_migration_files: true)
|
83
|
-
rescue Sequel::Migrator::Error =>
|
84
|
-
raise MigrationError.new(
|
85
|
+
rescue Sequel::Migrator::Error => exception
|
86
|
+
raise MigrationError.new(exception.message)
|
85
87
|
end
|
86
88
|
|
87
89
|
# @since 1.1.0
|
@@ -91,8 +93,8 @@ module Hanami
|
|
91
93
|
version = version_to_rollback(table, steps)
|
92
94
|
|
93
95
|
Sequel::Migrator.run(connection.raw, migrations, target: version, allow_missing_migration_files: true)
|
94
|
-
rescue Sequel::Migrator::Error =>
|
95
|
-
raise MigrationError.new(
|
96
|
+
rescue Sequel::Migrator::Error => exception
|
97
|
+
raise MigrationError.new(exception.message)
|
96
98
|
end
|
97
99
|
|
98
100
|
# Load database schema.
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "cgi"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Model
|
@@ -36,7 +38,7 @@ module Hanami
|
|
36
38
|
# @since 0.5.0
|
37
39
|
# @api private
|
38
40
|
def host
|
39
|
-
@host ||= parsed_uri.host || parsed_opt(
|
41
|
+
@host ||= parsed_uri.host || parsed_opt("host")
|
40
42
|
end
|
41
43
|
|
42
44
|
# Returns DB connection port
|
@@ -46,7 +48,7 @@ module Hanami
|
|
46
48
|
# @since 0.5.0
|
47
49
|
# @api private
|
48
50
|
def port
|
49
|
-
@port ||= parsed_uri.port || parsed_opt(
|
51
|
+
@port ||= parsed_uri.port || parsed_opt("port").to_i.nonzero?
|
50
52
|
end
|
51
53
|
|
52
54
|
# Returns DB name from conenction
|
@@ -85,7 +87,7 @@ module Hanami
|
|
85
87
|
# @since 0.5.0
|
86
88
|
# @api private
|
87
89
|
def user
|
88
|
-
@user ||= parsed_opt(
|
90
|
+
@user ||= parsed_opt("user") || parsed_uri.user
|
89
91
|
end
|
90
92
|
|
91
93
|
# Returns user from DB connection
|
@@ -95,7 +97,7 @@ module Hanami
|
|
95
97
|
# @since 0.5.0
|
96
98
|
# @api private
|
97
99
|
def password
|
98
|
-
@password ||= parsed_opt(
|
100
|
+
@password ||= parsed_opt("password") || parsed_uri.password
|
99
101
|
end
|
100
102
|
|
101
103
|
# Returns DB connection URI directly from adapter
|
@@ -111,7 +113,7 @@ module Hanami
|
|
111
113
|
# @since 0.5.0
|
112
114
|
# @api private
|
113
115
|
def global_uri
|
114
|
-
uri.sub(parsed_uri.select(:path).first,
|
116
|
+
uri.sub(parsed_uri.select(:path).first, "")
|
115
117
|
end
|
116
118
|
|
117
119
|
# Returns a boolean telling if a DB connection is from JDBC or not
|
@@ -119,7 +121,7 @@ module Hanami
|
|
119
121
|
# @since 0.5.0
|
120
122
|
# @api private
|
121
123
|
def jdbc?
|
122
|
-
!uri.scan(
|
124
|
+
!uri.scan("jdbc:").empty?
|
123
125
|
end
|
124
126
|
|
125
127
|
# Returns database connection URI instance without JDBC namespace
|
@@ -127,7 +129,7 @@ module Hanami
|
|
127
129
|
# @since 0.5.0
|
128
130
|
# @api private
|
129
131
|
def parsed_uri
|
130
|
-
@parsed_uri ||= URI.parse(uri.sub(
|
132
|
+
@parsed_uri ||= URI.parse(uri.sub("jdbc:", ""))
|
131
133
|
end
|
132
134
|
|
133
135
|
# @api private
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Hanami
|
2
4
|
module Model
|
3
5
|
class Migrator
|
@@ -8,23 +10,27 @@ module Hanami
|
|
8
10
|
class MySQLAdapter < Adapter
|
9
11
|
# @since 0.7.0
|
10
12
|
# @api private
|
11
|
-
PASSWORD =
|
13
|
+
PASSWORD = "MYSQL_PWD"
|
14
|
+
|
15
|
+
# @since 1.3.3
|
16
|
+
# @api private
|
17
|
+
DEFAULT_PORT = 3306
|
12
18
|
|
13
19
|
# @since 1.0.0
|
14
20
|
# @api private
|
15
|
-
DB_CREATION_ERROR =
|
16
|
-
|
17
|
-
|
21
|
+
DB_CREATION_ERROR = "Database creation failed. If the database exists, " \
|
22
|
+
"then its console may be open. See this issue for more details: " \
|
23
|
+
"https://github.com/hanami/model/issues/250"
|
18
24
|
|
19
25
|
# @since 0.4.0
|
20
26
|
# @api private
|
21
27
|
def create
|
22
28
|
new_connection(global: true).run %(CREATE DATABASE `#{database}`;)
|
23
|
-
rescue Sequel::DatabaseError =>
|
24
|
-
message = if
|
29
|
+
rescue Sequel::DatabaseError => exception
|
30
|
+
message = if exception.message.match(/database exists/)
|
25
31
|
DB_CREATION_ERROR
|
26
32
|
else
|
27
|
-
|
33
|
+
exception.message
|
28
34
|
end
|
29
35
|
|
30
36
|
raise MigrationError.new(message)
|
@@ -34,11 +40,11 @@ module Hanami
|
|
34
40
|
# @api private
|
35
41
|
def drop
|
36
42
|
new_connection(global: true).run %(DROP DATABASE `#{database}`;)
|
37
|
-
rescue Sequel::DatabaseError =>
|
38
|
-
message = if
|
43
|
+
rescue Sequel::DatabaseError => exception
|
44
|
+
message = if exception.message.match(/doesn\'t exist/)
|
39
45
|
"Cannot find database: #{database}"
|
40
46
|
else
|
41
|
-
|
47
|
+
exception.message
|
42
48
|
end
|
43
49
|
|
44
50
|
raise MigrationError.new(message)
|
@@ -65,22 +71,26 @@ module Hanami
|
|
65
71
|
connection.password
|
66
72
|
end
|
67
73
|
|
74
|
+
def port
|
75
|
+
super || DEFAULT_PORT
|
76
|
+
end
|
77
|
+
|
68
78
|
# @since 0.4.0
|
69
79
|
# @api private
|
70
80
|
def dump_structure
|
71
|
-
execute "mysqldump --host=#{host} --port=#{port} --user=#{username} --no-data --skip-comments --ignore-table=#{database}.#{migrations_table} #{database} > #{schema}", env: {
|
81
|
+
execute "mysqldump --host=#{host} --port=#{port} --user=#{username} --no-data --skip-comments --ignore-table=#{database}.#{migrations_table} #{database} > #{schema}", env: {PASSWORD => password}
|
72
82
|
end
|
73
83
|
|
74
84
|
# @since 0.4.0
|
75
85
|
# @api private
|
76
86
|
def load_structure
|
77
|
-
execute("mysql --host=#{host} --port=#{port} --user=#{username} #{database} < #{escape(schema)}", env: {
|
87
|
+
execute("mysql --host=#{host} --port=#{port} --user=#{username} #{database} < #{escape(schema)}", env: {PASSWORD => password}) if schema.exist?
|
78
88
|
end
|
79
89
|
|
80
90
|
# @since 0.4.0
|
81
91
|
# @api private
|
82
92
|
def dump_migrations_data
|
83
|
-
execute "mysqldump --host=#{host} --port=#{port} --user=#{username} --skip-comments #{database} #{migrations_table} >> #{schema}", env: {
|
93
|
+
execute "mysqldump --host=#{host} --port=#{port} --user=#{username} --skip-comments #{database} #{migrations_table} >> #{schema}", env: {PASSWORD => password}
|
84
94
|
end
|
85
95
|
end
|
86
96
|
end
|
@@ -1,3 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/utils/blank"
|
4
|
+
|
1
5
|
module Hanami
|
2
6
|
module Model
|
3
7
|
class Migrator
|
@@ -8,46 +12,41 @@ module Hanami
|
|
8
12
|
class PostgresAdapter < Adapter
|
9
13
|
# @since 0.4.0
|
10
14
|
# @api private
|
11
|
-
HOST =
|
15
|
+
HOST = "PGHOST"
|
12
16
|
|
13
17
|
# @since 0.4.0
|
14
18
|
# @api private
|
15
|
-
PORT =
|
19
|
+
PORT = "PGPORT"
|
16
20
|
|
17
21
|
# @since 0.4.0
|
18
22
|
# @api private
|
19
|
-
USER =
|
23
|
+
USER = "PGUSER"
|
20
24
|
|
21
25
|
# @since 0.4.0
|
22
26
|
# @api private
|
23
|
-
PASSWORD =
|
27
|
+
PASSWORD = "PGPASSWORD"
|
24
28
|
|
25
29
|
# @since 1.0.0
|
26
30
|
# @api private
|
27
|
-
DB_CREATION_ERROR =
|
28
|
-
|
29
|
-
|
31
|
+
DB_CREATION_ERROR = "createdb: database creation failed. If the database exists, " \
|
32
|
+
"then its console may be open. See this issue for more details: " \
|
33
|
+
"https://github.com/hanami/model/issues/250"
|
30
34
|
|
31
35
|
# @since 0.4.0
|
32
36
|
# @api private
|
33
37
|
def create
|
34
|
-
|
35
|
-
|
36
|
-
call_db_command('createdb')
|
38
|
+
call_db_command("createdb")
|
37
39
|
end
|
38
40
|
|
39
41
|
# @since 0.4.0
|
40
42
|
# @api private
|
41
43
|
def drop
|
42
|
-
|
43
|
-
|
44
|
-
call_db_command('dropdb')
|
44
|
+
call_db_command("dropdb")
|
45
45
|
end
|
46
46
|
|
47
47
|
# @since 0.4.0
|
48
48
|
# @api private
|
49
49
|
def dump
|
50
|
-
set_environment_variables
|
51
50
|
dump_structure
|
52
51
|
dump_migrations_data
|
53
52
|
end
|
@@ -55,51 +54,54 @@ module Hanami
|
|
55
54
|
# @since 0.4.0
|
56
55
|
# @api private
|
57
56
|
def load
|
58
|
-
set_environment_variables
|
59
57
|
load_structure
|
60
58
|
end
|
61
59
|
|
62
60
|
private
|
63
61
|
|
64
|
-
# @since
|
62
|
+
# @since 1.3.3
|
65
63
|
# @api private
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
64
|
+
def environment_variables
|
65
|
+
{}.tap do |env|
|
66
|
+
env[HOST] = host unless host.nil?
|
67
|
+
env[PORT] = port.to_s unless port.nil?
|
68
|
+
env[PASSWORD] = password unless password.nil?
|
69
|
+
env[USER] = username unless username.nil?
|
70
|
+
end
|
71
71
|
end
|
72
72
|
|
73
73
|
# @since 0.4.0
|
74
74
|
# @api private
|
75
75
|
def dump_structure
|
76
|
-
execute "pg_dump -s -x -O -T #{migrations_table} -f #{escape(schema)} #{database}"
|
76
|
+
execute "pg_dump -s -x -O -T #{migrations_table} -f #{escape(schema)} #{database}", env: environment_variables
|
77
77
|
end
|
78
78
|
|
79
79
|
# @since 0.4.0
|
80
80
|
# @api private
|
81
81
|
def load_structure
|
82
|
-
|
82
|
+
return unless schema.exist?
|
83
|
+
|
84
|
+
execute "psql -X -q -f #{escape(schema)} #{database}", env: environment_variables
|
83
85
|
end
|
84
86
|
|
85
87
|
# @since 0.4.0
|
86
88
|
# @api private
|
87
89
|
def dump_migrations_data
|
88
90
|
error = ->(err) { raise MigrationError.new(err) unless err =~ /no matching tables/i }
|
89
|
-
execute "pg_dump -t #{migrations_table} #{database} >> #{escape(schema)}", error: error
|
91
|
+
execute "pg_dump -t #{migrations_table} #{database} >> #{escape(schema)}", error: error, env: environment_variables
|
90
92
|
end
|
91
93
|
|
92
94
|
# @since 0.5.1
|
93
95
|
# @api private
|
94
96
|
def call_db_command(command)
|
95
|
-
require
|
97
|
+
require "open3"
|
96
98
|
|
97
99
|
begin
|
98
|
-
Open3.popen3(command, database) do |_stdin, _stdout, stderr, wait_thr|
|
100
|
+
Open3.popen3(environment_variables, command, database) do |_stdin, _stdout, stderr, wait_thr|
|
99
101
|
raise MigrationError.new(modified_message(stderr.read)) unless wait_thr.value.success? # wait_thr.value is the exit status
|
100
102
|
end
|
101
|
-
rescue SystemCallError =>
|
102
|
-
raise MigrationError.new(modified_message(
|
103
|
+
rescue SystemCallError => exception
|
104
|
+
raise MigrationError.new(modified_message(exception.message))
|
103
105
|
end
|
104
106
|
end
|
105
107
|
|