Skip to content
This repository

Quick commit for allowing required config options for mysql and postgres #12

Open
wants to merge 5 commits into from

3 participants

Tom Meier Josh Bassett javi santana
Tom Meier

In our local setup we run create/drop on dbs from a cruise server to our seperate (remote) db. So I found I couldn't connect without some key config attributes set, especially 'host' so it doesn't default to 'localhost'.

The second commit simply adds some of these key config values to the create/drop commands on mysql and postgres if they're present in the passed database.yml.

The first commit just bumps the rails beta version, as the required one no longer exists and means i couldn't build the gemset.

Thanks for a great gem!

Tom

added some commits March 18, 2011
Tom Meier Update rails beta version 6be575c
Tom Meier On Create / Drop add missing required config options. Locally it work…
…s without these options, but when running with remote databases (eg, UAT environments) these config options are required or obscure error messages happen.

Postgres -
  - Add config options for owner, port, and host
MySQL -
  - Add config options for host
3d98f8c
Josh Bassett

+1

Tom Meier

Also - Additional rake task that will close any open sessions on the test database. Avoids problems on test build servers etc that don't close the process gracefully.

Markus Fenske iblue referenced this pull request from a commit in blacklane/sequel-rails February 09, 2013
Jonathan Tron Make dependency on railtie looser (fixes #12) 1104793
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 5 unique commits by 1 author.

Mar 18, 2011
Tom Meier Update rails beta version 6be575c
Tom Meier On Create / Drop add missing required config options. Locally it work…
…s without these options, but when running with remote databases (eg, UAT environments) these config options are required or obscure error messages happen.

Postgres -
  - Add config options for owner, port, and host
MySQL -
  - Add config options for host
3d98f8c
May 13, 2011
Tom Meier Fix bug with setting multiple arguments in system command f8e6bb4
Tom Meier Force close any open connections on postgres 8f0906f
Tom Meier Fix - Use Sequel model directly 9b87d09
This page is out of date. Refresh to see the latest.
6  Gemfile
@@ -6,9 +6,9 @@ gem 'yard',                 '~> 0.5'
6 6
 
7 7
 git 'git://github.com/rails/rails.git' do
8 8
 
9  
-  gem 'activesupport',      '~> 3.0.0.beta3', :require => 'active_support'
10  
-  gem 'actionpack',         '~> 3.0.0.beta3', :require => 'action_pack'
11  
-  gem 'railties',           '~> 3.0.0.beta3', :require => 'rails'
  9
+  gem 'activesupport',      '~> 3.1.0.beta', :require => 'active_support'
  10
+  gem 'actionpack',         '~> 3.1.0.beta', :require => 'action_pack'
  11
+  gem 'railties',           '~> 3.1.0.beta', :require => 'rails'
12 12
 
13 13
 end
14 14
 
81  Gemfile.lock
... ...
@@ -0,0 +1,81 @@
  1
+GIT
  2
+  remote: git://github.com/rails/rails.git
  3
+  revision: 4532b39f5faa15af957e3b41b671f07ed201488d
  4
+  specs:
  5
+    actionpack (3.1.0.beta)
  6
+      activemodel (= 3.1.0.beta)
  7
+      activesupport (= 3.1.0.beta)
  8
+      builder (~> 3.0.0)
  9
+      erubis (~> 2.6.6)
  10
+      i18n (~> 0.5.0)
  11
+      rack (~> 1.2.1)
  12
+      rack-cache (~> 1.0.0)
  13
+      rack-mount (~> 0.6.13)
  14
+      rack-test (~> 0.5.7)
  15
+      tzinfo (~> 0.3.23)
  16
+    activemodel (3.1.0.beta)
  17
+      activesupport (= 3.1.0.beta)
  18
+      bcrypt-ruby (~> 2.1.4)
  19
+      builder (~> 3.0.0)
  20
+      i18n (~> 0.5.0)
  21
+    activesupport (3.1.0.beta)
  22
+    railties (3.1.0.beta)
  23
+      actionpack (= 3.1.0.beta)
  24
+      activesupport (= 3.1.0.beta)
  25
+      rake (>= 0.8.7)
  26
+      thor (~> 0.14.4)
  27
+
  28
+GEM
  29
+  remote: http://rubygems.org/
  30
+  specs:
  31
+    ZenTest (4.5.0)
  32
+    abstract (1.0.0)
  33
+    autotest (4.4.6)
  34
+      ZenTest (>= 4.4.1)
  35
+    bcrypt-ruby (2.1.4)
  36
+    builder (3.0.0)
  37
+    diff-lcs (1.1.2)
  38
+    erubis (2.6.6)
  39
+      abstract (>= 1.0.0)
  40
+    git (1.2.5)
  41
+    i18n (0.5.0)
  42
+    jeweler (1.5.2)
  43
+      bundler (~> 1.0.0)
  44
+      git (>= 1.2.5)
  45
+      rake
  46
+    rack (1.2.2)
  47
+    rack-cache (1.0)
  48
+      rack (>= 0.4)
  49
+    rack-mount (0.6.13)
  50
+      rack (>= 1.0.0)
  51
+    rack-test (0.5.7)
  52
+      rack (>= 1.0)
  53
+    rake (0.8.7)
  54
+    rcov (0.9.9)
  55
+    rspec (2.5.0)
  56
+      rspec-core (~> 2.5.0)
  57
+      rspec-expectations (~> 2.5.0)
  58
+      rspec-mocks (~> 2.5.0)
  59
+    rspec-core (2.5.1)
  60
+    rspec-expectations (2.5.0)
  61
+      diff-lcs (~> 1.1.2)
  62
+    rspec-mocks (2.5.0)
  63
+    sequel (3.11.0)
  64
+    thor (0.14.6)
  65
+    tzinfo (0.3.25)
  66
+    yard (0.6.5)
  67
+
  68
+PLATFORMS
  69
+  ruby
  70
+
  71
+DEPENDENCIES
  72
+  actionpack (~> 3.1.0.beta)!
  73
+  activesupport (~> 3.1.0.beta)!
  74
+  autotest
  75
+  jeweler (~> 1.4)
  76
+  railties (~> 3.1.0.beta)!
  77
+  rake (~> 0.8.7)
  78
+  rcov
  79
+  rspec
  80
+  sequel (~> 3.11.0)
  81
+  yard (~> 0.5)
45  lib/sequel-rails/railties/database.rake
@@ -10,12 +10,12 @@ namespace :db do
10 10
       end
11 11
       Rake::Task["db:schema:dump"].reenable
12 12
     end
13  
-    
  13
+
14 14
     desc "Load a schema.rb file into the database"
15 15
     task :load, :needs => :environment do
16 16
       require 'sequel-rails/storage'
17 17
       Rails::Sequel::Storage.new(Rails.env).create
18  
-      
  18
+
19 19
       file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
20 20
       if File.exists?(file)
21 21
         load(file)
@@ -36,15 +36,15 @@ namespace :db do
36 36
   desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
37 37
   task :create, :env, :needs => :environment do |t, args|
38 38
     args.with_defaults(:env => Rails.env)
39  
-    
  39
+
40 40
     require 'sequel-rails/storage'
41 41
     Rails::Sequel::Storage.new(args.env).create
42  
-    
  42
+
43 43
     if Rails.env.development? && Rails.configuration.database_configuration['test']
44 44
       Rails::Sequel::Storage.new('test').create
45 45
     end
46 46
   end
47  
-  
  47
+
48 48
   namespace :drop do
49 49
     desc 'Drops all the local databases defined in config/database.yml'
50 50
     task :all, :needs => :environment do
@@ -52,14 +52,14 @@ namespace :db do
52 52
       Rails::Sequel::Storage.drop_all
53 53
     end
54 54
   end
55  
-  
  55
+
56 56
   desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
57 57
   task :drop, :env, :needs => :environment do |t, args|
58 58
     args.with_defaults(:env => Rails.env)
59  
-    
  59
+
60 60
     require 'sequel-rails/storage'
61 61
     Rails::Sequel::Storage.new(args.env).drop
62  
-    
  62
+
63 63
     if Rails.env.development? && Rails.configuration.database_configuration['test']
64 64
       Rails::Sequel::Storage.new('test').drop
65 65
     end
@@ -101,7 +101,7 @@ namespace :db do
101 101
       Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
102 102
     end
103 103
   end
104  
-  
  104
+
105 105
   desc 'Migrate the database to the latest version'
106 106
   task :migrate => :'migrate:load' do
107 107
     Rails::Sequel::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
@@ -121,22 +121,41 @@ namespace :db do
121 121
     Sequel::Migrator.forward('db/migrate/', step)
122 122
     Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
123 123
   end
124  
-  
  124
+
125 125
   desc 'Load the seed data from db/seeds.rb'
126 126
   task :seed => :environment do
127 127
     seed_file = File.join(Rails.root, 'db', 'seeds.rb')
128 128
     load(seed_file) if File.exist?(seed_file)
129 129
   end
130  
-  
  130
+
131 131
   desc 'Create the database, load the schema, and initialize with the seed data'
132 132
   task :setup => [ 'db:create', 'db:migrate', 'db:seed' ]
133  
-  
  133
+
134 134
   desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
135 135
   task :reset => [ 'db:drop', 'db:setup' ]
136  
-  
  136
+
  137
+  desc 'Forcibly close any open connections to the test database'
  138
+  task :force_close_open_connections => :environment do
  139
+    if Rails.env.test?
  140
+       db_config = Rails.configuration.database_configuration[Rails.env].symbolize_keys
  141
+       begin
  142
+         #Will only work on Postgres > 8.4
  143
+         Sequel::Model.db.execute <<-SQL.gsub(/^\s{9}/,'')
  144
+         SELECT COUNT(pg_terminate_backend(procpid))
  145
+         FROM  pg_stat_activity
  146
+         WHERE datname = '#{db_config[:database]}';
  147
+         SQL
  148
+       rescue => e
  149
+         #Will raise an error as it kills existing process running this command
  150
+         #Seems to be only way to ensure *all* test connections are closed
  151
+       end
  152
+     end
  153
+  end
  154
+
137 155
   namespace :test do
138 156
     task :prepare do
139 157
       Rails.env = 'test'
  158
+      Rake::Task['db:force_close_open_connections'].invoke()
140 159
       Rake::Task['db:reset'].invoke()
141 160
       Sequel::DATABASES.each do |db|
142 161
         db.disconnect
81  lib/sequel-rails/storage.rb
@@ -26,7 +26,7 @@ def self.drop_environment(config)
26 26
 
27 27
       def self.new(config)
28 28
         config = Rails::Sequel.configuration.environments[config.to_s] unless config.kind_of?(Hash)
29  
-        
  29
+
30 30
         klass = lookup_class(config['adapter'])
31 31
         if klass.equal?(self)
32 32
           super(config)
@@ -86,6 +86,18 @@ def password
86 86
         @password ||= config['password'] || ''
87 87
       end
88 88
 
  89
+      def host
  90
+        @host ||= config['host'] || ''
  91
+      end
  92
+
  93
+      def port
  94
+        @port ||= config['port'] || ''
  95
+      end
  96
+
  97
+      def owner
  98
+        @owner ||= config['owner'] || ''
  99
+      end
  100
+
89 101
       def charset
90 102
         @charset ||= config['charset'] || ENV['CHARSET'] || 'utf8'
91 103
       end
@@ -125,13 +137,14 @@ def _drop
125 137
       private
126 138
 
127 139
         def execute(statement)
128  
-          system(
129  
-            'mysql',
130  
-            (username.blank? ? '' : "--user=#{username}"),
131  
-            (password.blank? ? '' : "--password=#{password}"),
132  
-            '-e',
133  
-            statement
134  
-          )
  140
+          commands = 'mysql '
  141
+          commands << "--user=#{username} "     unless username.blank?
  142
+          commands << "--password=#{password} " unless password.blank?
  143
+          commands << "--host=#{host} "         unless host.blank?
  144
+          commands << '-e '
  145
+          commands << statement
  146
+
  147
+          system(commands)
135 148
         end
136 149
 
137 150
         def collation
@@ -141,45 +154,47 @@ def collation
141 154
       end
142 155
 
143 156
       class Postgres < Storage
  157
+
144 158
         def _create
145  
-          system(
146  
-            'createdb',
147  
-            '-E',
148  
-            charset,
149  
-            '-U',
150  
-            username,
151  
-            database
152  
-          )
  159
+          commands = "createdb --encoding=#{charset} "
  160
+          commands << "--username=#{username} " unless username.blank?
  161
+          commands << "--owner=#{owner} "       unless owner.blank?
  162
+          commands << "--port=#{port} "         unless port.blank?
  163
+          commands << "--host=#{host} "         unless host.blank?
  164
+          commands << database
  165
+
  166
+          system(commands)
153 167
         end
154 168
 
155 169
         def _drop
156  
-          system(
157  
-            'dropdb',
158  
-            '-U',
159  
-            username,
160  
-            database
161  
-          )
  170
+          commands = "dropdb "
  171
+          commands << "--username=#{username} " unless username.blank?
  172
+          commands << "--port=#{port} "         unless port.blank?
  173
+          commands << "--host=#{host} "         unless host.blank?
  174
+          commands << database
  175
+
  176
+          system(commands)
162 177
         end
163 178
       end
164  
-      
  179
+
165 180
       class Jdbc < Storage
166  
-        
  181
+
167 182
         def _is_mysql?
168 183
           database.match(/^jdbc:mysql/)
169 184
         end
170  
-        
  185
+
171 186
         def _root_url
172 187
           database.scan /^jdbc:mysql:\/\/\w*:?\d*/
173 188
         end
174  
-        
  189
+
175 190
         def db_name
176 191
           database.scan(/^jdbc:mysql:\/\/\w+:?\d*\/(\w+)/).flatten.first
177 192
         end
178  
-        
  193
+
179 194
         def _params
180 195
           database.scan /\?.*$/
181 196
         end
182  
-        
  197
+
183 198
         def _create
184 199
           if _is_mysql?
185 200
             ::Sequel.connect("#{_root_url}#{_params}") do |db|
@@ -195,16 +210,16 @@ def _drop
195 210
             end
196 211
           end
197 212
         end
198  
-        
  213
+
199 214
         private
200  
-        
  215
+
201 216
         def collation
202 217
           @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
203 218
         end
204  
-        
205  
-        
  219
+
  220
+
206 221
       end
207  
-      
  222
+
208 223
     end
209 224
   end
210 225
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.