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
@@ -1,27 +1,40 @@
1
- require 'ardb'
1
+ # frozen_string_literal: true
2
+
3
+ require "ardb"
2
4
 
3
5
  module Ardb; end
4
- module Ardb::Adapter
5
6
 
7
+ module Ardb::Adapter
6
8
  class Base
7
-
8
9
  attr_reader :config
9
10
 
10
11
  def initialize(config)
11
12
  @config = config
13
+ validate!
14
+ end
15
+
16
+ def connect_hash
17
+ config.activerecord_connect_hash
18
+ end
19
+
20
+ def database
21
+ config.database
12
22
  end
13
23
 
14
- def connect_hash; self.config.activerecord_connect_hash; end
15
- def database; self.config.database; end
16
- def migrations_path; self.config.migrations_path; end
17
- def schema_format; self.config.schema_format; end
24
+ def migrations_path
25
+ config.migrations_path
26
+ end
27
+
28
+ def schema_format
29
+ config.schema_format
30
+ end
18
31
 
19
32
  def ruby_schema_path
20
- @ruby_schema_path ||= "#{self.config.schema_path}.rb"
33
+ @ruby_schema_path ||= "#{config.schema_path}.rb"
21
34
  end
22
35
 
23
36
  def sql_schema_path
24
- @sql_schema_path ||= "#{self.config.schema_path}.sql"
37
+ @sql_schema_path ||= "#{config.schema_path}.sql"
25
38
  end
26
39
 
27
40
  def escape_like_pattern(pattern, escape_char = nil)
@@ -30,52 +43,58 @@ module Ardb::Adapter
30
43
  pattern.gsub!(escape_char){ escape_char * 2 }
31
44
  # don't allow custom wildcards
32
45
  pattern.gsub!(/%|_/){ |wildcard_char| "#{escape_char}#{wildcard_char}" }
46
+ pattern
33
47
  end
34
48
 
35
- def foreign_key_add_sql(*args); raise NotImplementedError; end
36
- def foreign_key_drop_sql(*args); raise NotImplementedError; end
49
+ def create_db(*args)
50
+ raise NotImplementedError
51
+ end
37
52
 
38
- def create_db(*args); raise NotImplementedError; end
39
- def drop_db(*args); raise NotImplementedError; end
53
+ def drop_db(*args)
54
+ raise NotImplementedError
55
+ end
40
56
 
41
- def drop_tables(*args); raise NotImplementedError; end
57
+ def drop_tables(*args)
58
+ raise NotImplementedError
59
+ end
42
60
 
43
61
  def connect_db
44
- ActiveRecord::Base.establish_connection(self.connect_hash)
45
- # checkout a connection to ensure we can connect to the DB, we don't do
62
+ ActiveRecord::Base.establish_connection(connect_hash)
63
+ # checkout a connection to ensure we can connect to the DB, we don"t do
46
64
  # anything with the connection and immediately check it back in
47
- ActiveRecord::Base.connection_pool.with_connection{ }
65
+ ActiveRecord::Base.connection_pool.with_connection{}
48
66
  end
49
67
 
50
68
  def migrate_db
51
- verbose = ENV["MIGRATE_QUIET"].nil?
52
- version = ENV["MIGRATE_VERSION"] ? ENV["MIGRATE_VERSION"].to_i : nil
53
-
54
- if defined?(ActiveRecord::Migration::CommandRecorder)
55
- require 'ardb/migration_helpers'
56
- ActiveRecord::Migration::CommandRecorder.class_eval do
57
- include Ardb::MigrationHelpers::RecorderMixin
58
- end
59
- end
69
+ migrate_db_up
70
+ end
60
71
 
61
- ActiveRecord::Migrator.migrations_path = self.migrations_path
62
- ActiveRecord::Migration.verbose = verbose
63
- ActiveRecord::Migrator.migrate(self.migrations_path, version) do |migration|
64
- ENV["MIGRATE_SCOPE"].blank? || (ENV["MIGRATE_SCOPE"] == migration.scope)
65
- end
72
+ def migrate_db_up(target_version = nil)
73
+ migration_context.up(target_version)
74
+ end
75
+
76
+ def migrate_db_down(target_version = nil)
77
+ migration_context.down(target_version)
78
+ end
79
+
80
+ def migrate_db_forward(steps = 1)
81
+ migration_context.forward(steps)
82
+ end
83
+
84
+ def migrate_db_backward(steps = 1)
85
+ migration_context.rollback(steps)
66
86
  end
67
87
 
68
88
  def load_schema
69
- # silence STDOUT
70
- current_stdout = $stdout.dup
71
- $stdout = File.new('/dev/null', 'w')
72
- load_ruby_schema if self.schema_format == Ardb::Config::RUBY_SCHEMA_FORMAT
73
- load_sql_schema if self.schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
89
+ current_stdout = $stdout.dup # silence STDOUT
90
+ $stdout = File.new("/dev/null", "w")
91
+ load_ruby_schema if schema_format == Ardb::Config::RUBY_SCHEMA_FORMAT
92
+ load_sql_schema if schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
74
93
  $stdout = current_stdout
75
94
  end
76
95
 
77
96
  def load_ruby_schema
78
- load self.ruby_schema_path
97
+ load ruby_schema_path
79
98
  end
80
99
 
81
100
  def load_sql_schema
@@ -83,18 +102,17 @@ module Ardb::Adapter
83
102
  end
84
103
 
85
104
  def dump_schema
86
- # silence STDOUT
87
- current_stdout = $stdout.dup
88
- $stdout = File.new('/dev/null', 'w')
105
+ current_stdout = $stdout.dup # silence STDOUT
106
+ $stdout = File.new("/dev/null", "w")
89
107
  dump_ruby_schema
90
- dump_sql_schema if self.schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
108
+ dump_sql_schema if schema_format == Ardb::Config::SQL_SCHEMA_FORMAT
91
109
  $stdout = current_stdout
92
110
  end
93
111
 
94
112
  def dump_ruby_schema
95
- require 'active_record/schema_dumper'
96
- FileUtils.mkdir_p File.dirname(self.ruby_schema_path)
97
- File.open(self.ruby_schema_path, 'w:utf-8') do |file|
113
+ require "active_record/schema_dumper"
114
+ FileUtils.mkdir_p File.dirname(ruby_schema_path)
115
+ File.open(ruby_schema_path, "w:utf-8") do |file|
98
116
  ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
99
117
  end
100
118
  end
@@ -104,13 +122,21 @@ module Ardb::Adapter
104
122
  end
105
123
 
106
124
  def ==(other)
107
- if other.kind_of?(self.class)
108
- self.config == other.config
125
+ if other.is_a?(self.class)
126
+ config == other.config
109
127
  else
110
128
  super
111
129
  end
112
130
  end
113
131
 
114
- end
132
+ private
115
133
 
134
+ def validate!
135
+ # override as needed
136
+ end
137
+
138
+ def migration_context
139
+ ActiveRecord::MigrationContext.new(migrations_path)
140
+ end
141
+ end
116
142
  end
@@ -1,21 +1,8 @@
1
- require 'ardb/adapter/base'
1
+ # frozen_string_literal: true
2
2
 
3
- module Ardb::Adapter
4
-
5
- class Mysql < Base
6
-
7
- def foreign_key_add_sql
8
- "ALTER TABLE :from_table"\
9
- " ADD CONSTRAINT :name"\
10
- " FOREIGN KEY (:from_column)"\
11
- " REFERENCES :to_table (:to_column)"
12
- end
13
-
14
- def foreign_key_drop_sql
15
- "ALTER TABLE :from_table"\
16
- " DROP FOREIGN KEY :name"
17
- end
3
+ require "ardb/adapter/base"
18
4
 
5
+ module Ardb::Adapter
6
+ class Mysql < Ardb::Adapter::Base
19
7
  end
20
-
21
8
  end
@@ -1,39 +1,45 @@
1
- require 'ardb/adapter/base'
1
+ # frozen_string_literal: true
2
2
 
3
- module Ardb::Adapter
4
-
5
- class Postgresql < Base
3
+ require "ardb/adapter/base"
6
4
 
7
- # the 'postgres' db is a "public" (doesn't typically require auth/grants to
5
+ module Ardb::Adapter
6
+ class Postgresql < Ardb::Adapter::Base
7
+ # the "postgres" db is a "public" (doesn"t typically require auth/grants to
8
8
  # connect to) db that typically exists for all postgres installations; the
9
9
  # adapter uses it to create/drop other databases
10
10
  def public_connect_hash
11
- @public_connect_hash ||= self.connect_hash.merge({
12
- 'database' => 'postgres',
13
- 'schema_search_path' => 'public'
11
+ @public_connect_hash ||= connect_hash.merge({
12
+ "database" => "postgres",
13
+ "schema_search_path" => "public",
14
14
  })
15
15
  end
16
16
 
17
17
  def create_db
18
- ActiveRecord::Base.establish_connection(self.public_connect_hash)
19
- ActiveRecord::Base.connection.create_database(self.database, self.connect_hash)
20
- 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)
21
24
  end
22
25
 
23
26
  def drop_db
24
27
  begin
25
- ActiveRecord::Base.establish_connection(self.public_connect_hash)
28
+ ActiveRecord::Base.establish_connection(public_connect_hash)
26
29
  ActiveRecord::Base.connection.tap do |conn|
27
- conn.execute "UPDATE pg_catalog.pg_database"\
28
- " SET datallowconn=false WHERE datname='#{self.database}'"
29
- # 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
30
36
  # http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL-TABLE
31
37
  conn.execute "SELECT pg_terminate_backend(pid)"\
32
- " FROM pg_stat_activity WHERE datname='#{self.database}'"
33
- conn.execute "DROP DATABASE IF EXISTS #{self.database}"
38
+ " FROM pg_stat_activity WHERE datname='#{database}'"
39
+ conn.execute "DROP DATABASE IF EXISTS #{database}"
34
40
  end
35
- rescue PG::Error => e
36
- raise e unless e.message =~ /does not exist/
41
+ rescue PG::Error => ex
42
+ raise ex unless ex.message =~ /does not exist/
37
43
  end
38
44
  end
39
45
 
@@ -42,47 +48,46 @@ module Ardb::Adapter
42
48
  tables = conn.execute "SELECT table_name"\
43
49
  " FROM information_schema.tables"\
44
50
  " WHERE table_schema = 'public';"
45
- 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
46
54
  end
47
55
  end
48
56
 
49
- def foreign_key_add_sql
50
- "ALTER TABLE :from_table"\
51
- " ADD CONSTRAINT :name"\
52
- " FOREIGN KEY (:from_column)"\
53
- " REFERENCES :to_table (:to_column)"
54
- end
55
-
56
- def foreign_key_drop_sql
57
- "ALTER TABLE :from_table"\
58
- " DROP CONSTRAINT :name"
59
- end
60
-
61
57
  def load_sql_schema
62
- require 'scmd'
63
- cmd_str = "psql -f \"#{self.sql_schema_path}\" #{self.database}"
64
- cmd = Scmd.new(cmd_str, :env => env_var_hash).tap(&:run)
65
- raise 'Error loading database' unless cmd.success?
58
+ require "scmd"
59
+ cmd_str = "psql -f \"#{sql_schema_path}\" #{database}"
60
+ cmd = Scmd.new(cmd_str, env: env_var_hash).tap(&:run)
61
+ raise "Error loading database" unless cmd.success?
66
62
  end
67
63
 
68
64
  def dump_sql_schema
69
- require 'scmd'
70
- cmd_str = "pg_dump -i -s -x -O -f \"#{self.sql_schema_path}\" #{self.database}"
71
- cmd = Scmd.new(cmd_str, :env => env_var_hash).tap(&:run)
72
- raise 'Error dumping database' unless cmd.success?
65
+ require "scmd"
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)
69
+ raise "Error dumping database" unless cmd.success?
73
70
  end
74
71
 
75
72
  private
76
73
 
74
+ def validate!
75
+ if database =~ /\W/
76
+ raise(
77
+ Ardb::ConfigurationError,
78
+ "database value must not contain non-word characters. "\
79
+ "Given: #{database.inspect}.",
80
+ )
81
+ end
82
+ end
83
+
77
84
  def env_var_hash
78
85
  @env_var_hash ||= {
79
- 'PGHOST' => self.connect_hash['host'],
80
- 'PGPORT' => self.connect_hash['port'],
81
- 'PGUSER' => self.connect_hash['username'],
82
- '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"],
83
90
  }
84
91
  end
85
-
86
92
  end
87
-
88
93
  end
@@ -1,31 +1,27 @@
1
- require 'fileutils'
2
- require 'ardb'
3
- require 'ardb/adapter/base'
1
+ # frozen_string_literal: true
4
2
 
5
- module Ardb::Adapter
6
-
7
- class Sqlite < Base
3
+ require "fileutils"
4
+ require "ardb"
5
+ require "ardb/adapter/base"
8
6
 
7
+ module Ardb::Adapter
8
+ class Sqlite < Ardb::Adapter::Base
9
9
  def db_file_path
10
- File.expand_path(self.database, self.config.root_path)
10
+ File.expand_path(database, config.root_path)
11
11
  end
12
12
 
13
13
  def validate!
14
- if File.exist?(self.db_file_path)
15
- raise RuntimeError, "`#{self.database}` already exists"
16
- end
14
+ raise "`#{database}` already exists" if File.exist?(db_file_path)
17
15
  end
18
16
 
19
17
  def create_db
20
18
  validate!
21
- FileUtils.mkdir_p File.dirname(self.db_file_path)
22
- ActiveRecord::Base.establish_connection(self.connect_hash)
19
+ FileUtils.mkdir_p File.dirname(db_file_path)
20
+ ActiveRecord::Base.establish_connection(connect_hash)
23
21
  end
24
22
 
25
23
  def drop_db
26
- FileUtils.rm(self.db_file_path) if File.exist?(self.db_file_path)
24
+ FileUtils.rm(db_file_path) if File.exist?(db_file_path)
27
25
  end
28
-
29
26
  end
30
-
31
27
  end
@@ -1,10 +1,10 @@
1
- require 'ardb'
2
- require 'ardb/adapter/base'
1
+ # frozen_string_literal: true
3
2
 
4
- module Ardb
3
+ require "ardb"
4
+ require "ardb/adapter/base"
5
5
 
6
+ module Ardb
6
7
  class AdapterSpy < Ardb::Adapter::Base
7
-
8
8
  attr_accessor :drop_tables_called_count
9
9
  attr_accessor :dump_schema_called_count, :load_schema_called_count
10
10
  attr_accessor :drop_db_called_count, :create_db_called_count
@@ -22,73 +22,61 @@ module Ardb
22
22
  end
23
23
 
24
24
  def create_db_called?
25
- self.create_db_called_count > 0
25
+ create_db_called_count > 0
26
26
  end
27
27
 
28
28
  def drop_db_called?
29
- self.drop_db_called_count > 0
29
+ drop_db_called_count > 0
30
30
  end
31
31
 
32
32
  def drop_tables_called?
33
- self.drop_tables_called_count > 0
33
+ drop_tables_called_count > 0
34
34
  end
35
35
 
36
36
  def connect_db_called?
37
- self.connect_db_called_count > 0
37
+ connect_db_called_count > 0
38
38
  end
39
39
 
40
40
  def migrate_db_called?
41
- self.migrate_db_called_count > 0
41
+ migrate_db_called_count > 0
42
42
  end
43
43
 
44
44
  def load_schema_called?
45
- self.load_schema_called_count > 0
45
+ load_schema_called_count > 0
46
46
  end
47
47
 
48
48
  def dump_schema_called?
49
- self.dump_schema_called_count > 0
49
+ dump_schema_called_count > 0
50
50
  end
51
51
 
52
52
  # Overwritten `Adapter::Base` methods
53
53
 
54
- def foreign_key_add_sql
55
- "FAKE ADD FOREIGN KEY SQL :from_table :from_column " \
56
- ":to_table :to_column :name"
57
- end
58
-
59
- def foreign_key_drop_sql
60
- "FAKE DROP FOREIGN KEY SQL :from_table :from_column " \
61
- ":to_table :to_column :name"
62
- end
63
-
64
- def create_db(*args, &block)
54
+ def create_db(*_args)
65
55
  self.create_db_called_count += 1
66
56
  end
67
57
 
68
- def drop_db(*args, &block)
58
+ def drop_db(*_args)
69
59
  self.drop_db_called_count += 1
70
60
  end
71
61
 
72
- def drop_tables(*args, &block)
62
+ def drop_tables(*_args)
73
63
  self.drop_tables_called_count += 1
74
64
  end
75
65
 
76
- def connect_db(*args, &block)
66
+ def connect_db(*_args)
77
67
  self.connect_db_called_count += 1
78
68
  end
79
69
 
80
- def migrate_db(*args, &block)
70
+ def migrate_db(*_args)
81
71
  self.migrate_db_called_count += 1
82
72
  end
83
73
 
84
- def load_schema(*args, &block)
74
+ def load_schema(*_args)
85
75
  self.load_schema_called_count += 1
86
76
  end
87
77
 
88
- def dump_schema(*args, &block)
78
+ def dump_schema(*_args)
89
79
  self.dump_schema_called_count += 1
90
80
  end
91
-
92
81
  end
93
-
94
82
  end