ardb 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ardb.rb +1 -1
- data/lib/ardb/adapter/base.rb +8 -0
- data/lib/ardb/adapter/postgresql.rb +13 -9
- data/lib/ardb/adapter_spy.rb +59 -0
- data/lib/ardb/test_helpers.rb +14 -6
- data/lib/ardb/version.rb +1 -1
- data/test/unit/adapter/base_tests.rb +29 -1
- data/test/unit/adapter_spy_tests.rb +66 -0
- data/test/unit/test_helpers_tests.rb +76 -1
- metadata +21 -17
data/lib/ardb.rb
CHANGED
data/lib/ardb/adapter/base.rb
CHANGED
@@ -17,6 +17,14 @@ class Ardb::Adapter::Base
|
|
17
17
|
|
18
18
|
def drop_tables(*args); raise NotImplementedError; end
|
19
19
|
|
20
|
+
def load_schema
|
21
|
+
# silence STDOUT
|
22
|
+
current_stdout = $stdout.dup
|
23
|
+
$stdout = File.new('/dev/null', 'w')
|
24
|
+
load Ardb.config.schema_path
|
25
|
+
$stdout = current_stdout
|
26
|
+
end
|
27
|
+
|
20
28
|
def ==(other_adapter)
|
21
29
|
self.class == other_adapter.class
|
22
30
|
end
|
@@ -19,15 +19,19 @@ class Ardb::Adapter
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def drop_db
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
22
|
+
begin
|
23
|
+
ActiveRecord::Base.establish_connection(self.public_schema_settings)
|
24
|
+
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
|
28
|
+
# http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL-TABLE
|
29
|
+
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}"
|
32
|
+
end
|
33
|
+
rescue PG::Error => e
|
34
|
+
raise e unless e.message =~ /does not exist/
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Ardb
|
2
|
+
|
3
|
+
module AdapterSpy
|
4
|
+
|
5
|
+
def self.new(&block)
|
6
|
+
block ||= proc{ }
|
7
|
+
record_spy = Class.new{ include Ardb::AdapterSpy }
|
8
|
+
record_spy.class_eval(&block)
|
9
|
+
record_spy
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.included(klass)
|
13
|
+
klass.class_eval do
|
14
|
+
include InstanceMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module InstanceMethods
|
19
|
+
|
20
|
+
attr_accessor :drop_tables_called_count, :load_schema_called_count
|
21
|
+
attr_accessor :drop_db_called_count, :create_db_called_count
|
22
|
+
|
23
|
+
def drop_tables_called_count
|
24
|
+
@drop_tables_called_count ||= 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def drop_tables(*args, &block)
|
28
|
+
self.drop_tables_called_count += 1
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_schema_called_count
|
32
|
+
@load_schema_called_count ||= 0
|
33
|
+
end
|
34
|
+
|
35
|
+
def load_schema(*args, &block)
|
36
|
+
self.load_schema_called_count += 1
|
37
|
+
end
|
38
|
+
|
39
|
+
def drop_db_called_count
|
40
|
+
@drop_db_called_count ||= 0
|
41
|
+
end
|
42
|
+
|
43
|
+
def drop_db(*args, &block)
|
44
|
+
self.drop_db_called_count += 1
|
45
|
+
end
|
46
|
+
|
47
|
+
def create_db_called_count
|
48
|
+
@create_db_called_count ||= 0
|
49
|
+
end
|
50
|
+
|
51
|
+
def create_db(*args, &block)
|
52
|
+
self.create_db_called_count += 1
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/lib/ardb/test_helpers.rb
CHANGED
@@ -13,12 +13,20 @@ module Ardb::TestHelpers
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def load_schema
|
16
|
-
|
17
|
-
current_stdout = $stdout.dup
|
18
|
-
$stdout = File.new('/dev/null', 'w')
|
19
|
-
load Ardb.config.schema_path
|
20
|
-
$stdout = current_stdout
|
16
|
+
Ardb.adapter.load_schema
|
21
17
|
end
|
22
18
|
|
23
|
-
|
19
|
+
def reset_db!
|
20
|
+
Ardb.adapter.drop_db
|
21
|
+
Ardb.adapter.create_db
|
22
|
+
self.load_schema
|
23
|
+
end
|
24
24
|
|
25
|
+
def reset_db
|
26
|
+
@reset_db ||= begin
|
27
|
+
self.reset_db!
|
28
|
+
true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/lib/ardb/version.rb
CHANGED
@@ -12,7 +12,7 @@ class Ardb::Adapter::Base
|
|
12
12
|
|
13
13
|
should have_reader :config_settings, :database
|
14
14
|
should have_imeths :foreign_key_add_sql, :foreign_key_drop_sql
|
15
|
-
should have_imeths :create_db, :drop_db
|
15
|
+
should have_imeths :create_db, :drop_db, :load_schema
|
16
16
|
|
17
17
|
should "use the config's db settings " do
|
18
18
|
assert_equal Ardb.config.db_settings, subject.config_settings
|
@@ -38,4 +38,32 @@ class Ardb::Adapter::Base
|
|
38
38
|
|
39
39
|
end
|
40
40
|
|
41
|
+
class LoadSchemaTests < BaseTests
|
42
|
+
desc "given a schema"
|
43
|
+
setup do
|
44
|
+
::FAKE_SCHEMA_LOAD = OpenStruct.new(:count => 0)
|
45
|
+
@orig_schema_path = Ardb.config.schema_path
|
46
|
+
Ardb.config.schema_path = 'fake_schema.rb'
|
47
|
+
end
|
48
|
+
teardown do
|
49
|
+
Ardb.config.schema_path = @orig_schema_path
|
50
|
+
end
|
51
|
+
|
52
|
+
should "load the schema suppressing $stdout" do
|
53
|
+
orig_stdout = $stdout.dup
|
54
|
+
captured_stdout = ""
|
55
|
+
$stdout = StringIO.new(captured_stdout)
|
56
|
+
|
57
|
+
assert_equal 0, FAKE_SCHEMA_LOAD.count
|
58
|
+
subject.load_schema
|
59
|
+
assert_equal 1, FAKE_SCHEMA_LOAD.count
|
60
|
+
subject.load_schema
|
61
|
+
assert_equal 2, FAKE_SCHEMA_LOAD.count
|
62
|
+
assert_empty captured_stdout
|
63
|
+
|
64
|
+
$stdout = orig_stdout
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
41
69
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'ardb/adapter_spy'
|
3
|
+
|
4
|
+
module Ardb::AdapterSpy
|
5
|
+
|
6
|
+
class MyAdapter
|
7
|
+
include Ardb::AdapterSpy
|
8
|
+
end
|
9
|
+
|
10
|
+
class BaseTests < Assert::Context
|
11
|
+
desc "Ardb::AdapterSpy"
|
12
|
+
setup do
|
13
|
+
@adapter = MyAdapter.new
|
14
|
+
end
|
15
|
+
subject{ @adapter }
|
16
|
+
|
17
|
+
should have_accessors :drop_tables_called_count, :load_schema_called_count
|
18
|
+
should have_accessors :drop_db_called_count, :create_db_called_count
|
19
|
+
should have_imeths :drop_tables, :load_schema, :drop_db, :create_db
|
20
|
+
|
21
|
+
should "included the record spy instance methods" do
|
22
|
+
assert_includes Ardb::AdapterSpy::InstanceMethods, subject.class.included_modules
|
23
|
+
end
|
24
|
+
|
25
|
+
should "default all call counts to zero" do
|
26
|
+
assert_equal 0, subject.drop_tables_called_count
|
27
|
+
assert_equal 0, subject.load_schema_called_count
|
28
|
+
assert_equal 0, subject.drop_db_called_count
|
29
|
+
assert_equal 0, subject.create_db_called_count
|
30
|
+
end
|
31
|
+
|
32
|
+
should "add a call count when each method is called" do
|
33
|
+
subject.drop_tables
|
34
|
+
assert_equal 1, subject.drop_tables_called_count
|
35
|
+
|
36
|
+
subject.load_schema
|
37
|
+
assert_equal 1, subject.load_schema_called_count
|
38
|
+
|
39
|
+
subject.drop_db
|
40
|
+
assert_equal 1, subject.drop_db_called_count
|
41
|
+
|
42
|
+
subject.create_db
|
43
|
+
assert_equal 1, subject.create_db_called_count
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
class NewMethTests < BaseTests
|
49
|
+
desc "`new` method"
|
50
|
+
setup do
|
51
|
+
@adapter_spy_class = Ardb::AdapterSpy.new do
|
52
|
+
attr_accessor :name
|
53
|
+
end
|
54
|
+
@adapter = @adapter_spy_class.new
|
55
|
+
end
|
56
|
+
subject{ @adapter }
|
57
|
+
|
58
|
+
should "build a new spy class and use any custom definition" do
|
59
|
+
assert_includes Ardb::AdapterSpy, subject.class.included_modules
|
60
|
+
assert subject.respond_to? :name
|
61
|
+
assert subject.respond_to? :name=
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -7,7 +7,82 @@ module Ardb::TestHelpers
|
|
7
7
|
desc "Ardb test helpers"
|
8
8
|
subject{ Ardb::TestHelpers }
|
9
9
|
|
10
|
-
should have_imeths :drop_tables, :load_schema
|
10
|
+
should have_imeths :drop_tables, :load_schema, :reset_db, :reset_db!
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
class UsageTests < BaseTests
|
15
|
+
setup do
|
16
|
+
@adapter_spy_class = Ardb::AdapterSpy.new
|
17
|
+
@orig_ardb_adapter = Ardb.adapter
|
18
|
+
Ardb::Adapter.current = @adapter_spy = @adapter_spy_class.new
|
19
|
+
end
|
20
|
+
teardown do
|
21
|
+
Ardb::Adapter.current = @orig_ardb_adapter
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
class DropTablesTests < UsageTests
|
27
|
+
desc "`drop_tables` method"
|
28
|
+
|
29
|
+
should "tell the adapter to drop the tables" do
|
30
|
+
assert_equal 0, @adapter_spy.drop_tables_called_count
|
31
|
+
subject.drop_tables
|
32
|
+
assert_equal 1, @adapter_spy.drop_tables_called_count
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
class LoadSchemaTests < UsageTests
|
38
|
+
desc "`load_schema` method"
|
39
|
+
|
40
|
+
should "tell the adapter to load the schema" do
|
41
|
+
assert_equal 0, @adapter_spy.load_schema_called_count
|
42
|
+
subject.load_schema
|
43
|
+
assert_equal 1, @adapter_spy.load_schema_called_count
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
class ResetDbTests < UsageTests
|
49
|
+
desc "reset db methods"
|
50
|
+
|
51
|
+
should "tell the adapter to drop/create the db and load the schema only once" do
|
52
|
+
assert_equal 0, @adapter_spy.drop_db_called_count
|
53
|
+
assert_equal 0, @adapter_spy.create_db_called_count
|
54
|
+
assert_equal 0, @adapter_spy.load_schema_called_count
|
55
|
+
|
56
|
+
subject.reset_db
|
57
|
+
|
58
|
+
assert_equal 1, @adapter_spy.drop_db_called_count
|
59
|
+
assert_equal 1, @adapter_spy.create_db_called_count
|
60
|
+
assert_equal 1, @adapter_spy.load_schema_called_count
|
61
|
+
|
62
|
+
subject.reset_db
|
63
|
+
|
64
|
+
assert_equal 1, @adapter_spy.drop_db_called_count
|
65
|
+
assert_equal 1, @adapter_spy.create_db_called_count
|
66
|
+
assert_equal 1, @adapter_spy.load_schema_called_count
|
67
|
+
end
|
68
|
+
|
69
|
+
should "force the adapter to drop/create the db and load the schema" do
|
70
|
+
assert_equal 0, @adapter_spy.drop_db_called_count
|
71
|
+
assert_equal 0, @adapter_spy.create_db_called_count
|
72
|
+
assert_equal 0, @adapter_spy.load_schema_called_count
|
73
|
+
|
74
|
+
subject.reset_db!
|
75
|
+
|
76
|
+
assert_equal 1, @adapter_spy.drop_db_called_count
|
77
|
+
assert_equal 1, @adapter_spy.create_db_called_count
|
78
|
+
assert_equal 1, @adapter_spy.load_schema_called_count
|
79
|
+
|
80
|
+
subject.reset_db!
|
81
|
+
|
82
|
+
assert_equal 2, @adapter_spy.drop_db_called_count
|
83
|
+
assert_equal 2, @adapter_spy.create_db_called_count
|
84
|
+
assert_equal 2, @adapter_spy.load_schema_called_count
|
85
|
+
end
|
11
86
|
|
12
87
|
end
|
13
88
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ardb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 55
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 10
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.10.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,11 +16,12 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-06-
|
19
|
+
date: 2013-06-24 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
+
name: assert
|
22
23
|
prerelease: false
|
23
|
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
25
|
none: false
|
25
26
|
requirements:
|
26
27
|
- - ~>
|
@@ -30,12 +31,12 @@ dependencies:
|
|
30
31
|
- 2
|
31
32
|
- 0
|
32
33
|
version: "2.0"
|
33
|
-
requirement: *id001
|
34
|
-
name: assert
|
35
34
|
type: :development
|
35
|
+
version_requirements: *id001
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
+
name: activerecord
|
37
38
|
prerelease: false
|
38
|
-
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
40
|
none: false
|
40
41
|
requirements:
|
41
42
|
- - ~>
|
@@ -45,12 +46,12 @@ dependencies:
|
|
45
46
|
- 3
|
46
47
|
- 2
|
47
48
|
version: "3.2"
|
48
|
-
requirement: *id002
|
49
|
-
name: activerecord
|
50
49
|
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
|
+
name: activesupport
|
52
53
|
prerelease: false
|
53
|
-
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
55
|
none: false
|
55
56
|
requirements:
|
56
57
|
- - ~>
|
@@ -60,12 +61,12 @@ dependencies:
|
|
60
61
|
- 3
|
61
62
|
- 2
|
62
63
|
version: "3.2"
|
63
|
-
requirement: *id003
|
64
|
-
name: activesupport
|
65
64
|
type: :runtime
|
65
|
+
version_requirements: *id003
|
66
66
|
- !ruby/object:Gem::Dependency
|
67
|
+
name: ns-options
|
67
68
|
prerelease: false
|
68
|
-
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
70
|
none: false
|
70
71
|
requirements:
|
71
72
|
- - ~>
|
@@ -75,9 +76,8 @@ dependencies:
|
|
75
76
|
- 1
|
76
77
|
- 1
|
77
78
|
version: "1.1"
|
78
|
-
requirement: *id004
|
79
|
-
name: ns-options
|
80
79
|
type: :runtime
|
80
|
+
version_requirements: *id004
|
81
81
|
description: Activerecord database tools.
|
82
82
|
email:
|
83
83
|
- kelly@kellyredding.com
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- lib/ardb/adapter/mysql.rb
|
102
102
|
- lib/ardb/adapter/postgresql.rb
|
103
103
|
- lib/ardb/adapter/sqlite.rb
|
104
|
+
- lib/ardb/adapter_spy.rb
|
104
105
|
- lib/ardb/cli.rb
|
105
106
|
- lib/ardb/migration_helpers.rb
|
106
107
|
- lib/ardb/record_spy.rb
|
@@ -119,6 +120,7 @@ files:
|
|
119
120
|
- test/unit/adapter/mysql_tests.rb
|
120
121
|
- test/unit/adapter/postgresql_tests.rb
|
121
122
|
- test/unit/adapter/sqlite_tests.rb
|
123
|
+
- test/unit/adapter_spy_tests.rb
|
122
124
|
- test/unit/ardb_tests.rb
|
123
125
|
- test/unit/config_tests.rb
|
124
126
|
- test/unit/migration_helpers_tests.rb
|
@@ -142,6 +144,7 @@ files:
|
|
142
144
|
- tmp/sqlitetest/config/.gitkeep
|
143
145
|
- tmp/testdb/config/.gitkeep
|
144
146
|
- tmp/testdb/config/db.rb
|
147
|
+
- tmp/testdb/fake_schema.rb
|
145
148
|
homepage: http://github.com/redding/ardb
|
146
149
|
licenses: []
|
147
150
|
|
@@ -171,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
174
|
requirements: []
|
172
175
|
|
173
176
|
rubyforge_project:
|
174
|
-
rubygems_version: 1.8.
|
177
|
+
rubygems_version: 1.8.24
|
175
178
|
signing_key:
|
176
179
|
specification_version: 3
|
177
180
|
summary: Activerecord database tools.
|
@@ -181,6 +184,7 @@ test_files:
|
|
181
184
|
- test/unit/adapter/mysql_tests.rb
|
182
185
|
- test/unit/adapter/postgresql_tests.rb
|
183
186
|
- test/unit/adapter/sqlite_tests.rb
|
187
|
+
- test/unit/adapter_spy_tests.rb
|
184
188
|
- test/unit/ardb_tests.rb
|
185
189
|
- test/unit/config_tests.rb
|
186
190
|
- test/unit/migration_helpers_tests.rb
|