Permalink
Browse files

updated README.md

  • Loading branch information...
1 parent ad45aa0 commit 161290acb220a00a59e0506e859501c6fab94af0 @mwlang committed Feb 13, 2010
Showing with 214 additions and 2 deletions.
  1. +42 −1 README.md
  2. +1 −1 VERSION
  3. +167 −0 lib/tasks/databases.rake
  4. +3 −0 lib/tasks/sequel.rb
  5. +1 −0 test/rake/Rakefile
View
@@ -8,6 +8,7 @@ in specs and tested and used in production-level deployments already.
### Currently, sequel_plus contains:
* plugin for Trees to mimic the Rails acts_as_tree plugin.
* extension for exporting data using Dataset#export.
+* rake tasks to handle basic migrations and schema inspections (similar to Rails projects)
NOTE: Authors of other plugins and extensions for Sequel are welcome to contact me for inclusion
of your plugin and extension to this project.
@@ -46,9 +47,49 @@ This gem is released to gemcutter. Rubyforge is not utilized.
# Specific rows and columns
File.open("nodes.txt", "w"){|file| DB[:nodes].filter(:id < 5).select(:id, :name).export(file)}
+## Use Rake Tasks
+
+Several rake tasks are made available simply by including the "tasks/sequel" per below:
+
+ require 'sequel'
+ require 'sequel_plus'
+ require 'tasks/sequel'
+
+ task :environment do
+ DB = Sequel.sqlite
+ end
+
+ # Establish DB somewhere in your Rakefile or elsewhere before invoking the DB dependent tasks
+ # If you define in :environment, the sequel tasks will invoke :environment as needed.
+
+ task :environment do
+ DB = Sequel.sqlite
+ end
+
+Example tasks that are available:
+
+* rake db:desc[table] # Displays schema of table
+* rake db:fields[table] # Displays simple list of fields of table in sorted order
+* rake db:migrate # Migrate the database through scripts in db/migrate and update db/schema.rb by in...
+* rake db:migrate:down[step] # Reverts to previous schema version.
+* rake db:migrate:new[table,verb] # Creates a new migrate script.
+* rake db:migrate:redo # Rollbacks the database one migration and re-migrates up.
+* rake db:migrate:up[version] # Runs the "up" for a given migration VERSION.
+* rake db:reset # Drops all tables and recreates the schema from db/schema.rb
+* rake db:rollback # Rolls the schema back to the previous version.
+* rake db:schema:drop # drops the schema, using schema.rb
+* rake db:schema:dump # Dumps the schema to db/schema.db
+* rake db:schema:load # loads the schema from db/schema.rb
+* rake db:schema:version # Returns current schema version
+* rake db:show[table] # Displays content of table or lists all tables
+* rake db:tables # Displays a list of tables in the database
+
+These tasks will expect migrations to be in db/migration that is based off the folder your Rakefile resides in. If you wish to change the location of the "db" folder, simply declare :environment task and set APP_ROOT folder to be something other than the folder the Rakefile is residing in.
+
# Note on Patches/Pull Requests
-* This release adds an export facility to the Sequel::Dataset
+* This release adds rake tasks
+* last release adds an export facility to the Sequel::Dataset
# Copyright
View
@@ -1 +1 @@
-0.1.2
+0.1.3
View
@@ -0,0 +1,167 @@
+namespace :db do
+ task :app_root do
+ APP_ROOT = File.dirname File.expand_path Rake.application.rakefile
+ Rake::Task["environment"].invoke if Rake::Task.task_defined?("environment")
+ end
+
+ task :load_config => [:app_root] do
+ raise "no DB has been defined.\n Assign DB in your Rakefile or declare an :environment task and connect to your database." unless defined? DB
+ Sequel.extension :migration
+ Sequel.extension :schema_dumper
+ Sequel.extension :pretty_table
+ end
+
+ desc "Displays a list of tables in the database"
+ task :tables => :load_config do
+ puts "No tables in this database" if DB.tables.empty?
+ DB.tables.each_with_index do |table_name, i|
+ puts "#{'%3d' % (i+1)}: #{table_name}"
+ end
+ end
+
+ desc "Displays content of table or lists all tables"
+ task :show, [:table] => :load_config do |t, args|
+ args.table ? DB[args.table.to_sym].print : Rake::Task["db:tables"].invoke
+ end
+
+ desc "Displays simple list of fields of table in sorted order"
+ task :fields, [:table] => :load_config do |t, args|
+ raise "no table name given" unless args[:table]
+
+ puts '==[' << args.table << ']' << '=' * (80 - args.table.size - 4)
+ DB.schema(args.table.to_sym).sort{|a, b| a[0].to_s <=> b[0].to_s}.each{|col| puts col[0]}
+ puts '=' * 80
+ end
+
+ desc "Displays schema of table"
+ task :desc, [:table] => :load_config do |t, args|
+ def o(value, size = 25)
+ "%#{-1*size}s" % value.to_s
+ end
+ unless args[:table]
+ Rake::Task["db:list_tables"].invoke
+ else
+ puts '==[' << args.table << ']' << '=' * (80 - args.table.size - 4)
+ DB.schema(args.table.to_sym).each_with_index do |col, i|
+ name, info = col
+ puts "#{o i+1, -3}: #{o name}:#{o info[:type], 15}:#{o info[:db_type], 15}:#{' not null ' unless info[:allow_null]} #{' pk ' if info[:primary_key]} #{' default: ' << info[:default].to_s if info[:default]}"
+ end
+ puts '-' * 80
+ indexes = DB.indexes(args.table.to_sym)
+ if indexes.size == 0
+ puts " No indexes defined"
+ else
+ indexes.each_with_index do |idx, i|
+ name, attrs = idx
+ puts ' ' << o(name, 28) << ": unique? " << o(attrs[:unique] ? 'yes' : 'no', 6) << ': ' << attrs[:columns].join(', ')
+ end
+ end
+ puts '=' * 80
+ end
+ end
+
+ namespace :schema do
+ task :ensure_db_dir do
+ FileUtils.mkdir_p File.join(APP_ROOT, 'db', 'migrate')
+ end
+
+ desc "Dumps the schema to db/schema.db"
+ task :dump => [:load_config, :ensure_db_dir] do
+ schema = DB.dump_schema_migration
+ schema_file = File.open(File.join(APP_ROOT, 'db', 'schema.rb'), "w"){|f| f.write(schema)}
+ end
+
+ desc "drops the schema, using schema.rb"
+ task :drop => [:load_config, :dump] do
+ eval(File.read(File.join(APP_ROOT, 'db', 'schema.rb'))).apply(DB, :down)
+ end
+
+ desc "loads the schema from db/schema.rb"
+ task :load => :load_config do
+ eval(File.read(File.join(APP_ROOT, 'db', 'schema.rb'))).apply(DB, :up)
+ latest_version = Sequel::Migrator.latest_migration_version(File.join(APP_ROOT, 'db', 'migrate'))
+ Sequel::Migrator.set_current_migration_version(DB, latest_version)
+ puts "Database schema loaded version #{latest_version}"
+ end
+
+ desc "Returns current schema version"
+ task :version => :load_config do
+ puts "Current Schema Version: #{Sequel::Migrator.get_current_migration_version(DB)}"
+ end
+ end
+
+ desc "Migrate the database through scripts in db/migrate and update db/schema.rb by invoking db:schema:dump."
+ task :migrate => :load_config do
+ Sequel::Migrator.apply(DB, File.join(APP_ROOT, 'db', 'migrate'))
+ Rake::Task["db:schema:dump"].invoke
+ Rake::Task["db:schema:version"].invoke
+ end
+
+ namespace :migrate do
+ desc 'Rollbacks the database one migration and re-migrates up.'
+ task :redo => :load_config do
+ Rake::Task["db:rollback"].invoke
+ Rake::Task["db:migrate"].invoke
+ Rake::Task["db:schema:dump"].invoke
+ end
+
+ desc 'Runs the "up" for a given migration VERSION.'
+ task :up, [:version] => :load_config do |t, args|
+ raise "VERSION is required" unless args[:version]
+
+ puts "migrating up to version #{args.version}"
+ Sequel::Migrator.apply(DB, File.join(APP_ROOT, 'db', 'migrate'), args.version)
+ Rake::Task["db:schema:dump"].invoke
+ end
+
+ desc 'Reverts to previous schema version. Specify the number of steps with STEP=n'
+ task :down, [:step] => :load_config do |t, args|
+ step = args[:step] ? args.step.to_i : 1
+ current_version = Sequel::Migrator.get_current_migration_version(DB)
+ down_version = current_version - step
+ down_version = 0 if down_version < 0
+
+ puts "migrating down to version #{down_version}"
+ Sequel::Migrator.apply(DB, File.join(APP_ROOT, 'db', 'migrate'), down_version)
+ Rake::Task["db:schema:dump"].invoke
+ end
+
+ desc "Creates a new migrate script. The verb is optional."
+ task :new, [:table, :verb] => :load_config do |t, args|
+ unless args[:table]
+ puts "need to provide a table name: rake db:migrate:new[new_table]"
+ else
+ table = args.table
+ verb = args.verb || 'create'
+ migrate_path = File.join(APP_ROOT,'db', 'migrate')
+ begin
+ last_file = File.basename(Dir.glob(File.join(migrate_path, '*.rb')).sort.last)
+ next_value = last_file.scan(/\d+/).first.to_i + 1
+ rescue
+ next_value = 1
+ end
+ filename = '%03d' % next_value << "_" << args.table << '.rb'
+ File.open(File.join(migrate_path, filename), 'w') do |file|
+ file.puts "class #{verb.capitalize}#{table.capitalize} < Sequel::Migration\n"
+ file.puts "\tdef up"
+ file.puts "\t\t#{verb}_table :#{table} do"
+ file.puts "\t\t\tprimary_key\t:id"
+ file.puts "\t\tend"
+ file.puts "\tend\n\n"
+ file.puts "\tdef down\n"
+ file.puts "\t\tdrop_table :#{table}"
+ file.puts "\tend"
+ file.puts "end"
+ end
+ end
+ end
+ end
+
+ desc 'Rolls the schema back to the previous version.'
+ task :rollback => :load_config do
+ Rake::Task["db:migrate:down"].invoke
+ end
+
+ desc 'Drops all tables and recreates the schema from db/schema.rb'
+ task :reset => ['db:schema:drop', 'db:schema:load']
+end
View
@@ -0,0 +1,3 @@
+# Load rakefile extensions
+Dir["#{File.dirname(__FILE__)}/*.rake"].each { |ext| load ext }
+
View
@@ -3,6 +3,7 @@ require 'sequel_plus'
require 'tasks/sequel'
task :environment do
+ puts "dummy environment"
DB = Sequel.sqlite
DB.create_table :nodes do

0 comments on commit 161290a

Please sign in to comment.