sequel-rails 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,7 +35,8 @@ module SequelRails
35
35
  db.execute("CREATE DATABASE IF NOT EXISTS `#{db_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
36
36
  end
37
37
  elsif _is_postgres?
38
- system("createdb #{db_name}")
38
+ adapter = ::SequelRails::Storage::Postgres.new(config)
39
+ adapter._create
39
40
  end
40
41
  end
41
42
 
@@ -48,14 +49,33 @@ module SequelRails
48
49
  db.execute("DROP DATABASE IF EXISTS `#{db_name}`")
49
50
  end
50
51
  elsif _is_postgres?
51
- system("dropdb #{db_name}")
52
+ adapter = ::SequelRails::Storage::Postgres.new(config)
53
+ adapter._drop
54
+ end
55
+ end
56
+
57
+ def _dump(filename)
58
+ if _is_postgres?
59
+ adapter = ::SequelRails::Storage::Postgres.new(config)
60
+ adapter._dump(filename)
61
+ else
62
+ raise NotImplementedError
63
+ end
64
+ end
65
+
66
+ def _load(filename)
67
+ if _is_postgres?
68
+ adapter = ::SequelRails::Storage::Postgres.new(config)
69
+ adapter._load(filename)
70
+ else
71
+ raise NotImplementedError
52
72
  end
53
73
  end
54
74
 
55
75
  private
56
76
 
57
77
  def collation
58
- @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
78
+ @collation ||= super || "utf8_unicode_ci"
59
79
  end
60
80
 
61
81
  def in_memory?
@@ -1,49 +1,50 @@
1
- require 'shellwords'
2
-
3
1
  module SequelRails
4
2
  module Storage
5
3
  class Mysql < Abstract
4
+
6
5
  def _create
7
- execute("CREATE DATABASE IF NOT EXISTS `#{database}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
6
+ execute "CREATE DATABASE IF NOT EXISTS `#{database}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}"
8
7
  end
9
8
 
10
9
  def _drop
11
- execute("DROP DATABASE IF EXISTS `#{database}`")
10
+ execute "DROP DATABASE IF EXISTS `#{database}`"
12
11
  end
13
12
 
14
13
  def _dump(filename)
15
- commands = %w(mysqldump --no-data)
16
- commands << "--user=#{Shellwords.escape(username)}" unless username.blank?
17
- commands << "--password=#{Shellwords.escape(password)}" unless password.blank?
18
- commands << "--host=#{host}" unless host.blank?
19
- commands << "--result-file" << filename
14
+ commands = ["mysqldump"]
15
+ add_connection_settings commands
16
+ add_flag commands, "--no-data"
17
+ add_option commands, "--result-file", filename
20
18
  commands << database
21
- system(*commands)
19
+ safe_exec commands
22
20
  end
23
21
 
24
22
  def _load(filename)
25
- commands = %w(mysql)
26
- commands << "--user=#{Shellwords.escape(username)}" unless username.blank?
27
- commands << "--password=#{Shellwords.escape(password)}" unless password.blank?
28
- commands << "--host=#{host}" unless host.blank?
29
- commands << '--execute' << %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}
30
- commands << '--database' << database
31
- system(*commands)
23
+ commands = ["mysql"]
24
+ add_connection_settings commands
25
+ add_option commands, "--database", database
26
+ add_option commands, "--execute", %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}
27
+ safe_exec commands
28
+ end
29
+
30
+ def collation
31
+ @collation ||= super || "utf8_unicode_ci"
32
32
  end
33
33
 
34
34
  private
35
35
 
36
- def execute(statement)
37
- commands = ["mysql"]
38
- commands << "--user=#{Shellwords.escape(username)}" unless username.blank?
39
- commands << "--password=#{Shellwords.escape(password)}" unless password.blank?
40
- commands << "--host=#{host}" unless host.blank?
41
- commands << "-e" << statement
42
- system(*commands)
36
+ def add_connection_settings(commands)
37
+ add_option commands, "--user", username
38
+ add_option commands, "--password", password
39
+ add_option commands, "--host", host
40
+ add_option commands, "--port", port.to_s
43
41
  end
44
42
 
45
- def collation
46
- @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
43
+ def execute(statement)
44
+ commands = ["mysql"]
45
+ add_connection_settings commands
46
+ add_option commands, "--execute", statement
47
+ safe_exec commands
47
48
  end
48
49
 
49
50
  end
@@ -1,55 +1,55 @@
1
1
  module SequelRails
2
2
  module Storage
3
3
  class Postgres < Abstract
4
+
4
5
  def _create
5
- ENV["PGPASSWORD"] = password unless password.blank?
6
- commands = ["createdb", "--encoding", charset]
7
- commands << "--username" << username unless username.blank?
8
- commands << "--owner" << owner unless owner.blank?
9
- commands << "--port" << port.to_s unless port.blank?
10
- commands << "--host" << host unless host.blank?
11
- commands << database
12
- res = system(*commands)
13
- ENV["PGPASSWORD"] = nil unless password.blank?
14
- res
6
+ with_pgpassword do
7
+ commands = ["createdb"]
8
+ add_connection_settings commands
9
+ add_option commands, "--maintenance-db", maintenance_db
10
+ add_option commands, "--encoding", encoding
11
+ add_option commands, "--locale", locale
12
+ add_option commands, "--lc-collate", collation
13
+ add_option commands, "--lc-ctype", ctype
14
+ add_option commands, "--template", template
15
+ add_option commands, "--tablespace", tablespace
16
+ add_option commands, "--owner", owner
17
+ commands << database
18
+ safe_exec commands
19
+ end
15
20
  end
16
21
 
17
22
  def _drop
18
- ENV["PGPASSWORD"] = password unless password.blank?
19
- commands = ["dropdb"]
20
- commands << "-U" << username unless username.blank?
21
- commands << "--port" << port.to_s unless port.blank?
22
- commands << "--host" << host unless host.blank?
23
- commands << database
24
- res = system(*commands)
25
- ENV["PGPASSWORD"] = nil unless password.blank?
26
- res
23
+ with_pgpassword do
24
+ commands = ["dropdb"]
25
+ add_connection_settings commands
26
+ commands << database
27
+ safe_exec commands
28
+ end
27
29
  end
28
30
 
29
31
  def _dump(filename)
30
- ENV["PGPASSWORD"] = password unless password.blank?
31
- commands = %w(pg_dump -i -s -x -O)
32
- commands << "-f" << filename
33
- commands << "-U" << username unless username.blank?
34
- commands << "--port" << port.to_s unless port.blank?
35
- commands << "--host" << host unless host.blank?
36
- commands << database
37
- res = system(*commands)
38
- ENV["PGPASSWORD"] = nil unless password.blank?
39
- res
32
+ with_pgpassword do
33
+ commands = ["pg_dump"]
34
+ add_connection_settings commands
35
+ add_flag commands, "-i"
36
+ add_flag commands, "-s"
37
+ add_flag commands, "-x"
38
+ add_flag commands, "-O"
39
+ add_option commands, "--file", filename
40
+ commands << database
41
+ safe_exec commands
42
+ end
40
43
  end
41
44
 
42
45
  def _load(filename)
43
- ENV["PGPASSWORD"] = password unless password.blank?
44
- commands = %w(psql)
45
- commands << "-f" << filename
46
- commands << "-U" << username unless username.blank?
47
- commands << "--port" << port.to_s unless port.blank?
48
- commands << "--host" << host unless host.blank?
49
- commands << database
50
- res = system(*commands)
51
- ENV["PGPASSWORD"] = nil unless password.blank?
52
- res
46
+ with_pgpassword do
47
+ commands = ["psql"]
48
+ add_connection_settings commands
49
+ add_option commands, "--file", filename
50
+ commands << database
51
+ safe_exec commands
52
+ end
53
53
  end
54
54
 
55
55
  def close_connections
@@ -68,6 +68,46 @@ module SequelRails
68
68
  # are closed
69
69
  end
70
70
  end
71
+
72
+ def encoding
73
+ @encoding ||= config["encoding"] || charset
74
+ end
75
+
76
+ def locale
77
+ @locale ||= config["locale"] || ""
78
+ end
79
+
80
+ def template
81
+ @template ||= config["template"] || ""
82
+ end
83
+
84
+ def ctype
85
+ @ctype ||= config["ctype"] || ""
86
+ end
87
+
88
+ def tablespace
89
+ @tablespace ||= config["tablespace"] || ""
90
+ end
91
+
92
+ def maintenance_db
93
+ @maintenance_db ||= config["maintenance_db"] || ""
94
+ end
95
+
96
+ private
97
+
98
+ def with_pgpassword
99
+ ENV["PGPASSWORD"] = password unless password.blank?
100
+ yield
101
+ ensure
102
+ ENV["PGPASSWORD"] = nil unless password.blank?
103
+ end
104
+
105
+ def add_connection_settings(commands)
106
+ add_option commands, "--username", username
107
+ add_option commands, "--host", host
108
+ add_option commands, "--port", port.to_s
109
+ end
110
+
71
111
  end
72
112
  end
73
113
  end
@@ -13,12 +13,16 @@ module SequelRails
13
13
 
14
14
  def _dump(filename)
15
15
  return if in_memory?
16
- system "sqlite3 \"#{path.to_s}\" .schema > \"#{filename}\""
16
+ escaped_path = SequelRails::Shellwords.shellescape(path.to_s)
17
+ escaped_filename = SequelRails::Shellwords.shellescape(filename)
18
+ exec "sqlite3 #{escaped_path} .schema > #{escaped_filename}"
17
19
  end
18
20
 
19
21
  def _load(filename)
20
22
  return if in_memory?
21
- system "sqlite3 \"#{path.to_s}\" < \"#{filename}\""
23
+ escaped_path = SequelRails::Shellwords.shellescape(path.to_s)
24
+ escaped_filename = SequelRails::Shellwords.shellescape(filename)
25
+ exec "sqlite3 #{escaped_path} < #{escaped_filename}"
22
26
  end
23
27
 
24
28
  private
@@ -1,3 +1,3 @@
1
1
  module SequelRails
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
data/sequel-rails.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.platform = Gem::Platform::RUBY
10
10
  s.authors = ["Brasten Sager (brasten)", "Jonathan TRON"]
11
11
  s.email = ["brasten@gmail.com", "jonathan.tron@metrilio.com"]
12
- s.homepage = "https://github.com/TalentBox/sequel-rails"
12
+ s.homepage = "http://talentbox.github.io/sequel-rails/"
13
13
  s.description = "Integrate Sequel with Rails 3"
14
14
  s.summary = "Use Sequel with Rails 3"
15
15
  s.files = `git ls-files`.split("\n")
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ["lib"]
19
19
  s.extra_rdoc_files = ["LICENSE", "README.md"]
20
20
  s.rdoc_options = ["--charset=UTF-8"]
21
+ s.license = "MIT"
21
22
 
22
23
  s.add_runtime_dependency "sequel", [">= 3.28", "< 5.0"]
23
24
  s.add_runtime_dependency "railties", ">= 3.2.0"
@@ -1,9 +1,89 @@
1
1
  require "spec_helper"
2
2
 
3
- describe SequelRails::Configuration do
3
+ describe SequelRails do
4
4
 
5
5
  describe ".setup" do
6
+ let(:environment) { "development" }
7
+ let(:configuration) { SequelRails::Configuration.new }
8
+
9
+ it "delegates to current configuration" do
10
+ SequelRails.configuration = configuration
11
+ configuration.should_receive(:connect).with(environment)
12
+ SequelRails.setup environment
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ describe SequelRails::Configuration do
19
+
20
+ describe "#schema_dump" do
21
+ before{ Rails.stub(:env).and_return environment }
22
+ subject{ described_class.new }
6
23
 
24
+ context "in test environment" do
25
+ let(:environment) { "test" }
26
+ it "defaults to false" do
27
+ subject.schema_dump.should be_false
28
+ end
29
+ it "can be assigned" do
30
+ subject.schema_dump = true
31
+ subject.schema_dump.should be_true
32
+ end
33
+ it "can be set from merging another hash" do
34
+ subject.merge!(:schema_dump => true)
35
+ subject.schema_dump.should be_true
36
+ end
37
+ end
38
+
39
+ context "in production environment" do
40
+ let(:environment) { "production" }
41
+ it "defaults to false" do
42
+ subject.schema_dump.should be_false
43
+ end
44
+ it "can be assigned" do
45
+ subject.schema_dump = true
46
+ subject.schema_dump.should be_true
47
+ end
48
+ it "can be set from merging another hash" do
49
+ subject.merge!(:schema_dump => true)
50
+ subject.schema_dump.should be_true
51
+ end
52
+ end
53
+
54
+ context "in other environments" do
55
+ let(:environment) { "development" }
56
+ it "defaults to true" do
57
+ subject.schema_dump.should be_true
58
+ end
59
+ it "can be assigned" do
60
+ subject.schema_dump = false
61
+ subject.schema_dump.should be_false
62
+ end
63
+ it "can be set from merging another hash" do
64
+ subject.merge!(:schema_dump => false)
65
+ subject.schema_dump.should be_false
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "#load_database_tasks" do
71
+ subject{ described_class.new }
72
+
73
+ it "defaults to true" do
74
+ subject.load_database_tasks.should be_true
75
+ end
76
+ it "can be assigned" do
77
+ subject.load_database_tasks = false
78
+ subject.load_database_tasks.should be_false
79
+ end
80
+ it "can be set from merging another hash" do
81
+ subject.merge!(:load_database_tasks => false)
82
+ subject.load_database_tasks.should be_false
83
+ end
84
+ end
85
+
86
+ describe "#connect" do
7
87
  let(:environments) do
8
88
  {
9
89
  "development" => {
@@ -38,21 +118,20 @@ describe SequelRails::Configuration do
38
118
  end
39
119
  let(:is_jruby) { false }
40
120
 
41
- before do
42
- SequelRails.configuration = described_class.new
43
- SequelRails.configuration.raw = environments
44
- SequelRails.configuration.instance_variable_set('@environments', nil)
45
- SequelRails.stub(:jruby?).and_return is_jruby
121
+ subject do
122
+ config = described_class.new
123
+ config.raw = environments
124
+ config
46
125
  end
47
126
 
48
- subject { SequelRails.setup(environment) }
127
+ before { SequelRails.stub(:jruby?).and_return is_jruby }
49
128
 
50
129
  shared_examples "max_connections" do
51
130
  context "with max_connections config option" do
52
131
  let(:max_connections) { 31337 }
53
132
  before do
54
133
  environments[environment]["max_connections"] = 7
55
- SequelRails.configuration.max_connections = max_connections
134
+ subject.max_connections = max_connections
56
135
  end
57
136
 
58
137
  it "overrides the option from the configuration" do
@@ -63,7 +142,7 @@ describe SequelRails::Configuration do
63
142
  hash_or_url.should include("max_connections=#{max_connections}")
64
143
  end
65
144
  end
66
- subject
145
+ subject.connect environment
67
146
  end
68
147
  end
69
148
  end
@@ -75,7 +154,7 @@ describe SequelRails::Configuration do
75
154
  let(:search_path) { ['secret', 'private', 'public'] }
76
155
  before do
77
156
  environments[environment]["search_path"] = "private, public"
78
- SequelRails.configuration.search_path = search_path
157
+ subject.search_path = search_path
79
158
  end
80
159
 
81
160
  it "overrides the option from the configuration" do
@@ -86,7 +165,7 @@ describe SequelRails::Configuration do
86
165
  hash_or_url.should include("search_path=secret,private,public")
87
166
  end
88
167
  end
89
- subject
168
+ subject.connect environment
90
169
  end
91
170
  end
92
171
  end
@@ -102,7 +181,7 @@ describe SequelRails::Configuration do
102
181
  ::Sequel.should_receive(:connect) do |hash|
103
182
  hash['adapter'].should == 'postgres'
104
183
  end
105
- subject
184
+ subject.connect environment
106
185
  end
107
186
 
108
187
  end
@@ -120,7 +199,7 @@ describe SequelRails::Configuration do
120
199
  hash['adapter'].should == 'jdbc:postgresql'
121
200
  hash['host'].should == '127.0.0.1'
122
201
  end
123
- subject
202
+ subject.connect environment
124
203
  end
125
204
 
126
205
  context "when url is already given" do
@@ -132,7 +211,7 @@ describe SequelRails::Configuration do
132
211
  url.should == "jdbc:adapter_name://HOST/DB?user=U&password=P&ssl=true&sslfactory=sslFactoryOption"
133
212
  hash['adapter'].should == 'jdbc:adapter_name'
134
213
  end
135
- subject
214
+ subject.connect environment
136
215
  end
137
216
 
138
217
  end
@@ -151,7 +230,7 @@ describe SequelRails::Configuration do
151
230
  ::Sequel.should_receive(:connect) do |hash|
152
231
  hash['adapter'].should == 'mysql'
153
232
  end
154
- subject
233
+ subject.connect environment
155
234
  end
156
235
  end
157
236
 
@@ -167,7 +246,7 @@ describe SequelRails::Configuration do
167
246
  hash['adapter'].should == 'jdbc:mysql'
168
247
  hash['database'].should == 'sequel_rails_test_storage_remote'
169
248
  end
170
- subject
249
+ subject.connect environment
171
250
  end
172
251
 
173
252
  context "when url is already given" do
@@ -179,77 +258,23 @@ describe SequelRails::Configuration do
179
258
  url.should == "jdbc:adapter_name://HOST/DB?user=U&password=P&ssl=true&sslfactory=sslFactoryOption"
180
259
  hash['adapter'].should == 'jdbc:adapter_name'
181
260
  end
182
- subject
261
+ subject.connect environment
183
262
  end
184
263
 
185
264
  end
186
265
  end
187
266
  end
188
- end
189
-
190
- describe "#schema_dump" do
191
- before{ Rails.stub(:env).and_return environment }
192
- subject{ described_class.new }
193
-
194
- context "in test environment" do
195
- let(:environment) { "test" }
196
- it "defaults to false" do
197
- subject.schema_dump.should be_false
198
- end
199
- it "can be assigned" do
200
- subject.schema_dump = true
201
- subject.schema_dump.should be_true
202
- end
203
- it "can be set from merging another hash" do
204
- subject.merge!(:schema_dump => true)
205
- subject.schema_dump.should be_true
206
- end
207
- end
208
-
209
- context "in production environment" do
210
- let(:environment) { "production" }
211
- it "defaults to false" do
212
- subject.schema_dump.should be_false
213
- end
214
- it "can be assigned" do
215
- subject.schema_dump = true
216
- subject.schema_dump.should be_true
217
- end
218
- it "can be set from merging another hash" do
219
- subject.merge!(:schema_dump => true)
220
- subject.schema_dump.should be_true
221
- end
222
- end
223
267
 
224
- context "in other environments" do
268
+ describe "after connect hook" do
269
+ let(:hook) { double }
225
270
  let(:environment) { "development" }
226
- it "defaults to true" do
227
- subject.schema_dump.should be_true
228
- end
229
- it "can be assigned" do
230
- subject.schema_dump = false
231
- subject.schema_dump.should be_false
232
- end
233
- it "can be set from merging another hash" do
234
- subject.merge!(:schema_dump => false)
235
- subject.schema_dump.should be_false
236
- end
237
- end
238
- end
271
+ before { SequelRails.unstub :jruby? }
239
272
 
240
- describe "#load_database_tasks" do
241
- subject{ described_class.new }
242
-
243
- it "defaults to true" do
244
- subject.load_database_tasks.should be_true
245
- end
246
- it "can be assigned" do
247
- subject.load_database_tasks = false
248
- subject.load_database_tasks.should be_false
249
- end
250
- it "can be set from merging another hash" do
251
- subject.merge!(:load_database_tasks => false)
252
- subject.load_database_tasks.should be_false
273
+ it "runs hook if provided" do
274
+ subject.after_connect = hook
275
+ hook.should_receive(:call)
276
+ subject.connect environment
277
+ end
253
278
  end
254
279
  end
255
280
  end