Last active
December 31, 2016 09:54
-
-
Save robshep/4192f698f323d3b222f6623f403a9b65 to your computer and use it in GitHub Desktop.
Use dockered databases for rails development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is free and unencumbered software released into the public domain. | |
# | |
# Anyone is free to copy, modify, publish, use, compile, sell, or | |
# distribute this software, either in source code form or as a compiled | |
# binary, for any purpose, commercial or non-commercial, and by any | |
# means. | |
# | |
# In jurisdictions that recognize copyright laws, the author or authors | |
# of this software dedicate any and all copyright interest in the | |
# software to the public domain. We make this dedication for the benefit | |
# of the public at large and to the detriment of our heirs and | |
# successors. We intend this dedication to be an overt act of | |
# relinquishment in perpetuity of all present and future rights to this | |
# software under copyright law. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
# OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# For more information, please refer to <http://unlicense.org> | |
# lives in config/initializers/docker_datasource.rb | |
# requires: | |
# gem 'docker-api', '1.32.1' | |
# | |
# gem 'docker-api', '1.32.1' | |
require 'docker' | |
# set this to something other than 'postgres' | |
DB_NAME='myProject' | |
def try_psql(container) | |
ret = container.exec(["psql", "-U", "postgres", "-c", "SELECT 1"], wait: 10, tty: true) | |
raise 'No PSQL SELECT 1' unless ret[2] == 0 | |
end | |
def retry_psql(container) | |
tries ||= 10 | |
print "." | |
try_psql(container) | |
rescue Exception => e | |
sleep 1 | |
retry unless (tries -= 1).zero? | |
else | |
puts " success (attempt #{11-tries}/10)" | |
# just hold off for a couple of seconds as the docker init process brings the database up and down a few times at startup. | |
sleep 2 | |
end | |
if( ENV['DOCKER_DB'] ) | |
container = Docker::Container.create( 'Image' => 'postgres:latest', 'ExposedPorts' => { '5432/tcp' => {} }, | |
'HostConfig' => { | |
'PortBindings' => { | |
'5432/tcp' => [{}] | |
} | |
}, 'Env' => [ 'POSTGRES_DB=' + DB_NAME ] ) | |
container.start | |
at_exit do | |
puts "destroying docker container: #{container.id}" | |
container.delete(:force => true) | |
end | |
puts "started docker container: #{container.id}" | |
print "waiting for database: " | |
retry_psql container | |
json = container.json | |
pg_port = json['NetworkSettings']['Ports']["5432/tcp"][0]["HostPort"] | |
ENV['DATABASE_URL'] = "postgresql://postgres@localhost:#{pg_port}/" + DB_NAME + "?pool=5" | |
puts "db:migrate" | |
if(true || Rails.env != "test") | |
migrations_path = Rails.root.join("db/migrate") | |
ActiveRecord::Migrator.migrate(migrations_path) | |
migrations = ActiveRecord::Migrator.migrations(migrations_path) | |
migrator = ActiveRecord::Migrator.new(:up, migrations) | |
puts "HERE" | |
puts migrator.pending_migrations | |
puts "THERE" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment