Created
May 18, 2012 18:50
-
-
Save dallasmarlow/2727016 to your computer and use it in GitHub Desktop.
quick example of sequel and time ranges
This file contains 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
require 'sequel' | |
config = { | |
:database => { | |
:adapter => 'mysql2', | |
:database => 'mysql_backup_reports', | |
:host => '1.2.3.4', | |
:user => 'backup', | |
:password => 'pants' | |
}, | |
} | |
db = Sequel.connect config[:database] | |
db.tables | |
=> [:failures, :reports, :schema_migrations] | |
db[:failures] | |
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `failures`"> | |
db[:failures].limit 1 | |
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `failures` LIMIT 1"> | |
# match on values | |
db[:failures].where :pool => 'blogs-15847500-16999999' | |
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `failures` WHERE (`pool` = 'blogs-15847500-16999999')"> | |
# use Time and Date objects natively | |
db[:failures].where :checkpoint => Date.today | |
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `failures` WHERE (`checkpoint` = '2012-05-18')"> | |
# date ranges | |
db[:failures].where :checkpoint => ((Date.today - 1)..Date.today) | |
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `failures` WHERE ((`checkpoint` >= '2012-05-17') AND (`checkpoint` <= '2012-05-18'))"> | |
# sample results | |
db[:failures].limit(1).all | |
=> [{:checkpoint=>2012-05-09 21:17:36 -0400, :pool=>"blogs-15847500-16999999", :host=>"db-7d13xxx", :error=>"E, [2012-05-09T21:17:31.605107 #24186] ERROR -- : upload to s3 failed"}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment