ardb 0.28.1 → 0.29.2

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.
Files changed (59) hide show
  1. checksums.yaml +7 -7
  2. data/.l.yml +8 -0
  3. data/.rubocop.yml +3 -0
  4. data/.t.yml +6 -0
  5. data/Gemfile +21 -8
  6. data/README.md +252 -3
  7. data/ardb.gemspec +14 -10
  8. data/bin/ardb +3 -1
  9. data/lib/ardb.rb +110 -80
  10. data/lib/ardb/adapter/base.rb +73 -47
  11. data/lib/ardb/adapter/mysql.rb +4 -17
  12. data/lib/ardb/adapter/postgresql.rb +51 -46
  13. data/lib/ardb/adapter/sqlite.rb +11 -15
  14. data/lib/ardb/adapter_spy.rb +18 -30
  15. data/lib/ardb/cli.rb +29 -24
  16. data/lib/ardb/cli/clirb.rb +19 -17
  17. data/lib/ardb/cli/commands.rb +308 -129
  18. data/lib/ardb/db_tests.rb +4 -4
  19. data/lib/ardb/default_order_by.rb +13 -21
  20. data/lib/ardb/migration.rb +15 -16
  21. data/lib/ardb/record_spy.rb +46 -61
  22. data/lib/ardb/relation_spy.rb +27 -31
  23. data/lib/ardb/require_autoloaded_active_record_files.rb +174 -58
  24. data/lib/ardb/test_helpers.rb +13 -14
  25. data/lib/ardb/use_db_default.rb +10 -19
  26. data/lib/ardb/version.rb +3 -1
  27. data/script/determine_autoloaded_active_record_files.rb +31 -24
  28. data/test/helper.rb +6 -13
  29. data/test/support/factory.rb +4 -3
  30. data/test/support/fake_schema.rb +3 -1
  31. data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
  32. data/test/support/postgresql/schema.rb +2 -1
  33. data/test/support/postgresql/setup_test_db.rb +17 -15
  34. data/test/support/relative_require_test_db_file.rb +1 -0
  35. data/test/support/require_test_db_file.rb +1 -0
  36. data/test/system/.keep +0 -0
  37. data/test/unit/adapter/base_tests.rb +83 -55
  38. data/test/unit/adapter/mysql_tests.rb +4 -19
  39. data/test/unit/adapter/postgresql_tests.rb +21 -30
  40. data/test/unit/adapter/sqlite_tests.rb +5 -11
  41. data/test/unit/adapter_spy_tests.rb +6 -17
  42. data/test/unit/ardb_tests.rb +81 -53
  43. data/test/unit/cli_tests.rb +232 -157
  44. data/test/unit/db_tests_tests.rb +7 -7
  45. data/test/unit/default_order_by_tests.rb +21 -20
  46. data/test/unit/migration_tests.rb +17 -18
  47. data/test/unit/record_spy_tests.rb +36 -34
  48. data/test/unit/relation_spy_tests.rb +40 -63
  49. data/test/unit/test_helpers_tests.rb +7 -15
  50. data/test/unit/use_db_default_tests.rb +22 -17
  51. metadata +117 -84
  52. data/lib/ardb/has_slug.rb +0 -107
  53. data/lib/ardb/migration_helpers.rb +0 -77
  54. data/lib/ardb/pg_json.rb +0 -90
  55. data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
  56. data/test/system/pg_json_tests.rb +0 -85
  57. data/test/unit/has_slug_tests.rb +0 -341
  58. data/test/unit/migration_helpers_tests.rb +0 -65
  59. data/test/unit/pg_json_tests.rb +0 -39
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
- ---
2
- SHA512:
3
- metadata.gz: 3a33083aa8a07a009733067aef0a4ccf2d54f59a9a8ef0bb928e54239930fbfa8ff9a5338fdfc1a872104dd3d1d96dbf628802467b5c1ff60f4c6230b87b0e47
4
- data.tar.gz: 63c06b03761b7e1e38fed261aa4168db133019130a5dc8170b908605a7b3594292bb05dd099b5ad779cea0d645703ef20379b5a1babff7a83b4ba3ee4a8ccfb5
5
- SHA1:
6
- metadata.gz: c1b302324ad7f7951179fd764b611ac576aabae6
7
- data.tar.gz: 51d0ce79158fb332dcf788e4d1c7625c19389800
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 49a5b06420fd7d8b670267fd508d46efe7d600838a5808c33d25e166739e453d
4
+ data.tar.gz: cee5d9470ee881423fb3783b5607a9be982b45a34637f936a39590912241ce79
5
+ SHA512:
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,13 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
- gemspec
5
+ ruby "~> 2.5"
4
6
 
5
- gem 'pg', "~> 0.17.1"
6
- gem 'pry', "~> 0.9.0"
7
+ gemspec
7
8
 
8
- # Lock down gem versions because they require a newer version of ruby
9
- gem 'i18n', "< 0.7"
9
+ # Uncomment one of these and run:
10
+ #
11
+ # bundle update activerecord activesupport
12
+ #
13
+ # to test a specific version.
14
+ #
15
+ # Rails 6.1
16
+ gem "activerecord", "~> 6.1"
17
+ gem "activesupport", "~> 6.1"
18
+ # Rails 6.0
19
+ # gem "activerecord", "~> 6.0.3"
20
+ # gem "activesupport", "~> 6.0.3"
21
+ # Rails 5.2
22
+ # gem "activerecord", "~> 5.2.4"
23
+ # gem "activesupport", "~> 5.2.4"
10
24
 
11
- platform :ruby_18 do
12
- gem 'json', '~> 1.8'
13
- end
25
+ gem "pg", "~> 1.1.4"
26
+ gem "pry"
data/README.md CHANGED
@@ -1,16 +1,265 @@
1
1
  # Ardb
2
2
 
3
- Activerecord database tools.
3
+ Tools for using ActiveRecord with or without Rails.
4
4
 
5
5
  ## Usage
6
6
 
7
- TODO: Write code samples and usage instructions here
7
+ Given configured database connection parameters, Ardb provides a CLI and assorted tools for working with an ActiveRecord database. Ardb is designed to be used with or without Rails.
8
+
9
+ ### Configuration
10
+
11
+ By default, Ardb looks for database configuration in the `config/db.rb` file. You can override this using the `ENV["ARDB_DB_FILE"]` env var.
12
+
13
+ The configuration includes typical database configuration parameters:
14
+
15
+ ```ruby
16
+ # in config/db.rb
17
+ require "ardb"
18
+
19
+ Ardb.configure do |c|
20
+ c.logger Logger.new($stdout)
21
+ c.root_path File.expand_path("../..", __FILE__)
22
+
23
+ c.db.adapter "postgresql"
24
+ c.db.encoding "unicode"
25
+ c.db.min_messages "WARNING"
26
+ c.db.url "localhost:5432"
27
+ c.db.username "testuser"
28
+ c.db.password "secret"
29
+ c.db.database "testdb"
30
+ end
31
+ ```
32
+
33
+ #### Rails configuration
34
+
35
+ If using Ardb with Rails, add a `config/db.rb` file to have Ardb use Rails's configuration settings:
36
+
37
+ ```ruby
38
+ # in config/db.rb
39
+ require_relative "./environment"
40
+ require "ardb"
41
+
42
+ # This Ardb configuration matches Rails's settings.
43
+ Ardb.configure do |c|
44
+ rails_db_config = Rails.application.config_for("database")
45
+ c.root_path = Rails.root
46
+ c.logger = Rails.logger
47
+ c.schema_format = Rails.application.config.active_record.schema_format || :ruby
48
+ c.default_timezone = :utc
49
+ c.adapter = rails_db_config["adapter"]
50
+ c.host = rails_db_config["host"]
51
+ c.port = rails_db_config["port"]
52
+ c.username = rails_db_config["username"]
53
+ c.password = rails_db_config["password"]
54
+ c.database = rails_db_config["database"]
55
+ c.encoding = rails_db_config["encoding"]
56
+ c.min_messages = rails_db_config["min_messages"]
57
+
58
+ c.migrations_path = "db/migrate"
59
+ c.schema_path = "db/schema"
60
+ end
61
+ ```
62
+
63
+ ### CLI
64
+
65
+ ```
66
+ $ ardb --help
67
+ Usage: ardb [COMMAND] [options]
68
+
69
+ Options:
70
+ --version
71
+ --help
72
+
73
+ Commands:
74
+ connect Connect to the configured DB
75
+ create Create the configured DB
76
+ drop Drop the configured DB
77
+ generate-migration Generate a MIGRATION-NAME migration file
78
+ migrate Migrate the configured DB
79
+ migrate-up Migrate the configured DB up
80
+ migrate-down Migrate the configured DB down
81
+ migrate-forward Migrate the configured DB forward
82
+ migrate-backward Migrate the configured DB backward
83
+ ```
84
+
85
+ #### `connect` command
86
+
87
+ ```
88
+ $ ardb connect --help
89
+ Usage: ardb connect [options]
90
+
91
+ Options:
92
+ --version
93
+ --help
94
+
95
+ Description:
96
+ Connect to the configured DB
97
+ $ ardb connect
98
+ error: database "some_database" does not exist
99
+ $ ardb create
100
+ created postgresql db "some_database"
101
+ $ ardb connect
102
+ connected to postgresql db "some_database"
103
+ ```
104
+
105
+ Use this command to verify the connection parameter configuration is correct.
106
+
107
+ #### `create` command
108
+
109
+ ```
110
+ $ ardb create --help
111
+ Usage: ardb create [options]
112
+
113
+ Options:
114
+ --version
115
+ --help
116
+
117
+ Description:
118
+ Create the configured DB
119
+ $ ardb create
120
+ created postgresql db "some_database"
121
+ $ ardb create
122
+ error: database "some_database" already exists
123
+ ```
124
+
125
+ #### `drop` command
126
+
127
+ ```
128
+ $ ardb drop --help
129
+ Usage: ardb drop [options]
130
+
131
+ Options:
132
+ --version
133
+ --help
134
+
135
+ Description:
136
+ Drop the configured DB
137
+ $ ardb drop
138
+ dropped postgresql db "some_database"
139
+ $ ardb drop
140
+ error: database "some_database" does not exist
141
+ ```
142
+
143
+ #### `generate-migration` command
144
+
145
+ ```
146
+ $ ardb generate-migration add_projects --help
147
+ Usage: ardb generate-migration MIGRATION-NAME [options]
148
+
149
+ Options:
150
+ --version
151
+ --help
152
+
153
+ Description:
154
+ Generate a MIGRATION-NAME migration file
155
+ $ ardb generate-migration add_projects
156
+ generated /path/to/app/db/migrate/20191222074043_add_projects.rb
157
+ ```
158
+
159
+ #### `migrate` command
160
+
161
+ ```
162
+ $ ardb migrate --help
163
+ Usage: ardb migrate [options]
164
+
165
+ Options:
166
+ --version
167
+ --help
168
+
169
+ Description:
170
+ Migrate the configured DB
171
+ $ ardb migrate
172
+ == 20191222074043 AddProjects: migrating ======================================
173
+ -- create_table(:projects)
174
+ -> 0.0276s
175
+ == 20191222074043 AddProjects: migrated (0.0277s) =============================
176
+ ```
177
+
178
+ #### `migrate-up` command
179
+
180
+ ```
181
+ $ ardb migrate-up --help
182
+ Usage: ardb migrate-up [options]
183
+
184
+ Options:
185
+ -t, --target-version VALUE version to migrate to
186
+ --version
187
+ --help
188
+
189
+ Description:
190
+ Migrate the configured DB up
191
+ $ ardb migrate-up
192
+ == 20191222074043 AddProjects: migrating ======================================
193
+ -- create_table(:projects)
194
+ -> 0.0510s
195
+ == 20191222074043 AddProjects: migrated (0.0511s) =============================
196
+ ```
197
+
198
+ #### `migrate-down` command
199
+
200
+ ```
201
+ $ ardb migrate-down --help
202
+ Usage: ardb migrate-down [options]
203
+
204
+ Options:
205
+ -t, --target-version VALUE version to migrate to
206
+ --version
207
+ --help
208
+
209
+ Description:
210
+ Migrate the configured DB down
211
+ $ ardb migrate-down
212
+ == 20191222074043 AddProjects: reverting ======================================
213
+ -- drop_table(:projects)
214
+ -> 0.0092s
215
+ == 20191222074043 AddProjects: reverted (0.0132s) =============================
216
+ ```
217
+
218
+ #### `migrate-forward` command
219
+
220
+ ```
221
+ $ ardb migrate-forward --help
222
+ Usage: ardb migrate-forward [options]
223
+
224
+ Options:
225
+ -s, --steps VALUE number of migrations to migrate
226
+ --version
227
+ --help
228
+
229
+ Description:
230
+ Migrate the configured DB forward
231
+ $ ardb migrate-forward
232
+ == 20191222074043 AddProjects: migrating ======================================
233
+ -- create_table(:projects)
234
+ -> 0.0510s
235
+ == 20191222074043 AddProjects: migrated (0.0511s) =============================
236
+ ```
237
+
238
+ #### `migrate-backward` command
239
+
240
+ ```
241
+ $ ardb migrate-backward --help
242
+ Usage: ardb migrate-backward [options]
243
+
244
+ Options:
245
+ -s, --steps VALUE number of migrations to migrate
246
+ --version
247
+ --help
248
+
249
+ Description:
250
+ Migrate the configured DB backward
251
+ $ ardb migrate-backward
252
+ == 20191222074043 AddProjects: reverting ======================================
253
+ -- drop_table(:projects)
254
+ -> 0.0092s
255
+ == 20191222074043 AddProjects: reverted (0.0132s) =============================
256
+ ```
8
257
 
9
258
  ## Installation
10
259
 
11
260
  Add this line to your application's Gemfile:
12
261
 
13
- gem 'ardb'
262
+ gem "ardb"
14
263
 
15
264
  And then execute:
16
265
 
@@ -1,5 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __FILE__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require "ardb/version"
5
7
 
@@ -8,21 +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
- gem.license = 'MIT'
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
- gem.add_development_dependency("assert", ["~> 2.16.1"])
23
+ gem.required_ruby_version = "~> 2.5"
22
24
 
23
- gem.add_dependency('activerecord', ["~> 3.2"])
24
- gem.add_dependency('activesupport', ["~> 3.2"])
25
- gem.add_dependency('much-plugin', ["~> 0.2.0"])
26
- gem.add_dependency('scmd', ["~> 3.0.2"])
25
+ gem.add_development_dependency("much-style-guide", ["~> 0.6.0"])
26
+ gem.add_development_dependency("assert", ["~> 2.19.3"])
27
27
 
28
+ gem.add_dependency("activerecord", ["> 5.0", "< 7.0"])
29
+ gem.add_dependency("activesupport", ["> 5.0", "< 7.0"])
30
+ gem.add_dependency("much-mixin", ["~> 0.2.4"])
31
+ gem.add_dependency("scmd", ["~> 3.0.4"])
28
32
  end
data/bin/ardb CHANGED
@@ -1,7 +1,9 @@
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
  #
5
7
 
6
- require 'ardb/cli'
8
+ require "ardb/cli"
7
9
  Ardb::CLI.run ARGV
@@ -1,89 +1,105 @@
1
- require 'active_record'
2
- require 'logger'
1
+ # frozen_string_literal: true
3
2
 
4
- require 'ardb/version'
3
+ require "active_record"
4
+ require "logger"
5
5
 
6
- ENV['ARDB_DB_FILE'] ||= 'config/db'
6
+ require "ardb/version"
7
7
 
8
- module Ardb
8
+ ENV["ARDB_DB_FILE"] ||= "config/db"
9
9
 
10
+ module Ardb
10
11
  def self.config
11
12
  @config ||= Config.new
12
13
  end
13
14
 
14
15
  def self.configure(&block)
15
- self.config.tap(&block)
16
+ config.tap(&block)
17
+ end
18
+
19
+ def self.adapter
20
+ @adapter || raise(NotInitializedError.new(caller))
16
21
  end
17
22
 
18
- def self.adapter; @adapter; end
23
+ def self.reset_adapter
24
+ @adapter = nil
25
+ end
19
26
 
20
27
  def self.init(establish_connection = true)
21
- require 'ardb/require_autoloaded_active_record_files'
28
+ require "ardb/require_autoloaded_active_record_files"
22
29
  begin
23
30
  require_db_file
24
- rescue InvalidDBFileError => exception
25
- raise exception.tap{ |e| e.set_backtrace(caller) }
31
+ rescue InvalidDBFileError => ex
32
+ ex.set_backtrace(caller)
33
+ raise ex
26
34
  end
27
35
 
28
- self.config.validate!
29
- @adapter = Adapter.new(self.config)
36
+ config.validate!
37
+ @adapter = Adapter.new(config)
30
38
 
31
39
  # setup AR
32
- ActiveRecord::Base.logger = self.config.logger
33
- 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
34
43
  end
35
44
 
36
45
  def self.escape_like_pattern(pattern, escape_char = nil)
37
- self.adapter.escape_like_pattern(pattern, escape_char)
46
+ adapter.escape_like_pattern(pattern, escape_char)
47
+ rescue NotInitializedError => ex
48
+ ex.set_backtrace(caller)
49
+ raise ex
38
50
  end
39
51
 
40
- private
41
-
42
52
  # try requiring the db file via the load path or as an absolute path, if
43
53
  # that fails it tries requiring relative to the current working directory
44
54
  def self.require_db_file
45
55
  begin
46
- 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
47
61
  rescue LoadError
48
- 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
+ )
49
67
  end
50
- rescue LoadError
51
- raise InvalidDBFileError, "can't require `#{ENV['ARDB_DB_FILE']}`, " \
52
- "check that the ARDB_DB_FILE env var is set to " \
53
- "the file path of your db file"
54
68
  end
55
69
 
56
70
  class Config
57
-
58
- ACTIVERECORD_ATTRS = [
59
- :adapter,
60
- :database,
61
- :encoding,
62
- :host,
63
- :port,
64
- :username,
65
- :password,
66
- :pool,
67
- :checkout_timeout,
68
- :min_messages
69
- ].freeze
70
- DEFAULT_MIGRATIONS_PATH = 'db/migrations'.freeze
71
- DEFAULT_SCHEMA_PATH = 'db/schema'.freeze
72
- RUBY_SCHEMA_FORMAT = :ruby.freeze
73
- 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
74
89
  VALID_SCHEMA_FORMATS = [RUBY_SCHEMA_FORMAT, SQL_SCHEMA_FORMAT].freeze
75
90
 
76
- attr_accessor *ACTIVERECORD_ATTRS
77
- attr_accessor :logger, :root_path
91
+ attr_accessor(*ACTIVERECORD_ATTRS)
92
+ attr_accessor :default_timezone, :logger, :root_path
78
93
  attr_reader :schema_format
79
94
  attr_writer :migrations_path, :schema_path
80
95
 
81
96
  def initialize
82
- @logger = Logger.new(STDOUT)
83
- @root_path = ENV['PWD']
84
- @migrations_path = DEFAULT_MIGRATIONS_PATH
85
- @schema_path = DEFAULT_SCHEMA_PATH
86
- @schema_format = RUBY_SCHEMA_FORMAT
97
+ @default_timezone = :utc
98
+ @logger = Logger.new(STDOUT)
99
+ @root_path = ENV["PWD"]
100
+ @migrations_path = DEFAULT_MIGRATIONS_PATH
101
+ @schema_path = DEFAULT_SCHEMA_PATH
102
+ @schema_format = RUBY_SCHEMA_FORMAT
87
103
  end
88
104
 
89
105
  def migrations_path
@@ -95,88 +111,102 @@ module Ardb
95
111
  end
96
112
 
97
113
  def schema_format=(new_value)
98
- @schema_format = begin
99
- new_value.to_sym
100
- rescue NoMethodError
101
- raise ArgumentError, "schema format must be a `Symbol`", caller
102
- 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
103
120
  end
104
121
 
105
122
  def activerecord_connect_hash
106
123
  ACTIVERECORD_ATTRS.inject({}) do |h, attr_name|
107
- value = self.send(attr_name)
124
+ value = send(attr_name)
108
125
  !value.nil? ? h.merge!(attr_name.to_s => value) : h
109
126
  end
110
127
  end
111
128
 
112
129
  def validate!
113
- if self.adapter.to_s.empty? || self.database.to_s.empty?
130
+ if adapter.to_s.empty? || database.to_s.empty?
114
131
  raise ConfigurationError, "an adapter and database must be provided"
115
- elsif !VALID_SCHEMA_FORMATS.include?(self.schema_format)
132
+ end
133
+
134
+ unless VALID_SCHEMA_FORMATS.include?(schema_format)
116
135
  raise ConfigurationError, "schema format must be one of: " \
117
- "#{VALID_SCHEMA_FORMATS.join(', ')}"
136
+ "#{VALID_SCHEMA_FORMATS.join(", ")}"
118
137
  end
138
+
119
139
  true
120
140
  end
121
141
 
122
142
  def ==(other)
123
- if other.kind_of?(self.class)
124
- self.activerecord_connect_hash == other.activerecord_connect_hash &&
125
- self.logger == other.logger &&
126
- self.root_path == other.root_path &&
127
- self.schema_format == other.schema_format &&
128
- self.migrations_path == other.migrations_path &&
129
- 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
130
151
  else
131
152
  super
132
153
  end
133
154
  end
134
-
135
155
  end
136
156
 
137
157
  module Adapter
138
-
139
158
  VALID_ADAPTERS = [
140
- 'sqlite',
141
- 'sqlite3',
142
- 'postgresql',
143
- 'postgres',
144
- 'mysql',
145
- 'mysql2'
159
+ "sqlite",
160
+ "sqlite3",
161
+ "postgresql",
162
+ "postgres",
163
+ "mysql",
164
+ "mysql2",
146
165
  ].freeze
147
166
 
148
167
  def self.new(config)
149
- if !VALID_ADAPTERS.include?(config.adapter)
168
+ unless VALID_ADAPTERS.include?(config.adapter)
150
169
  raise InvalidAdapterError, "invalid adapter: `#{config.adapter}`"
151
170
  end
152
- self.send(config.adapter, config)
171
+ send(config.adapter, config)
153
172
  end
154
173
 
155
174
  def self.sqlite(config)
156
- require 'ardb/adapter/sqlite'
175
+ require "ardb/adapter/sqlite"
157
176
  Adapter::Sqlite.new(config)
158
177
  end
159
178
 
160
- def self.sqlite3(config); self.sqlite(config); end
179
+ def self.sqlite3(config)
180
+ sqlite(config)
181
+ end
161
182
 
162
183
  def self.postgresql(config)
163
- require 'ardb/adapter/postgresql'
184
+ require "ardb/adapter/postgresql"
164
185
  Adapter::Postgresql.new(config)
165
186
  end
166
187
 
167
- def self.postgres(config); self.postgresql(config); end
188
+ def self.postgres(config)
189
+ postgresql(config)
190
+ end
168
191
 
169
192
  def self.mysql(config)
170
- require 'ardb/adapter/mysql'
193
+ require "ardb/adapter/mysql"
171
194
  Adapter::Mysql.new(config)
172
195
  end
173
196
 
174
- def self.mysql2(config); self.mysql(config); end
175
-
197
+ def self.mysql2(config)
198
+ mysql(config)
199
+ end
176
200
  end
177
201
 
178
202
  InvalidDBFileError = Class.new(ArgumentError)
179
203
  ConfigurationError = Class.new(ArgumentError)
180
204
  InvalidAdapterError = Class.new(RuntimeError)
181
205
 
206
+ class NotInitializedError < RuntimeError
207
+ def initialize(backtrace)
208
+ super("ardb hasn't been initialized yet, run `Ardb.init`")
209
+ set_backtrace(backtrace)
210
+ end
211
+ end
182
212
  end