ardb 0.29.1 → 0.29.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.l.yml +8 -0
- data/.rubocop.yml +3 -0
- data/.t.yml +6 -0
- data/Gemfile +4 -2
- data/ardb.gemspec +9 -6
- data/bin/ardb +2 -0
- data/lib/ardb.rb +75 -61
- data/lib/ardb/adapter/base.rb +40 -19
- data/lib/ardb/adapter/mysql.rb +2 -0
- data/lib/ardb/adapter/postgresql.rb +36 -25
- data/lib/ardb/adapter/sqlite.rb +7 -7
- data/lib/ardb/adapter_spy.rb +16 -14
- data/lib/ardb/cli.rb +23 -18
- data/lib/ardb/cli/clirb.rb +5 -0
- data/lib/ardb/cli/commands.rb +184 -95
- data/lib/ardb/db_tests.rb +2 -0
- data/lib/ardb/default_order_by.rb +13 -11
- data/lib/ardb/migration.rb +7 -4
- data/lib/ardb/record_spy.rb +42 -38
- data/lib/ardb/relation_spy.rb +27 -25
- data/lib/ardb/require_autoloaded_active_record_files.rb +3 -1
- data/lib/ardb/test_helpers.rb +11 -9
- data/lib/ardb/use_db_default.rb +9 -7
- data/lib/ardb/version.rb +3 -1
- data/script/determine_autoloaded_active_record_files.rb +22 -20
- data/test/helper.rb +2 -0
- data/test/support/factory.rb +2 -1
- data/test/support/fake_schema.rb +3 -1
- data/test/support/postgresql/schema.rb +3 -1
- data/test/support/postgresql/setup_test_db.rb +3 -1
- data/test/support/relative_require_test_db_file.rb +1 -0
- data/test/support/require_test_db_file.rb +1 -0
- data/test/unit/adapter/base_tests.rb +9 -5
- data/test/unit/adapter/mysql_tests.rb +2 -0
- data/test/unit/adapter/postgresql_tests.rb +14 -14
- data/test/unit/adapter/sqlite_tests.rb +2 -0
- data/test/unit/adapter_spy_tests.rb +4 -1
- data/test/unit/ardb_tests.rb +28 -13
- data/test/unit/cli_tests.rb +47 -34
- data/test/unit/db_tests_tests.rb +4 -1
- data/test/unit/default_order_by_tests.rb +18 -13
- data/test/unit/migration_tests.rb +8 -5
- data/test/unit/record_spy_tests.rb +21 -14
- data/test/unit/relation_spy_tests.rb +28 -22
- data/test/unit/test_helpers_tests.rb +4 -1
- data/test/unit/use_db_default_tests.rb +16 -7
- metadata +27 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 49a5b06420fd7d8b670267fd508d46efe7d600838a5808c33d25e166739e453d
|
4
|
+
data.tar.gz: cee5d9470ee881423fb3783b5607a9be982b45a34637f936a39590912241ce79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f3581d3e82f86af6b6dfcb79adab4560c577c907ac3c908febdf1b1216b7bfd473715053b87790083e4db7b44e7a0a6e0c35b0e512cdb652e75fec8c846d1e0
|
7
|
+
data.tar.gz: a63b4a6eef47151c9e22edff4177b25dce35e4a9be4914ad2a49cba22a7d785e8ac42d19e7eb0e51615d549c0f52d6a290a46e8847fd3701789261d1b10b7252
|
data/.l.yml
ADDED
data/.rubocop.yml
ADDED
data/.t.yml
ADDED
data/Gemfile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source "https://rubygems.org"
|
2
4
|
|
3
5
|
ruby "~> 2.5"
|
@@ -20,5 +22,5 @@ gem "activesupport", "~> 6.1"
|
|
20
22
|
# gem "activerecord", "~> 5.2.4"
|
21
23
|
# gem "activesupport", "~> 5.2.4"
|
22
24
|
|
23
|
-
gem "pg",
|
24
|
-
gem "pry"
|
25
|
+
gem "pg", "~> 1.1.4"
|
26
|
+
gem "pry"
|
data/ardb.gemspec
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
lib = File.expand_path("../lib", __FILE__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
6
|
require "ardb/version"
|
@@ -8,22 +10,23 @@ Gem::Specification.new do |gem|
|
|
8
10
|
gem.version = Ardb::VERSION
|
9
11
|
gem.authors = ["Kelly Redding", "Collin Redding"]
|
10
12
|
gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
|
11
|
-
gem.summary =
|
12
|
-
gem.description =
|
13
|
+
gem.summary = "Activerecord database tools."
|
14
|
+
gem.description = "Activerecord database tools."
|
13
15
|
gem.homepage = "http://github.com/redding/ardb"
|
14
16
|
gem.license = "MIT"
|
15
17
|
|
16
|
-
gem.files
|
18
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
19
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
20
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
21
|
gem.require_paths = ["lib"]
|
20
22
|
|
21
23
|
gem.required_ruby_version = "~> 2.5"
|
22
24
|
|
23
|
-
gem.add_development_dependency("
|
25
|
+
gem.add_development_dependency("much-style-guide", ["~> 0.6.0"])
|
26
|
+
gem.add_development_dependency("assert", ["~> 2.19.3"])
|
24
27
|
|
25
28
|
gem.add_dependency("activerecord", ["> 5.0", "< 7.0"])
|
26
29
|
gem.add_dependency("activesupport", ["> 5.0", "< 7.0"])
|
27
|
-
gem.add_dependency("much-
|
28
|
-
gem.add_dependency("scmd", ["~> 3.0.
|
30
|
+
gem.add_dependency("much-mixin", ["~> 0.2.4"])
|
31
|
+
gem.add_dependency("scmd", ["~> 3.0.4"])
|
29
32
|
end
|
data/bin/ardb
CHANGED
data/lib/ardb.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_record"
|
2
4
|
require "logger"
|
3
5
|
|
@@ -11,74 +13,79 @@ module Ardb
|
|
11
13
|
end
|
12
14
|
|
13
15
|
def self.configure(&block)
|
14
|
-
|
16
|
+
config.tap(&block)
|
15
17
|
end
|
16
18
|
|
17
19
|
def self.adapter
|
18
20
|
@adapter || raise(NotInitializedError.new(caller))
|
19
21
|
end
|
20
22
|
|
21
|
-
def self.reset_adapter
|
23
|
+
def self.reset_adapter
|
24
|
+
@adapter = nil
|
25
|
+
end
|
22
26
|
|
23
27
|
def self.init(establish_connection = true)
|
24
28
|
require "ardb/require_autoloaded_active_record_files"
|
25
29
|
begin
|
26
30
|
require_db_file
|
27
|
-
rescue InvalidDBFileError =>
|
28
|
-
|
29
|
-
raise
|
31
|
+
rescue InvalidDBFileError => ex
|
32
|
+
ex.set_backtrace(caller)
|
33
|
+
raise ex
|
30
34
|
end
|
31
35
|
|
32
|
-
|
33
|
-
@adapter = Adapter.new(
|
36
|
+
config.validate!
|
37
|
+
@adapter = Adapter.new(config)
|
34
38
|
|
35
39
|
# setup AR
|
36
|
-
ActiveRecord::Base.default_timezone =
|
37
|
-
ActiveRecord::Base.logger =
|
38
|
-
|
40
|
+
ActiveRecord::Base.default_timezone = config.default_timezone
|
41
|
+
ActiveRecord::Base.logger = config.logger
|
42
|
+
adapter.connect_db if establish_connection
|
39
43
|
end
|
40
44
|
|
41
45
|
def self.escape_like_pattern(pattern, escape_char = nil)
|
42
|
-
|
43
|
-
rescue NotInitializedError =>
|
44
|
-
|
45
|
-
raise
|
46
|
+
adapter.escape_like_pattern(pattern, escape_char)
|
47
|
+
rescue NotInitializedError => ex
|
48
|
+
ex.set_backtrace(caller)
|
49
|
+
raise ex
|
46
50
|
end
|
47
51
|
|
48
|
-
private
|
49
|
-
|
50
52
|
# try requiring the db file via the load path or as an absolute path, if
|
51
53
|
# that fails it tries requiring relative to the current working directory
|
52
54
|
def self.require_db_file
|
53
55
|
begin
|
54
|
-
|
56
|
+
begin
|
57
|
+
require ENV["ARDB_DB_FILE"]
|
58
|
+
rescue LoadError
|
59
|
+
require File.expand_path(ENV["ARDB_DB_FILE"], ENV["PWD"])
|
60
|
+
end
|
55
61
|
rescue LoadError
|
56
|
-
|
62
|
+
raise(
|
63
|
+
InvalidDBFileError,
|
64
|
+
"can't require `#{ENV["ARDB_DB_FILE"]}`, check that the ARDB_DB_FILE "\
|
65
|
+
"env var is set to the file path of your db file",
|
66
|
+
)
|
57
67
|
end
|
58
|
-
rescue LoadError
|
59
|
-
raise InvalidDBFileError, "can't require `#{ENV["ARDB_DB_FILE"]}`, " \
|
60
|
-
"check that the ARDB_DB_FILE env var is set to " \
|
61
|
-
"the file path of your db file"
|
62
68
|
end
|
63
69
|
|
64
70
|
class Config
|
65
|
-
ACTIVERECORD_ATTRS =
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
71
|
+
ACTIVERECORD_ATTRS =
|
72
|
+
[
|
73
|
+
:adapter,
|
74
|
+
:database,
|
75
|
+
:encoding,
|
76
|
+
:host,
|
77
|
+
:port,
|
78
|
+
:username,
|
79
|
+
:password,
|
80
|
+
:pool,
|
81
|
+
:checkout_timeout,
|
82
|
+
:min_messages,
|
83
|
+
].freeze
|
84
|
+
|
85
|
+
DEFAULT_MIGRATIONS_PATH = "db/migrations"
|
86
|
+
DEFAULT_SCHEMA_PATH = "db/schema"
|
87
|
+
RUBY_SCHEMA_FORMAT = :ruby
|
88
|
+
SQL_SCHEMA_FORMAT = :sql
|
82
89
|
VALID_SCHEMA_FORMATS = [RUBY_SCHEMA_FORMAT, SQL_SCHEMA_FORMAT].freeze
|
83
90
|
|
84
91
|
attr_accessor(*ACTIVERECORD_ATTRS)
|
@@ -104,26 +111,27 @@ module Ardb
|
|
104
111
|
end
|
105
112
|
|
106
113
|
def schema_format=(new_value)
|
107
|
-
@schema_format =
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
114
|
+
@schema_format =
|
115
|
+
begin
|
116
|
+
new_value.to_sym
|
117
|
+
rescue NoMethodError
|
118
|
+
raise ArgumentError, "schema format must be a `Symbol`", caller
|
119
|
+
end
|
112
120
|
end
|
113
121
|
|
114
122
|
def activerecord_connect_hash
|
115
123
|
ACTIVERECORD_ATTRS.inject({}) do |h, attr_name|
|
116
|
-
value =
|
124
|
+
value = send(attr_name)
|
117
125
|
!value.nil? ? h.merge!(attr_name.to_s => value) : h
|
118
126
|
end
|
119
127
|
end
|
120
128
|
|
121
129
|
def validate!
|
122
|
-
if
|
130
|
+
if adapter.to_s.empty? || database.to_s.empty?
|
123
131
|
raise ConfigurationError, "an adapter and database must be provided"
|
124
132
|
end
|
125
133
|
|
126
|
-
|
134
|
+
unless VALID_SCHEMA_FORMATS.include?(schema_format)
|
127
135
|
raise ConfigurationError, "schema format must be one of: " \
|
128
136
|
"#{VALID_SCHEMA_FORMATS.join(", ")}"
|
129
137
|
end
|
@@ -132,14 +140,14 @@ module Ardb
|
|
132
140
|
end
|
133
141
|
|
134
142
|
def ==(other)
|
135
|
-
if other.
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
+
if other.is_a?(self.class)
|
144
|
+
activerecord_connect_hash == other.activerecord_connect_hash &&
|
145
|
+
default_timezone == other.default_timezone &&
|
146
|
+
logger == other.logger &&
|
147
|
+
root_path == other.root_path &&
|
148
|
+
schema_format == other.schema_format &&
|
149
|
+
migrations_path == other.migrations_path &&
|
150
|
+
schema_path == other.schema_path
|
143
151
|
else
|
144
152
|
super
|
145
153
|
end
|
@@ -153,14 +161,14 @@ module Ardb
|
|
153
161
|
"postgresql",
|
154
162
|
"postgres",
|
155
163
|
"mysql",
|
156
|
-
"mysql2"
|
164
|
+
"mysql2",
|
157
165
|
].freeze
|
158
166
|
|
159
167
|
def self.new(config)
|
160
|
-
|
168
|
+
unless VALID_ADAPTERS.include?(config.adapter)
|
161
169
|
raise InvalidAdapterError, "invalid adapter: `#{config.adapter}`"
|
162
170
|
end
|
163
|
-
|
171
|
+
send(config.adapter, config)
|
164
172
|
end
|
165
173
|
|
166
174
|
def self.sqlite(config)
|
@@ -168,21 +176,27 @@ module Ardb
|
|
168
176
|
Adapter::Sqlite.new(config)
|
169
177
|
end
|
170
178
|
|
171
|
-
def self.sqlite3(config)
|
179
|
+
def self.sqlite3(config)
|
180
|
+
sqlite(config)
|
181
|
+
end
|
172
182
|
|
173
183
|
def self.postgresql(config)
|
174
184
|
require "ardb/adapter/postgresql"
|
175
185
|
Adapter::Postgresql.new(config)
|
176
186
|
end
|
177
187
|
|
178
|
-
def self.postgres(config)
|
188
|
+
def self.postgres(config)
|
189
|
+
postgresql(config)
|
190
|
+
end
|
179
191
|
|
180
192
|
def self.mysql(config)
|
181
193
|
require "ardb/adapter/mysql"
|
182
194
|
Adapter::Mysql.new(config)
|
183
195
|
end
|
184
196
|
|
185
|
-
def self.mysql2(config)
|
197
|
+
def self.mysql2(config)
|
198
|
+
mysql(config)
|
199
|
+
end
|
186
200
|
end
|
187
201
|
|
188
202
|
InvalidDBFileError = Class.new(ArgumentError)
|
data/lib/ardb/adapter/base.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "ardb"
|
2
4
|
|
3
5
|
module Ardb; end
|
6
|
+
|
4
7
|
module Ardb::Adapter
|
5
8
|
class Base
|
6
9
|
attr_reader :config
|
@@ -10,17 +13,28 @@ module Ardb::Adapter
|
|
10
13
|
validate!
|
11
14
|
end
|
12
15
|
|
13
|
-
def connect_hash
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
def connect_hash
|
17
|
+
config.activerecord_connect_hash
|
18
|
+
end
|
19
|
+
|
20
|
+
def database
|
21
|
+
config.database
|
22
|
+
end
|
23
|
+
|
24
|
+
def migrations_path
|
25
|
+
config.migrations_path
|
26
|
+
end
|
27
|
+
|
28
|
+
def schema_format
|
29
|
+
config.schema_format
|
30
|
+
end
|
17
31
|
|
18
32
|
def ruby_schema_path
|
19
|
-
@ruby_schema_path ||= "#{
|
33
|
+
@ruby_schema_path ||= "#{config.schema_path}.rb"
|
20
34
|
end
|
21
35
|
|
22
36
|
def sql_schema_path
|
23
|
-
@sql_schema_path ||= "#{
|
37
|
+
@sql_schema_path ||= "#{config.schema_path}.sql"
|
24
38
|
end
|
25
39
|
|
26
40
|
def escape_like_pattern(pattern, escape_char = nil)
|
@@ -32,16 +46,23 @@ module Ardb::Adapter
|
|
32
46
|
pattern
|
33
47
|
end
|
34
48
|
|
35
|
-
def create_db(*args)
|
36
|
-
|
49
|
+
def create_db(*args)
|
50
|
+
raise NotImplementedError
|
51
|
+
end
|
52
|
+
|
53
|
+
def drop_db(*args)
|
54
|
+
raise NotImplementedError
|
55
|
+
end
|
37
56
|
|
38
|
-
def drop_tables(*args)
|
57
|
+
def drop_tables(*args)
|
58
|
+
raise NotImplementedError
|
59
|
+
end
|
39
60
|
|
40
61
|
def connect_db
|
41
|
-
ActiveRecord::Base.establish_connection(
|
62
|
+
ActiveRecord::Base.establish_connection(connect_hash)
|
42
63
|
# checkout a connection to ensure we can connect to the DB, we don"t do
|
43
64
|
# anything with the connection and immediately check it back in
|
44
|
-
ActiveRecord::Base.connection_pool.with_connection{
|
65
|
+
ActiveRecord::Base.connection_pool.with_connection{}
|
45
66
|
end
|
46
67
|
|
47
68
|
def migrate_db
|
@@ -67,13 +88,13 @@ module Ardb::Adapter
|
|
67
88
|
def load_schema
|
68
89
|
current_stdout = $stdout.dup # silence STDOUT
|
69
90
|
$stdout = File.new("/dev/null", "w")
|
70
|
-
load_ruby_schema if
|
71
|
-
load_sql_schema if
|
91
|
+
load_ruby_schema if schema_format == Ardb::Config::RUBY_SCHEMA_FORMAT
|
92
|
+
load_sql_schema if schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
|
72
93
|
$stdout = current_stdout
|
73
94
|
end
|
74
95
|
|
75
96
|
def load_ruby_schema
|
76
|
-
load
|
97
|
+
load ruby_schema_path
|
77
98
|
end
|
78
99
|
|
79
100
|
def load_sql_schema
|
@@ -84,14 +105,14 @@ module Ardb::Adapter
|
|
84
105
|
current_stdout = $stdout.dup # silence STDOUT
|
85
106
|
$stdout = File.new("/dev/null", "w")
|
86
107
|
dump_ruby_schema
|
87
|
-
dump_sql_schema if
|
108
|
+
dump_sql_schema if schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
|
88
109
|
$stdout = current_stdout
|
89
110
|
end
|
90
111
|
|
91
112
|
def dump_ruby_schema
|
92
113
|
require "active_record/schema_dumper"
|
93
|
-
FileUtils.mkdir_p File.dirname(
|
94
|
-
File.open(
|
114
|
+
FileUtils.mkdir_p File.dirname(ruby_schema_path)
|
115
|
+
File.open(ruby_schema_path, "w:utf-8") do |file|
|
95
116
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
96
117
|
end
|
97
118
|
end
|
@@ -101,8 +122,8 @@ module Ardb::Adapter
|
|
101
122
|
end
|
102
123
|
|
103
124
|
def ==(other)
|
104
|
-
if other.
|
105
|
-
|
125
|
+
if other.is_a?(self.class)
|
126
|
+
config == other.config
|
106
127
|
else
|
107
128
|
super
|
108
129
|
end
|
data/lib/ardb/adapter/mysql.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "ardb/adapter/base"
|
2
4
|
|
3
5
|
module Ardb::Adapter
|
@@ -6,32 +8,38 @@ module Ardb::Adapter
|
|
6
8
|
# connect to) db that typically exists for all postgres installations; the
|
7
9
|
# adapter uses it to create/drop other databases
|
8
10
|
def public_connect_hash
|
9
|
-
@public_connect_hash ||=
|
10
|
-
"database"
|
11
|
-
"schema_search_path" => "public"
|
11
|
+
@public_connect_hash ||= connect_hash.merge({
|
12
|
+
"database" => "postgres",
|
13
|
+
"schema_search_path" => "public",
|
12
14
|
})
|
13
15
|
end
|
14
16
|
|
15
17
|
def create_db
|
16
|
-
ActiveRecord::Base.establish_connection(
|
17
|
-
ActiveRecord::Base.connection.create_database(
|
18
|
-
|
18
|
+
ActiveRecord::Base.establish_connection(public_connect_hash)
|
19
|
+
ActiveRecord::Base.connection.create_database(
|
20
|
+
database,
|
21
|
+
connect_hash,
|
22
|
+
)
|
23
|
+
ActiveRecord::Base.establish_connection(connect_hash)
|
19
24
|
end
|
20
25
|
|
21
26
|
def drop_db
|
22
27
|
begin
|
23
|
-
ActiveRecord::Base.establish_connection(
|
28
|
+
ActiveRecord::Base.establish_connection(public_connect_hash)
|
24
29
|
ActiveRecord::Base.connection.tap do |conn|
|
25
|
-
conn.execute
|
26
|
-
|
27
|
-
|
30
|
+
conn.execute(
|
31
|
+
"UPDATE pg_catalog.pg_database"\
|
32
|
+
" SET datallowconn=false WHERE datname='#{database}'",
|
33
|
+
)
|
34
|
+
# this SELECT actually runs a command: it terminates all the
|
35
|
+
# connections
|
28
36
|
# http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL-TABLE
|
29
37
|
conn.execute "SELECT pg_terminate_backend(pid)"\
|
30
|
-
" FROM pg_stat_activity WHERE datname='#{
|
31
|
-
conn.execute "DROP DATABASE IF EXISTS #{
|
38
|
+
" FROM pg_stat_activity WHERE datname='#{database}'"
|
39
|
+
conn.execute "DROP DATABASE IF EXISTS #{database}"
|
32
40
|
end
|
33
|
-
rescue PG::Error =>
|
34
|
-
raise
|
41
|
+
rescue PG::Error => ex
|
42
|
+
raise ex unless ex.message =~ /does not exist/
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
@@ -40,42 +48,45 @@ module Ardb::Adapter
|
|
40
48
|
tables = conn.execute "SELECT table_name"\
|
41
49
|
" FROM information_schema.tables"\
|
42
50
|
" WHERE table_schema = 'public';"
|
43
|
-
tables.each
|
51
|
+
tables.each do |row|
|
52
|
+
conn.execute "DROP TABLE #{row["table_name"]} CASCADE"
|
53
|
+
end
|
44
54
|
end
|
45
55
|
end
|
46
56
|
|
47
57
|
def load_sql_schema
|
48
58
|
require "scmd"
|
49
|
-
cmd_str = "psql -f \"#{
|
50
|
-
cmd = Scmd.new(cmd_str, :
|
59
|
+
cmd_str = "psql -f \"#{sql_schema_path}\" #{database}"
|
60
|
+
cmd = Scmd.new(cmd_str, env: env_var_hash).tap(&:run)
|
51
61
|
raise "Error loading database" unless cmd.success?
|
52
62
|
end
|
53
63
|
|
54
64
|
def dump_sql_schema
|
55
65
|
require "scmd"
|
56
|
-
cmd_str =
|
57
|
-
|
66
|
+
cmd_str =
|
67
|
+
"pg_dump -i -s -x -O -f \"#{sql_schema_path}\" #{database}"
|
68
|
+
cmd = Scmd.new(cmd_str, env: env_var_hash).tap(&:run)
|
58
69
|
raise "Error dumping database" unless cmd.success?
|
59
70
|
end
|
60
71
|
|
61
72
|
private
|
62
73
|
|
63
74
|
def validate!
|
64
|
-
if
|
75
|
+
if database =~ /\W/
|
65
76
|
raise(
|
66
77
|
Ardb::ConfigurationError,
|
67
78
|
"database value must not contain non-word characters. "\
|
68
|
-
"Given: #{
|
79
|
+
"Given: #{database.inspect}.",
|
69
80
|
)
|
70
81
|
end
|
71
82
|
end
|
72
83
|
|
73
84
|
def env_var_hash
|
74
85
|
@env_var_hash ||= {
|
75
|
-
"PGHOST"
|
76
|
-
"PGPORT"
|
77
|
-
"PGUSER"
|
78
|
-
"PGPASSWORD" =>
|
86
|
+
"PGHOST" => connect_hash["host"],
|
87
|
+
"PGPORT" => connect_hash["port"],
|
88
|
+
"PGUSER" => connect_hash["username"],
|
89
|
+
"PGPASSWORD" => connect_hash["password"],
|
79
90
|
}
|
80
91
|
end
|
81
92
|
end
|