ardb 0.29.1 → 0.29.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.l.yml +8 -0
  3. data/.rubocop.yml +3 -0
  4. data/.t.yml +6 -0
  5. data/Gemfile +4 -2
  6. data/ardb.gemspec +9 -6
  7. data/bin/ardb +2 -0
  8. data/lib/ardb.rb +75 -61
  9. data/lib/ardb/adapter/base.rb +40 -19
  10. data/lib/ardb/adapter/mysql.rb +2 -0
  11. data/lib/ardb/adapter/postgresql.rb +36 -25
  12. data/lib/ardb/adapter/sqlite.rb +7 -7
  13. data/lib/ardb/adapter_spy.rb +16 -14
  14. data/lib/ardb/cli.rb +23 -18
  15. data/lib/ardb/cli/clirb.rb +5 -0
  16. data/lib/ardb/cli/commands.rb +184 -95
  17. data/lib/ardb/db_tests.rb +2 -0
  18. data/lib/ardb/default_order_by.rb +13 -11
  19. data/lib/ardb/migration.rb +7 -4
  20. data/lib/ardb/record_spy.rb +42 -38
  21. data/lib/ardb/relation_spy.rb +27 -25
  22. data/lib/ardb/require_autoloaded_active_record_files.rb +3 -1
  23. data/lib/ardb/test_helpers.rb +11 -9
  24. data/lib/ardb/use_db_default.rb +9 -7
  25. data/lib/ardb/version.rb +3 -1
  26. data/script/determine_autoloaded_active_record_files.rb +22 -20
  27. data/test/helper.rb +2 -0
  28. data/test/support/factory.rb +2 -1
  29. data/test/support/fake_schema.rb +3 -1
  30. data/test/support/postgresql/schema.rb +3 -1
  31. data/test/support/postgresql/setup_test_db.rb +3 -1
  32. data/test/support/relative_require_test_db_file.rb +1 -0
  33. data/test/support/require_test_db_file.rb +1 -0
  34. data/test/unit/adapter/base_tests.rb +9 -5
  35. data/test/unit/adapter/mysql_tests.rb +2 -0
  36. data/test/unit/adapter/postgresql_tests.rb +14 -14
  37. data/test/unit/adapter/sqlite_tests.rb +2 -0
  38. data/test/unit/adapter_spy_tests.rb +4 -1
  39. data/test/unit/ardb_tests.rb +28 -13
  40. data/test/unit/cli_tests.rb +47 -34
  41. data/test/unit/db_tests_tests.rb +4 -1
  42. data/test/unit/default_order_by_tests.rb +18 -13
  43. data/test/unit/migration_tests.rb +8 -5
  44. data/test/unit/record_spy_tests.rb +21 -14
  45. data/test/unit/relation_spy_tests.rb +28 -22
  46. data/test/unit/test_helpers_tests.rb +4 -1
  47. data/test/unit/use_db_default_tests.rb +16 -7
  48. metadata +27 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50d874e2df679ac2dff5d8617591ec70eb92cdf236324c6b9ba58e815c3a0f97
4
- data.tar.gz: 28023038c65d75d082cd49545934657e3e11efb9be64cf7055335d09a68707ef
3
+ metadata.gz: 49a5b06420fd7d8b670267fd508d46efe7d600838a5808c33d25e166739e453d
4
+ data.tar.gz: cee5d9470ee881423fb3783b5607a9be982b45a34637f936a39590912241ce79
5
5
  SHA512:
6
- metadata.gz: b7a3cfcb5f7bc262540e68ff4b0d259b8d9d95d5b109e521506229b4276bad96d3123c8cce6e7b0bf4f36777041fdbe2c714a17ca1f39e29846ba5d11b81b80b
7
- data.tar.gz: 8cb2f15786e1a57c35ce32a39522aab648aa361ef29f4e0e6e8b63f70a3d5de3f41bfad99477751195a9d1ae68adf8363980211b1bd841841a508d57425246b0
6
+ metadata.gz: 1f3581d3e82f86af6b6dfcb79adab4560c577c907ac3c908febdf1b1216b7bfd473715053b87790083e4db7b44e7a0a6e0c35b0e512cdb652e75fec8c846d1e0
7
+ data.tar.gz: a63b4a6eef47151c9e22edff4177b25dce35e4a9be4914ad2a49cba22a7d785e8ac42d19e7eb0e51615d549c0f52d6a290a46e8847fd3701789261d1b10b7252
data/.l.yml ADDED
@@ -0,0 +1,8 @@
1
+ # https://github.com/redding/l.rb
2
+
3
+ linters:
4
+ - name: "Rubocop"
5
+ cmd: "bundle exec rubocop"
6
+ extensions:
7
+ - ".rb"
8
+ cli_abbrev: "u"
@@ -0,0 +1,3 @@
1
+ inherit_gem:
2
+ much-style-guide:
3
+ - "lib/much-style-guide/rubocop.yml"
data/.t.yml ADDED
@@ -0,0 +1,6 @@
1
+ # https://github.com/redding/t.rb
2
+
3
+ default_cmd: bundle exec assert
4
+ test_dir: test
5
+ test_file_suffixes:
6
+ - "_tests.rb"
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", "~> 1.1.4"
24
- gem "pry", "~> 0.12.2"
25
+ gem "pg", "~> 1.1.4"
26
+ gem "pry"
@@ -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 = %q{Activerecord database tools.}
12
- gem.description = %q{Activerecord database tools.}
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 = `git ls-files`.split($/)
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("assert", ["~> 2.19.0"])
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-plugin", ["~> 0.2.1"])
28
- gem.add_dependency("scmd", ["~> 3.0.3"])
30
+ gem.add_dependency("much-mixin", ["~> 0.2.4"])
31
+ gem.add_dependency("scmd", ["~> 3.0.4"])
29
32
  end
data/bin/ardb CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  #
3
5
  # Copyright (c) 2013-Present Kelly Redding and Collin Redding
4
6
  #
@@ -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
- self.config.tap(&block)
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; @adapter = nil; end
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 => exception
28
- exception.set_backtrace(caller)
29
- raise exception
31
+ rescue InvalidDBFileError => ex
32
+ ex.set_backtrace(caller)
33
+ raise ex
30
34
  end
31
35
 
32
- self.config.validate!
33
- @adapter = Adapter.new(self.config)
36
+ config.validate!
37
+ @adapter = Adapter.new(config)
34
38
 
35
39
  # setup AR
36
- ActiveRecord::Base.default_timezone = self.config.default_timezone
37
- ActiveRecord::Base.logger = self.config.logger
38
- self.adapter.connect_db if establish_connection
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
- self.adapter.escape_like_pattern(pattern, escape_char)
43
- rescue NotInitializedError => exception
44
- exception.set_backtrace(caller)
45
- raise exception
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
- require ENV["ARDB_DB_FILE"]
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
- require File.expand_path(ENV["ARDB_DB_FILE"], ENV["PWD"])
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
- :adapter,
67
- :database,
68
- :encoding,
69
- :host,
70
- :port,
71
- :username,
72
- :password,
73
- :pool,
74
- :checkout_timeout,
75
- :min_messages
76
- ].freeze
77
-
78
- DEFAULT_MIGRATIONS_PATH = "db/migrations".freeze
79
- DEFAULT_SCHEMA_PATH = "db/schema".freeze
80
- RUBY_SCHEMA_FORMAT = :ruby.freeze
81
- SQL_SCHEMA_FORMAT = :sql.freeze
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 = begin
108
- new_value.to_sym
109
- rescue NoMethodError
110
- raise ArgumentError, "schema format must be a `Symbol`", caller
111
- end
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 = self.send(attr_name)
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 self.adapter.to_s.empty? || self.database.to_s.empty?
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
- if !VALID_SCHEMA_FORMATS.include?(self.schema_format)
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.kind_of?(self.class)
136
- self.activerecord_connect_hash == other.activerecord_connect_hash &&
137
- self.default_timezone == other.default_timezone &&
138
- self.logger == other.logger &&
139
- self.root_path == other.root_path &&
140
- self.schema_format == other.schema_format &&
141
- self.migrations_path == other.migrations_path &&
142
- self.schema_path == other.schema_path
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
- if !VALID_ADAPTERS.include?(config.adapter)
168
+ unless VALID_ADAPTERS.include?(config.adapter)
161
169
  raise InvalidAdapterError, "invalid adapter: `#{config.adapter}`"
162
170
  end
163
- self.send(config.adapter, config)
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); self.sqlite(config); end
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); self.postgresql(config); end
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); self.mysql(config); end
197
+ def self.mysql2(config)
198
+ mysql(config)
199
+ end
186
200
  end
187
201
 
188
202
  InvalidDBFileError = Class.new(ArgumentError)
@@ -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; self.config.activerecord_connect_hash; end
14
- def database; self.config.database; end
15
- def migrations_path; self.config.migrations_path; end
16
- def schema_format; self.config.schema_format; end
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 ||= "#{self.config.schema_path}.rb"
33
+ @ruby_schema_path ||= "#{config.schema_path}.rb"
20
34
  end
21
35
 
22
36
  def sql_schema_path
23
- @sql_schema_path ||= "#{self.config.schema_path}.sql"
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); raise NotImplementedError; end
36
- def drop_db(*args); raise NotImplementedError; end
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); raise NotImplementedError; end
57
+ def drop_tables(*args)
58
+ raise NotImplementedError
59
+ end
39
60
 
40
61
  def connect_db
41
- ActiveRecord::Base.establish_connection(self.connect_hash)
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 self.schema_format == Ardb::Config::RUBY_SCHEMA_FORMAT
71
- load_sql_schema if self.schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
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 self.ruby_schema_path
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 self.schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
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(self.ruby_schema_path)
94
- File.open(self.ruby_schema_path, "w:utf-8") do |file|
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.kind_of?(self.class)
105
- self.config == other.config
125
+ if other.is_a?(self.class)
126
+ config == other.config
106
127
  else
107
128
  super
108
129
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "ardb/adapter/base"
2
4
 
3
5
  module Ardb::Adapter
@@ -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 ||= self.connect_hash.merge({
10
- "database" => "postgres",
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(self.public_connect_hash)
17
- ActiveRecord::Base.connection.create_database(self.database, self.connect_hash)
18
- ActiveRecord::Base.establish_connection(self.connect_hash)
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(self.public_connect_hash)
28
+ ActiveRecord::Base.establish_connection(public_connect_hash)
24
29
  ActiveRecord::Base.connection.tap do |conn|
25
- conn.execute "UPDATE pg_catalog.pg_database"\
26
- " SET datallowconn=false WHERE datname='#{self.database}'"
27
- # this SELECT actually runs a command: it terminates all the connections
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='#{self.database}'"
31
- conn.execute "DROP DATABASE IF EXISTS #{self.database}"
38
+ " FROM pg_stat_activity WHERE datname='#{database}'"
39
+ conn.execute "DROP DATABASE IF EXISTS #{database}"
32
40
  end
33
- rescue PG::Error => e
34
- raise e unless e.message =~ /does not exist/
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{ |row| conn.execute "DROP TABLE #{row["table_name"]} CASCADE" }
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 \"#{self.sql_schema_path}\" #{self.database}"
50
- cmd = Scmd.new(cmd_str, :env => env_var_hash).tap(&:run)
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 = "pg_dump -i -s -x -O -f \"#{self.sql_schema_path}\" #{self.database}"
57
- cmd = Scmd.new(cmd_str, :env => env_var_hash).tap(&:run)
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 self.database =~ /\W/
75
+ if database =~ /\W/
65
76
  raise(
66
77
  Ardb::ConfigurationError,
67
78
  "database value must not contain non-word characters. "\
68
- "Given: #{self.database.inspect}."
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" => self.connect_hash["host"],
76
- "PGPORT" => self.connect_hash["port"],
77
- "PGUSER" => self.connect_hash["username"],
78
- "PGPASSWORD" => self.connect_hash["password"]
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