├── VERSION ├── lib ├── database-tasks.rb └── tasks │ └── db.rake ├── Gemfile ├── .document ├── Gemfile.lock ├── README.rdoc ├── .gitignore ├── LICENSE.txt ├── database-tasks.gemspec └── Rakefile /VERSION: -------------------------------------------------------------------------------- 1 | 0.1.1 2 | -------------------------------------------------------------------------------- /lib/database-tasks.rb: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /.document: -------------------------------------------------------------------------------- 1 | lib/**/*.rb 2 | bin/* 3 | - 4 | features/**/*.feature 5 | LICENSE.txt 6 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | database-tasks (0.1.0) 5 | sequel (~> 5) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | sequel (5.38.0) 11 | 12 | PLATFORMS 13 | ruby 14 | 15 | DEPENDENCIES 16 | database-tasks! 17 | 18 | BUNDLED WITH 19 | 2.1.4 20 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = database-tasks 2 | 3 | Description goes here. 4 | 5 | == Contributing to database-tasks 6 | 7 | * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. 8 | * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. 9 | * Fork the project. 10 | * Start a feature/bugfix branch. 11 | * Commit and push until you are happy with your contribution. 12 | * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. 13 | * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it. 14 | 15 | == Copyright 16 | 17 | Copyright (c) 2015 Sam Reh. See LICENSE.txt for 18 | further details. 19 | 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # rcov generated 2 | coverage 3 | coverage.data 4 | 5 | # rdoc generated 6 | rdoc 7 | 8 | # yard generated 9 | doc 10 | .yardoc 11 | 12 | # bundler 13 | .bundle 14 | 15 | # jeweler generated 16 | pkg 17 | 18 | # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore: 19 | # 20 | # * Create a file at ~/.gitignore 21 | # * Include files you want ignored 22 | # * Run: git config --global core.excludesfile ~/.gitignore 23 | # 24 | # After doing this, these files will be ignored in all your git projects, 25 | # saving you from having to 'pollute' every project you touch with them 26 | # 27 | # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line) 28 | # 29 | # For MacOS: 30 | # 31 | #.DS_Store 32 | 33 | # For TextMate 34 | #*.tmproj 35 | #tmtags 36 | 37 | # For emacs: 38 | #*~ 39 | #\#* 40 | #.\#* 41 | 42 | # For vim: 43 | #*.swp 44 | 45 | # For redcar: 46 | #.redcar 47 | 48 | # For rubinius: 49 | #*.rbc 50 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Sam Reh 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /database-tasks.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by jeweler 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | # stub: database-tasks 0.1.0 ruby lib 6 | 7 | Gem::Specification.new do |s| 8 | s.name = "database-tasks" 9 | s.version = "0.1.1" 10 | 11 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 12 | s.require_paths = ["lib"] 13 | s.authors = ["Sam Reh"] 14 | s.date = "2015-05-20" 15 | s.description = "Rake tasks for Sequel ORM including dropping, creating and migrating" 16 | s.email = "samuelreh@gmail.com" 17 | s.extra_rdoc_files = [ 18 | "LICENSE.txt", 19 | "README.rdoc" 20 | ] 21 | s.files = [ 22 | ".document", 23 | "Gemfile", 24 | "Gemfile.lock", 25 | "LICENSE.txt", 26 | "README.rdoc", 27 | "Rakefile", 28 | "VERSION", 29 | "lib/database-tasks.rb", 30 | "lib/tasks/db.rake" 31 | ] 32 | s.homepage = "http://github.com/samuelreh/database-tasks" 33 | s.licenses = ["MIT"] 34 | s.rubygems_version = "2.4.5" 35 | s.summary = "Rake tasks for Sequel ORM" 36 | 37 | s.add_dependency 'sequel', '~> 5' 38 | end 39 | 40 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | require 'rubygems' 4 | require 'bundler' 5 | begin 6 | Bundler.setup(:default, :development) 7 | rescue Bundler::BundlerError => e 8 | $stderr.puts e.message 9 | $stderr.puts "Run `bundle install` to install missing gems" 10 | exit e.status_code 11 | end 12 | require 'rake' 13 | 14 | require 'jeweler' 15 | Jeweler::Tasks.new do |gem| 16 | # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options 17 | gem.name = "database-tasks" 18 | gem.homepage = "http://github.com/samuelreh/database-tasks" 19 | gem.license = "MIT" 20 | gem.summary = %Q{Rake tasks for Sequel ORM} 21 | gem.description = %Q{Rake tasks for Sequel ORM including dropping, creating and migrating} 22 | gem.email = "samuelreh@gmail.com" 23 | gem.authors = ["Sam Reh"] 24 | # dependencies defined in Gemfile 25 | end 26 | Jeweler::RubygemsDotOrgTasks.new 27 | 28 | require 'rake/testtask' 29 | Rake::TestTask.new(:test) do |test| 30 | test.libs << 'lib' << 'test' 31 | test.pattern = 'test/**/test_*.rb' 32 | test.verbose = true 33 | end 34 | 35 | desc "Code coverage detail" 36 | task :simplecov do 37 | ENV['COVERAGE'] = "true" 38 | Rake::Task['test'].execute 39 | end 40 | 41 | task :default => :test 42 | 43 | require 'rdoc/task' 44 | Rake::RDocTask.new do |rdoc| 45 | version = File.exist?('VERSION') ? File.read('VERSION') : "" 46 | 47 | rdoc.rdoc_dir = 'rdoc' 48 | rdoc.title = "database-tasks #{version}" 49 | rdoc.rdoc_files.include('README*') 50 | rdoc.rdoc_files.include('lib/**/*.rb') 51 | end 52 | -------------------------------------------------------------------------------- /lib/tasks/db.rake: -------------------------------------------------------------------------------- 1 | require 'yaml' 2 | 3 | namespace :db do 4 | desc "Run database migrations" 5 | task :migrate, [:allow_missing] => :environment do |_, args| 6 | Sequel.extension :migration 7 | allow_missing = !args[:allow_missing].nil? 8 | Sequel::Migrator.run(DB, "db/migrations", allow_missing_migration_files: allow_missing) 9 | end 10 | 11 | desc "Rollback the database" 12 | task rollback: :environment do 13 | Sequel.extension :migration 14 | target = DB[:schema_migrations].reverse_order(:filename).offset(1).first[:filename] 15 | version = target.match(/^\d+/)[0].to_i 16 | puts "Rolling back to #{target}" 17 | Sequel::Migrator.run(DB, "db/migrations", target: version, allow_missing_migration_files: true) 18 | end 19 | 20 | desc "Drop the database" 21 | task :drop do 22 | `dropdb "#{db_name}"` 23 | end 24 | 25 | desc "Create the database" 26 | task :create do 27 | `createdb "#{db_name}"` 28 | end 29 | 30 | desc "Seed the database" 31 | task seed: :environment do 32 | require './db/seeds' 33 | end 34 | 35 | desc "Dump DB schema to db/schema.rb" 36 | task :schema do 37 | `sequel -d #{db_path} > ./db/schema.rb` 38 | end 39 | 40 | desc "Dump DB structure to db/structure.sql" 41 | task :structure do 42 | `pg_dump --no-privileges --no-owner --schema-only #{db_path} > ./db/structure.sql` 43 | end 44 | 45 | namespace :generate do 46 | desc 'Generate a timestamped, empty Sequel migration.' 47 | task :migration, :name do |_, args| 48 | if args[:name].nil? 49 | puts 'You must specify a migration name (e.g. rake generate:migration[create_events])!' 50 | exit false 51 | end 52 | 53 | content = "Sequel.migration do\n change do\n \n end\nend\n" 54 | timestamp = Time.now.to_i 55 | filename = "#{timestamp}_#{args[:name]}.rb" 56 | 57 | Dir.chdir(File.join('db', 'migrations')) do 58 | File.open(filename, 'w') do |f| 59 | f.puts content 60 | end 61 | end 62 | 63 | puts "Created the migration #{filename}" 64 | end 65 | end 66 | 67 | def db_path 68 | return ENV['DATABASE_URL'] if ENV['DATABASE_URL'] 69 | 70 | yaml = YAML.load_file(File.join('config', 'database.yml')) 71 | yaml[ENV['RACK_ENV'] || 'development'] 72 | end 73 | 74 | def db_name 75 | db_path.split('/')[-1] 76 | end 77 | end 78 | 79 | --------------------------------------------------------------------------------