ardb 0.28.3 → 0.29.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -7
  2. data/Gemfile +4 -9
  3. data/README.md +252 -3
  4. data/ardb.gemspec +8 -7
  5. data/bin/ardb +1 -1
  6. data/lib/ardb.rb +35 -34
  7. data/lib/ardb/adapter/base.rb +34 -30
  8. data/lib/ardb/adapter/mysql.rb +2 -17
  9. data/lib/ardb/adapter/postgresql.rb +24 -30
  10. data/lib/ardb/adapter/sqlite.rb +4 -8
  11. data/lib/ardb/adapter_spy.rb +2 -16
  12. data/lib/ardb/cli.rb +15 -15
  13. data/lib/ardb/cli/clirb.rb +14 -17
  14. data/lib/ardb/cli/commands.rb +204 -114
  15. data/lib/ardb/db_tests.rb +2 -4
  16. data/lib/ardb/default_order_by.rb +3 -13
  17. data/lib/ardb/migration.rb +9 -13
  18. data/lib/ardb/record_spy.rb +7 -26
  19. data/lib/ardb/relation_spy.rb +0 -6
  20. data/lib/ardb/require_autoloaded_active_record_files.rb +103 -58
  21. data/lib/ardb/test_helpers.rb +2 -5
  22. data/lib/ardb/use_db_default.rb +4 -15
  23. data/lib/ardb/version.rb +1 -1
  24. data/script/determine_autoloaded_active_record_files.rb +11 -8
  25. data/test/helper.rb +4 -13
  26. data/test/support/factory.rb +2 -2
  27. data/test/support/postgresql/migrations/{.gitkeep → .keep} +0 -0
  28. data/test/support/postgresql/schema.rb +0 -1
  29. data/test/support/postgresql/setup_test_db.rb +14 -15
  30. data/test/system/.keep +0 -0
  31. data/test/unit/adapter/base_tests.rb +73 -53
  32. data/test/unit/adapter/mysql_tests.rb +2 -19
  33. data/test/unit/adapter/postgresql_tests.rb +14 -23
  34. data/test/unit/adapter/sqlite_tests.rb +3 -11
  35. data/test/unit/adapter_spy_tests.rb +2 -16
  36. data/test/unit/ardb_tests.rb +43 -43
  37. data/test/unit/cli_tests.rb +220 -158
  38. data/test/unit/db_tests_tests.rb +3 -6
  39. data/test/unit/default_order_by_tests.rb +4 -8
  40. data/test/unit/migration_tests.rb +11 -15
  41. data/test/unit/record_spy_tests.rb +17 -22
  42. data/test/unit/relation_spy_tests.rb +17 -46
  43. data/test/unit/test_helpers_tests.rb +3 -14
  44. data/test/unit/use_db_default_tests.rb +7 -11
  45. metadata +100 -84
  46. data/lib/ardb/has_slug.rb +0 -107
  47. data/lib/ardb/migration_helpers.rb +0 -77
  48. data/lib/ardb/pg_json.rb +0 -90
  49. data/test/support/postgresql/pg_json_migrations/20160519133432_create_pg_json_migrate_test.rb +0 -13
  50. data/test/system/pg_json_tests.rb +0 -85
  51. data/test/unit/has_slug_tests.rb +0 -341
  52. data/test/unit/migration_helpers_tests.rb +0 -65
  53. data/test/unit/pg_json_tests.rb +0 -39
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
- ---
2
- SHA1:
3
- data.tar.gz: 352c1dc50e3ac2567d5f74cc148ba572d4fa8f3d
4
- metadata.gz: 0a5e57adec591aaa7273bade002b3f873caef819
5
- SHA512:
6
- data.tar.gz: c16e759ea474b84490ebb450825b44e11725960d847ff82cfec5e5840dd00b853a4278cc92f49989da04f6f04196b61aa428f4ba7f151f7562cd3d562ead9015
7
- metadata.gz: f61b9a4df10d7d2b5b4520d8f601c1e3f9d3e1155a306d9b477cecf63d9021546bf9123adcdfddd9747f1348a905a388d564ef4babbde667c97fd732b355e213
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 81b830715964b5aace51929da208df3bf357a35c42e5c38aea50b4d98d625d96
4
+ data.tar.gz: f4014e705ae352722d6a16f08061767d31c5851b7c3e0cdc5c6592abcd74d034
5
+ SHA512:
6
+ metadata.gz: '08a7091251f11c2969aae47148185eae17efe0996b7dd0d79f63ac9648ba13c7d60694fe6b3f46ef43e77924e928f209d0f13cdbdbbd249578e143fc3f6d18a9'
7
+ data.tar.gz: 77f04eda98916783b4c1fb076a7bc461fd71f114547d62030f2379be7c5687219e2c5b3e3e925d42ead55d5e8b5e1a6651c0841710665d3313888b3c96935adc
data/Gemfile CHANGED
@@ -1,13 +1,8 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gemspec
4
-
5
- gem 'pg', "~> 0.17.1"
6
- gem 'pry', "~> 0.9.0"
3
+ ruby "~> 2.4"
7
4
 
8
- # Lock down gem versions because they require a newer version of ruby
9
- gem 'i18n', "< 0.7"
5
+ gemspec
10
6
 
11
- platform :ruby_18 do
12
- gem 'json', '~> 1.8'
13
- end
7
+ gem "pg", "~> 1.1.4"
8
+ gem "pry", "~> 0.12.2"
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,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require "ardb/version"
5
5
 
@@ -11,18 +11,19 @@ Gem::Specification.new do |gem|
11
11
  gem.summary = %q{Activerecord database tools.}
12
12
  gem.description = %q{Activerecord database tools.}
13
13
  gem.homepage = "http://github.com/redding/ardb"
14
- gem.license = 'MIT'
14
+ gem.license = "MIT"
15
15
 
16
16
  gem.files = `git ls-files`.split($/)
17
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_development_dependency("assert", ["~> 2.16.1"])
21
+ gem.required_ruby_version = "~> 2.4"
22
22
 
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"])
23
+ gem.add_development_dependency("assert", ["~> 2.18.1"])
27
24
 
25
+ gem.add_dependency("activerecord", ["> 5.0", "< 7.0"])
26
+ 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"])
28
29
  end
data/bin/ardb CHANGED
@@ -3,5 +3,5 @@
3
3
  # Copyright (c) 2013-Present Kelly Redding and Collin Redding
4
4
  #
5
5
 
6
- require 'ardb/cli'
6
+ require "ardb/cli"
7
7
  Ardb::CLI.run ARGV
@@ -1,12 +1,11 @@
1
- require 'active_record'
2
- require 'logger'
1
+ require "active_record"
2
+ require "logger"
3
3
 
4
- require 'ardb/version'
4
+ require "ardb/version"
5
5
 
6
- ENV['ARDB_DB_FILE'] ||= 'config/db'
6
+ ENV["ARDB_DB_FILE"] ||= "config/db"
7
7
 
8
8
  module Ardb
9
-
10
9
  def self.config
11
10
  @config ||= Config.new
12
11
  end
@@ -22,7 +21,7 @@ module Ardb
22
21
  def self.reset_adapter; @adapter = nil; end
23
22
 
24
23
  def self.init(establish_connection = true)
25
- require 'ardb/require_autoloaded_active_record_files'
24
+ require "ardb/require_autoloaded_active_record_files"
26
25
  begin
27
26
  require_db_file
28
27
  rescue InvalidDBFileError => exception
@@ -34,6 +33,7 @@ module Ardb
34
33
  @adapter = Adapter.new(self.config)
35
34
 
36
35
  # setup AR
36
+ ActiveRecord::Base.default_timezone = self.config.default_timezone
37
37
  ActiveRecord::Base.logger = self.config.logger
38
38
  self.adapter.connect_db if establish_connection
39
39
  end
@@ -51,18 +51,17 @@ module Ardb
51
51
  # that fails it tries requiring relative to the current working directory
52
52
  def self.require_db_file
53
53
  begin
54
- require ENV['ARDB_DB_FILE']
54
+ require ENV["ARDB_DB_FILE"]
55
55
  rescue LoadError
56
- require File.expand_path(ENV['ARDB_DB_FILE'], ENV['PWD'])
56
+ require File.expand_path(ENV["ARDB_DB_FILE"], ENV["PWD"])
57
57
  end
58
58
  rescue LoadError
59
- raise InvalidDBFileError, "can't require `#{ENV['ARDB_DB_FILE']}`, " \
59
+ raise InvalidDBFileError, "can't require `#{ENV["ARDB_DB_FILE"]}`, " \
60
60
  "check that the ARDB_DB_FILE env var is set to " \
61
61
  "the file path of your db file"
62
62
  end
63
63
 
64
64
  class Config
65
-
66
65
  ACTIVERECORD_ATTRS = [
67
66
  :adapter,
68
67
  :database,
@@ -75,23 +74,25 @@ module Ardb
75
74
  :checkout_timeout,
76
75
  :min_messages
77
76
  ].freeze
78
- DEFAULT_MIGRATIONS_PATH = 'db/migrations'.freeze
79
- DEFAULT_SCHEMA_PATH = 'db/schema'.freeze
77
+
78
+ DEFAULT_MIGRATIONS_PATH = "db/migrations".freeze
79
+ DEFAULT_SCHEMA_PATH = "db/schema".freeze
80
80
  RUBY_SCHEMA_FORMAT = :ruby.freeze
81
81
  SQL_SCHEMA_FORMAT = :sql.freeze
82
82
  VALID_SCHEMA_FORMATS = [RUBY_SCHEMA_FORMAT, SQL_SCHEMA_FORMAT].freeze
83
83
 
84
- attr_accessor *ACTIVERECORD_ATTRS
85
- attr_accessor :logger, :root_path
84
+ attr_accessor(*ACTIVERECORD_ATTRS)
85
+ attr_accessor :default_timezone, :logger, :root_path
86
86
  attr_reader :schema_format
87
87
  attr_writer :migrations_path, :schema_path
88
88
 
89
89
  def initialize
90
- @logger = Logger.new(STDOUT)
91
- @root_path = ENV['PWD']
92
- @migrations_path = DEFAULT_MIGRATIONS_PATH
93
- @schema_path = DEFAULT_SCHEMA_PATH
94
- @schema_format = RUBY_SCHEMA_FORMAT
90
+ @default_timezone = :utc
91
+ @logger = Logger.new(STDOUT)
92
+ @root_path = ENV["PWD"]
93
+ @migrations_path = DEFAULT_MIGRATIONS_PATH
94
+ @schema_path = DEFAULT_SCHEMA_PATH
95
+ @schema_format = RUBY_SCHEMA_FORMAT
95
96
  end
96
97
 
97
98
  def migrations_path
@@ -120,16 +121,20 @@ module Ardb
120
121
  def validate!
121
122
  if self.adapter.to_s.empty? || self.database.to_s.empty?
122
123
  raise ConfigurationError, "an adapter and database must be provided"
123
- elsif !VALID_SCHEMA_FORMATS.include?(self.schema_format)
124
+ end
125
+
126
+ if !VALID_SCHEMA_FORMATS.include?(self.schema_format)
124
127
  raise ConfigurationError, "schema format must be one of: " \
125
- "#{VALID_SCHEMA_FORMATS.join(', ')}"
128
+ "#{VALID_SCHEMA_FORMATS.join(", ")}"
126
129
  end
130
+
127
131
  true
128
132
  end
129
133
 
130
134
  def ==(other)
131
135
  if other.kind_of?(self.class)
132
136
  self.activerecord_connect_hash == other.activerecord_connect_hash &&
137
+ self.default_timezone == other.default_timezone &&
133
138
  self.logger == other.logger &&
134
139
  self.root_path == other.root_path &&
135
140
  self.schema_format == other.schema_format &&
@@ -139,18 +144,16 @@ module Ardb
139
144
  super
140
145
  end
141
146
  end
142
-
143
147
  end
144
148
 
145
149
  module Adapter
146
-
147
150
  VALID_ADAPTERS = [
148
- 'sqlite',
149
- 'sqlite3',
150
- 'postgresql',
151
- 'postgres',
152
- 'mysql',
153
- 'mysql2'
151
+ "sqlite",
152
+ "sqlite3",
153
+ "postgresql",
154
+ "postgres",
155
+ "mysql",
156
+ "mysql2"
154
157
  ].freeze
155
158
 
156
159
  def self.new(config)
@@ -161,26 +164,25 @@ module Ardb
161
164
  end
162
165
 
163
166
  def self.sqlite(config)
164
- require 'ardb/adapter/sqlite'
167
+ require "ardb/adapter/sqlite"
165
168
  Adapter::Sqlite.new(config)
166
169
  end
167
170
 
168
171
  def self.sqlite3(config); self.sqlite(config); end
169
172
 
170
173
  def self.postgresql(config)
171
- require 'ardb/adapter/postgresql'
174
+ require "ardb/adapter/postgresql"
172
175
  Adapter::Postgresql.new(config)
173
176
  end
174
177
 
175
178
  def self.postgres(config); self.postgresql(config); end
176
179
 
177
180
  def self.mysql(config)
178
- require 'ardb/adapter/mysql'
181
+ require "ardb/adapter/mysql"
179
182
  Adapter::Mysql.new(config)
180
183
  end
181
184
 
182
185
  def self.mysql2(config); self.mysql(config); end
183
-
184
186
  end
185
187
 
186
188
  InvalidDBFileError = Class.new(ArgumentError)
@@ -193,5 +195,4 @@ module Ardb
193
195
  set_backtrace(backtrace)
194
196
  end
195
197
  end
196
-
197
198
  end