ardb 0.26.0 → 0.27.0

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 (43) hide show
  1. data/ardb.gemspec +1 -0
  2. data/bin/ardb +1 -1
  3. data/lib/ardb.rb +1 -1
  4. data/lib/ardb/adapter/base.rb +73 -66
  5. data/lib/ardb/adapter/sqlite.rb +1 -1
  6. data/lib/ardb/adapter_spy.rb +18 -5
  7. data/lib/ardb/cli.rb +239 -76
  8. data/lib/ardb/clirb.rb +58 -0
  9. data/lib/ardb/default_order_by.rb +59 -0
  10. data/lib/ardb/has_slug.rb +7 -6
  11. data/lib/ardb/migration.rb +42 -0
  12. data/lib/ardb/migration_helpers.rb +56 -53
  13. data/lib/ardb/record_spy.rb +5 -5
  14. data/lib/ardb/relation_spy.rb +13 -2
  15. data/lib/ardb/root_path.rb +7 -4
  16. data/lib/ardb/test_helpers.rb +56 -42
  17. data/lib/ardb/use_db_default.rb +8 -7
  18. data/lib/ardb/version.rb +1 -1
  19. data/test/support/factory.rb +5 -0
  20. data/test/unit/adapter/base_tests.rb +22 -5
  21. data/test/unit/adapter/mysql_tests.rb +1 -1
  22. data/test/unit/adapter/sqlite_tests.rb +2 -2
  23. data/test/unit/adapter_spy_tests.rb +19 -6
  24. data/test/unit/cli_tests.rb +610 -0
  25. data/test/unit/default_order_by_tests.rb +122 -0
  26. data/test/unit/has_slug_tests.rb +5 -0
  27. data/test/unit/migration_tests.rb +88 -0
  28. data/test/unit/record_spy_tests.rb +6 -0
  29. data/test/unit/test_helpers_tests.rb +26 -3
  30. data/test/unit/use_db_default_tests.rb +5 -0
  31. metadata +31 -25
  32. data/lib/ardb/runner.rb +0 -50
  33. data/lib/ardb/runner/connect_command.rb +0 -20
  34. data/lib/ardb/runner/create_command.rb +0 -25
  35. data/lib/ardb/runner/drop_command.rb +0 -25
  36. data/lib/ardb/runner/generate_command.rb +0 -64
  37. data/lib/ardb/runner/migrate_command.rb +0 -28
  38. data/test/unit/runner/connect_command_tests.rb +0 -17
  39. data/test/unit/runner/create_command_tests.rb +0 -67
  40. data/test/unit/runner/drop_command_tests.rb +0 -68
  41. data/test/unit/runner/generate_command_tests.rb +0 -46
  42. data/test/unit/runner/migrate_command_tests.rb +0 -96
  43. data/test/unit/runner_tests.rb +0 -69
@@ -1,50 +0,0 @@
1
- require 'ardb'
2
-
3
- module Ardb; end
4
- class Ardb::Runner
5
- UnknownCmdError = Class.new(ArgumentError)
6
- CmdError = Class.new(RuntimeError)
7
- CmdFail = Class.new(RuntimeError)
8
-
9
- attr_reader :cmd_name, :cmd_args, :opts
10
-
11
- def initialize(args, opts)
12
- @opts = opts
13
- @cmd_name = args.shift || ""
14
- @cmd_args = args
15
- end
16
-
17
- def run
18
- $LOAD_PATH.push(Dir.pwd) unless $LOAD_PATH.include?(Dir.pwd)
19
- Ardb.init(false) # don't establish a connection
20
-
21
- case @cmd_name
22
- when 'migrate'
23
- require 'ardb/runner/migrate_command'
24
- MigrateCommand.new.run
25
- when 'generate'
26
- require 'ardb/runner/generate_command'
27
- GenerateCommand.new(@cmd_args).run
28
- when 'create'
29
- require 'ardb/runner/create_command'
30
- CreateCommand.new.run
31
- when 'drop'
32
- require 'ardb/runner/drop_command'
33
- DropCommand.new.run
34
- when 'connect'
35
- require 'ardb/runner/connect_command'
36
- ConnectCommand.new.run
37
- when 'null'
38
- NullCommand.new.run
39
- else
40
- raise UnknownCmdError, "unknown command `#{@cmd_name}`"
41
- end
42
- end
43
-
44
- class NullCommand
45
- def run
46
- # if this was a real command it would do something here
47
- end
48
- end
49
-
50
- end
@@ -1,20 +0,0 @@
1
- require 'ardb/runner'
2
-
3
- class Ardb::Runner::ConnectCommand
4
-
5
- def run
6
- begin
7
- Ardb.init
8
- ActiveRecord::Base.connection
9
- $stdout.puts "connected to #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`"
10
- rescue Ardb::Runner::CmdError => e
11
- raise e
12
- rescue Exception => e
13
- $stderr.puts e, *e.backtrace
14
- $stderr.puts "error connecting to #{Ardb.config.db.database.inspect} database"\
15
- " with #{Ardb.config.db_settings.inspect}"
16
- raise Ardb::Runner::CmdFail
17
- end
18
- end
19
-
20
- end
@@ -1,25 +0,0 @@
1
- require 'active_record'
2
- require 'ardb/runner'
3
-
4
- class Ardb::Runner::CreateCommand
5
-
6
- def initialize(out_io = nil, err_io = nil)
7
- @out_io = out_io || $stdout
8
- @err_io = err_io || $stderr
9
- @adapter = Ardb::Adapter.send(Ardb.config.db.adapter)
10
- end
11
-
12
- def run
13
- begin
14
- @adapter.create_db
15
- @out_io.puts "created #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`"
16
- rescue Ardb::Runner::CmdError => e
17
- raise e
18
- rescue StandardError => e
19
- @err_io.puts e
20
- @err_io.puts "error creating #{Ardb.config.db.database.inspect} database"
21
- raise Ardb::Runner::CmdFail
22
- end
23
- end
24
-
25
- end
@@ -1,25 +0,0 @@
1
- require 'active_record'
2
- require 'ardb/runner'
3
-
4
- class Ardb::Runner::DropCommand
5
-
6
- def initialize(out_io = nil, err_io = nil)
7
- @out_io = out_io || $stdout
8
- @err_io = err_io || $stderr
9
- @adapter = Ardb::Adapter.send(Ardb.config.db.adapter)
10
- end
11
-
12
- def run
13
- begin
14
- @adapter.drop_db
15
- @out_io.puts "dropped #{Ardb.config.db.adapter} db `#{Ardb.config.db.database}`"
16
- rescue Ardb::Runner::CmdError => e
17
- raise e
18
- rescue StandardError => e
19
- @err_io.puts e
20
- @err_io.puts "error dropping #{Ardb.config.db.database.inspect} database"
21
- raise Ardb::Runner::CmdFail
22
- end
23
- end
24
-
25
- end
@@ -1,64 +0,0 @@
1
- require 'fileutils'
2
- require 'active_support/core_ext/string'
3
- require 'ardb/runner'
4
-
5
- class Ardb::Runner::GenerateCommand
6
-
7
- def initialize(args)
8
- @item = args.shift
9
- @args = args
10
- end
11
-
12
- def run
13
- if @item.nil?
14
- raise Ardb::Runner::CmdError, "specify an item to generate"
15
- end
16
- if !self.respond_to?("#{@item}_cmd")
17
- raise Ardb::Runner::CmdError, "can't generate #{@item}"
18
- end
19
-
20
- begin
21
- self.send("#{@item}_cmd")
22
- rescue Ardb::Runner::CmdError => e
23
- raise e
24
- rescue Exception => e
25
- $stderr.puts e
26
- $stderr.puts "error generating #{@item}."
27
- raise Ardb::Runner::CmdFail
28
- end
29
- end
30
-
31
- def migration_cmd
32
- MigrationCommand.new(@args.first).run
33
- end
34
-
35
- class MigrationCommand
36
- attr_reader :identifier, :class_name, :file_name, :template
37
-
38
- def initialize(identifier)
39
- if identifier.nil?
40
- raise Ardb::Runner::CmdError, "specify a name for the migration"
41
- end
42
-
43
- @identifier = identifier
44
- @class_name = @identifier.classify.pluralize
45
- @file_name = begin
46
- "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{@identifier.underscore}"
47
- end
48
- @template = "require 'ardb/migration_helpers'\n\n"\
49
- "class #{@class_name} < ActiveRecord::Migration\n"\
50
- " include Ardb::MigrationHelpers\n\n"\
51
- " def change\n"\
52
- " end\n\n"\
53
- "end\n"
54
- end
55
-
56
- def run
57
- FileUtils.mkdir_p Ardb.config.migrations_path
58
- file_path = File.join(Ardb.config.migrations_path, "#{@file_name}.rb")
59
- File.open(file_path, "w"){ |f| f.write(@template) }
60
- $stdout.puts file_path
61
- end
62
- end
63
-
64
- end
@@ -1,28 +0,0 @@
1
- require 'fileutils'
2
- require 'active_record'
3
- require 'ardb/runner'
4
-
5
- class Ardb::Runner::MigrateCommand
6
-
7
- def initialize(out_io = nil, err_io = nil)
8
- @out_io = out_io || $stdout
9
- @err_io = err_io || $stderr
10
- @adapter = Ardb::Adapter.send(Ardb.config.db.adapter)
11
- end
12
-
13
- def run
14
- begin
15
- Ardb.init
16
- @adapter.migrate_db
17
- @adapter.dump_schema unless ENV['ARDB_MIGRATE_NO_SCHEMA']
18
- rescue Ardb::Runner::CmdError => e
19
- raise e
20
- rescue StandardError => e
21
- @err_io.puts "error migrating #{Ardb.config.db.database.inspect} database"
22
- @err_io.puts e
23
- @err_io.puts e.backtrace
24
- raise Ardb::Runner::CmdFail
25
- end
26
- end
27
-
28
- end
@@ -1,17 +0,0 @@
1
- require 'assert'
2
- require 'ardb/runner/connect_command'
3
-
4
- class Ardb::Runner::ConnectCommand
5
-
6
- class UnitTests < Assert::Context
7
- desc "Ardb::Runner::ConnectCommand"
8
- setup do
9
- @cmd = Ardb::Runner::ConnectCommand.new
10
- end
11
- subject{ @cmd }
12
-
13
- should have_imeths :run
14
-
15
- end
16
-
17
- end
@@ -1,67 +0,0 @@
1
- require 'assert'
2
- require 'ardb/adapter_spy'
3
- require 'ardb/runner/create_command'
4
-
5
- class Ardb::Runner::CreateCommand
6
-
7
- class UnitTests < Assert::Context
8
- desc "Ardb::Runner::CreateCommand"
9
- setup do
10
- @command_class = Ardb::Runner::CreateCommand
11
- end
12
-
13
- end
14
-
15
- class InitTests < UnitTests
16
- desc "when init"
17
- setup do
18
- @adapter_spy = Class.new{ include Ardb::AdapterSpy }.new
19
- Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
20
-
21
- # provide an output and error IO to avoid using $stdout/$stderr in tests
22
- out_io = err_io = StringIO.new
23
- @command = @command_class.new(out_io, err_io)
24
- end
25
- subject{ @command }
26
-
27
- should have_imeths :run
28
-
29
- end
30
-
31
- class RunTests < InitTests
32
- desc "and run"
33
- setup do
34
- @command.run
35
- end
36
-
37
- should "create the db via the adapter" do
38
- assert_equal true, @adapter_spy.create_db_called?
39
- end
40
-
41
- end
42
-
43
- class RunWithCmdErrorTests < InitTests
44
- desc "and run with command errors"
45
- setup do
46
- Assert.stub(@adapter_spy, :create_db){ raise Ardb::Runner::CmdError.new }
47
- end
48
-
49
- should "not handle the error" do
50
- assert_raises(Ardb::Runner::CmdError){ subject.run }
51
- end
52
-
53
- end
54
-
55
- class RunWithUnspecifiedErrorTests < InitTests
56
- desc "and run with a standard error"
57
- setup do
58
- Assert.stub(@adapter_spy, :create_db){ raise StandardError.new }
59
- end
60
-
61
- should "raise a CmdFail error" do
62
- assert_raises(Ardb::Runner::CmdFail){ subject.run }
63
- end
64
-
65
- end
66
-
67
- end
@@ -1,68 +0,0 @@
1
- require 'assert'
2
- require 'ardb/adapter_spy'
3
- require 'ardb/runner/drop_command'
4
-
5
- class Ardb::Runner::DropCommand
6
-
7
- class UnitTests < Assert::Context
8
- desc "Ardb::Runner::DropCommand"
9
- setup do
10
- @command_class = Ardb::Runner::DropCommand
11
- end
12
-
13
- end
14
-
15
- class InitTests < UnitTests
16
- desc "when init"
17
- setup do
18
- @adapter_spy = Class.new{ include Ardb::AdapterSpy }.new
19
- Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
20
-
21
- # provide an output and error IO to avoid using $stdout/$stderr in tests
22
- out_io = err_io = StringIO.new
23
- @command = @command_class.new(out_io, err_io)
24
- end
25
- subject{ @command }
26
-
27
- should have_imeths :run
28
-
29
- end
30
-
31
- class RunTests < InitTests
32
- desc "and run"
33
- setup do
34
- @command.run
35
- end
36
-
37
- should "drop the db via the adapter" do
38
- assert_equal true, @adapter_spy.drop_db_called?
39
- end
40
-
41
- end
42
-
43
- class RunWithCmdErrorTests < InitTests
44
- desc "and run with command errors"
45
- setup do
46
- Assert.stub(@adapter_spy, :drop_db){ raise Ardb::Runner::CmdError.new }
47
- end
48
-
49
- should "not handle the error" do
50
- assert_raises(Ardb::Runner::CmdError){ subject.run }
51
- end
52
-
53
- end
54
-
55
- class RunWithUnspecifiedErrorTests < InitTests
56
- desc "and run with a standard error"
57
- setup do
58
- Assert.stub(@adapter_spy, :drop_db){ raise StandardError.new }
59
- end
60
-
61
- should "raise a CmdFail error" do
62
- assert_raises(Ardb::Runner::CmdFail){ subject.run }
63
- end
64
-
65
- end
66
-
67
- end
68
-
@@ -1,46 +0,0 @@
1
- require 'assert'
2
- require 'ardb/runner/generate_command'
3
-
4
- class Ardb::Runner::GenerateCommand
5
-
6
- class UnitTests < Assert::Context
7
- desc "Ardb::Runner::GenerateCommand"
8
- setup do
9
- @cmd = Ardb::Runner::GenerateCommand.new(['something'])
10
- end
11
- subject{ @cmd }
12
-
13
- should have_imeths :run, :migration_cmd
14
-
15
- end
16
-
17
- class MigrationTests < UnitTests
18
- desc "Ardb::Runner::GenerateCommand::MigrationCommand"
19
- setup do
20
- @cmd = Ardb::Runner::GenerateCommand::MigrationCommand.new('a_migration')
21
- end
22
-
23
- should have_readers :identifier, :class_name, :file_name, :template
24
-
25
- should "know its given identifier" do
26
- assert_equal 'a_migration', subject.identifier
27
- end
28
-
29
- should "know its class name" do
30
- assert_equal "AMigrations", subject.class_name
31
- end
32
-
33
- should "know its file name" do
34
- assert_match /.+_a_migration$/, subject.file_name
35
- end
36
-
37
- should "know its template" do
38
- assert_includes "require 'ardb/migration_helpers'", subject.template
39
- assert_includes "class #{subject.class_name} < ActiveRecord::Migration", subject.template
40
- assert_includes "include Ardb::MigrationHelpers", subject.template
41
- assert_includes "def change", subject.template
42
- end
43
-
44
- end
45
-
46
- end
@@ -1,96 +0,0 @@
1
- require 'assert'
2
- require 'ardb/adapter_spy'
3
- require 'ardb/runner/migrate_command'
4
-
5
- class Ardb::Runner::MigrateCommand
6
-
7
- class UnitTests < Assert::Context
8
- desc "Ardb::Runner::MigrateCommand"
9
- setup do
10
- @command_class = Ardb::Runner::MigrateCommand
11
- end
12
-
13
- end
14
-
15
- class InitTests < UnitTests
16
- desc "when init"
17
- setup do
18
- @adapter_spy = Class.new{ include Ardb::AdapterSpy }.new
19
- Assert.stub(Ardb::Adapter, Ardb.config.db.adapter.to_sym){ @adapter_spy }
20
-
21
- @ardb_init_called = false
22
- Assert.stub(Ardb, :init){ @ardb_init_called = true }
23
-
24
- # provide an output and error IO to avoid using $stdout/$stderr in tests
25
- out_io = err_io = StringIO.new
26
- @command = @command_class.new(out_io, err_io)
27
- end
28
- subject{ @command }
29
-
30
- should have_imeths :run
31
-
32
- end
33
-
34
- class RunSetupTests < InitTests
35
- desc "and run"
36
-
37
- end
38
-
39
- class RunTests < RunSetupTests
40
- setup do
41
- @command.run
42
- end
43
-
44
- should "initialize Ardb, migrate the db and dump schema via the adapter" do
45
- assert_true @ardb_init_called
46
- assert_true @adapter_spy.migrate_db_called?
47
- assert_true @adapter_spy.dump_schema_called?
48
- end
49
-
50
- end
51
-
52
- class RunWithNoSchemaEnvVarTests < RunSetupTests
53
- desc "with no schema dump env var set"
54
- setup do
55
- @current_no_schema = ENV['ARDB_MIGRATE_NO_SCHEMA']
56
- ENV['ARDB_MIGRATE_NO_SCHEMA'] = 'yes'
57
-
58
- @command.run
59
- end
60
- teardown do
61
- ENV['ARDB_MIGRATE_NO_SCHEMA'] = @current_no_schema
62
- end
63
-
64
- should "initialize Ardb and migrate the db but not dump schema" do
65
- assert_true @ardb_init_called
66
- assert_true @adapter_spy.migrate_db_called?
67
- assert_false @adapter_spy.dump_schema_called?
68
- end
69
-
70
- end
71
-
72
- class RunWithCmdErrorTests < InitTests
73
- desc "and run with command errors"
74
- setup do
75
- Assert.stub(@adapter_spy, :migrate_db){ raise Ardb::Runner::CmdError.new }
76
- end
77
-
78
- should "not handle the error" do
79
- assert_raises(Ardb::Runner::CmdError){ subject.run }
80
- end
81
-
82
- end
83
-
84
- class RunWithUnspecifiedErrorTests < InitTests
85
- desc "and run with a standard error"
86
- setup do
87
- Assert.stub(@adapter_spy, :migrate_db){ raise StandardError.new }
88
- end
89
-
90
- should "raise a CmdFail error" do
91
- assert_raises(Ardb::Runner::CmdFail){ subject.run }
92
- end
93
-
94
- end
95
-
96
- end