Created
          December 10, 2015 21:54 
        
      - 
      
- 
        Save elado/db6fa54f901fb805e7bc to your computer and use it in GitHub Desktop. 
    Sequel Rake Tasks
  
        
  
    
      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
    
  
  
    
  | require 'sequel' | |
| require 'yaml' | |
| require 'erb' | |
| Sequel.extension :migration | |
| Sequel.extension :pg_array_ops, :pg_json_ops, :pg_hstore_ops | |
| SEQUEL_MIGRATIONS_PATH = './db/migrate' | |
| def database_name | |
| @database_name ||= URI.parse(Nenv.database_url).path.sub(%r[^/], "") | |
| end | |
| def db | |
| @db ||= begin | |
| db = Sequel.connect(Nenv.database_url) | |
| db.extension :pg_hstore, :pg_array, :pg_json | |
| end | |
| end | |
| namespace :db do | |
| desc "Create DB" | |
| task :create do |_, args| | |
| db.execute "CREATE DATABASE #{database_name}" | |
| puts "DB #{database_name} created" | |
| end | |
| desc "Drop DB" | |
| task :drop do |_, args| | |
| db.execute "DROP DATABASE #{database_name}" | |
| puts "DB #{database_name} dropped" | |
| end | |
| desc "Migrate DB" | |
| task :migrate do |_, args| | |
| before = db.from(:schema_migrations).all rescue [] | |
| Sequel::Migrator.run(db, SEQUEL_MIGRATIONS_PATH) | |
| after = db.from(:schema_migrations).all | |
| puts "Migrated: #{(after - before).collect{|x| x[:filename]}.join(", ")}" | |
| end | |
| desc "Rollback Migration" | |
| task :rollback do |_, args| | |
| all_migrations_files = db.from(:schema_migrations).all | |
| previous_to_last_migration = if all_migrations_files.length == 1 | |
| 0 | |
| else | |
| filename = all_migrations_files[-2][:filename] | |
| filename[/^\d+/].to_i | |
| end | |
| puts "rollback to #{previous_to_last_migration}" | |
| Sequel::Migrator.run(db, SEQUEL_MIGRATIONS_PATH, target: previous_to_last_migration) | |
| end | |
| desc "Generate migration" | |
| task :migration, [:name, :updown] do |_, args| | |
| next unless args[:name] | |
| content = "# migration #{args[:name]}\n\n" | |
| content << if args[:updown] | |
| "Sequel.migration do\n up do\n \n end\n\n down do\n \n end\nend\n" | |
| else | |
| "Sequel.migration do\n change do\n \n end\nend\n" | |
| end | |
| timestamp = Time.now.strftime("%Y%m%d%H%M%S") | |
| filename = File.join(SEQUEL_MIGRATIONS_PATH, "#{timestamp}_#{args[:name]}.rb") | |
| File.open(filename, 'w') do |f| | |
| f.puts content | |
| end | |
| puts "Created Sequel migration #{filename}" | |
| end | |
| desc "Erase DB" | |
| task :erase do |_, args| | |
| db.tables.each { |table| db.drop_table table } | |
| puts "DB erased." | |
| end | |
| desc "Reset DB" | |
| task :reset do |_, args| | |
| Rake::Task["db:erase"].invoke | |
| Rake::Task["db:migrate"].invoke | |
| puts "DB reset." | |
| end | |
| namespace :schema do | |
| desc "Schema Dump" | |
| task :dump do | |
| db.extension :schema_dumper | |
| schema = db.dump_schema_migration | |
| open(ROOT + "/db/schema.rb", 'w') { |f| f << schema } | |
| end | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment