tasks/schema_comments.rake in schema_comments-0.2.0.alpha2 vs tasks/schema_comments.rake in schema_comments-0.2.0.alpha3
- old
+ new
@@ -1,81 +1,45 @@
# -*- coding: utf-8 -*-
require 'yaml'
-require 'yaml_waml'
+# require 'yaml_waml'
require 'active_record'
+require 'schema_comments'
+require 'schema_comments/schema_comment'
-# テストを実行する際はschema_commentsのschema_comments.ymlへの出力を抑制します。
-namespace :db do
- Rake.application.send(:eval, "@tasks.delete('db:migrate')")
- desc "Migrate the database through scripts in db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
- task :migrate => :environment do
- SchemaComments::SchemaComment.yaml_access do
- ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
- ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
- SchemaComments.quiet = true
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
- end
- end
+db_namespace = namespace :db do
+ namespace :schema do
- Rake.application.send(:eval, "@tasks.delete('db:rollback')")
- desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
- task :rollback => :environment do
- SchemaComments::SchemaComment.yaml_access do
- step = ENV['STEP'] ? ENV['STEP'].to_i : 1
- ActiveRecord::Migrator.rollback('db/migrate/', step)
- SchemaComments.quiet = true
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
- end
- end
-
- namespace :migrate do
- desc 'Runs the "up" for a given migration VERSION.'
- task :up => :environment do
- SchemaComments::SchemaComment.yaml_access do
- version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
- raise "VERSION is required" unless version
- ActiveRecord::Migrator.run(:up, "db/migrate/", version)
- SchemaComments.quiet = true
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
- end
- end
-
- desc 'Runs the "down" for a given migration VERSION.'
- task :down => :environment do
- SchemaComments::SchemaComment.yaml_access do
- version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
- raise "VERSION is required" unless version
- ActiveRecord::Migrator.run(:down, "db/migrate/", version)
- SchemaComments.quiet = true
- Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
- end
- end
- end
-
- namespace :test do
- Rake.application.send(:eval, "@tasks.delete('db:test:prepare')")
- desc 'Check for pending migrations and load the test schema'
- task :prepare => 'db:abort_if_pending_migrations' do
- SchemaComments::SchemaComment.yaml_access do
- SchemaComments.quiet = true
- if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
- Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:load"
- }[ActiveRecord::Base.schema_format]].invoke
+ Rake.application.send(:eval, "@tasks.delete('db:schema:dump')")
+ desc 'Create a db/schema.rb file that can be portably used against any DB supported by AR'
+ task :dump => [:environment, :load_config] do
+ begin
+ SchemaComments.setup
+ require 'active_record/schema_dumper'
+ filename = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
+ File.open(filename, "w:utf-8") do |file|
+ ActiveRecord::Base.establish_connection(Rails.env)
+ # ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
+ SchemaComments::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
+ db_namespace['schema:dump'].reenable
+ rescue Exception => e
+ puts "[#{e.class}] #{e.message}:\n " << e.backtrace.join("\n ")
+ raise e
end
end
- end
+ end
end
-
-
class ActiveRecord::Base
class << self
attr_accessor :ignore_pattern_to_export_i18n
- self.ignore_pattern_to_export_i18n = /\(\(\(.*\)\)\)/
+ end
+ self.ignore_pattern_to_export_i18n = /\(\(\(.*\)\)\)/
+
+ class << self
def export_i18n_models
subclasses = ActiveRecord::Base.send(:subclasses).select do |klass|
(klass != SchemaComments::SchemaComment) and
klass.respond_to?(:table_exists?) and klass.table_exists?
end
@@ -157,11 +121,11 @@
end
namespace :i18n do
namespace :schema_comments do
task :load_all_models => :environment do
- Dir.glob(File.join(RAILS_ROOT, 'app', 'models', '**', '*.rb')) do |file_name|
+ Dir.glob(Rails.root.join('app/models/**/*.rb')) do |file_name|
require file_name
end
end
desc "Export i18n model resources from schema_comments. you can set locale with environment variable LOCALE"
@@ -180,10 +144,10 @@
desc "update i18n YAML. you can set locale with environment variable LOCALE"
task :update_config_locale => :"i18n:schema_comments:load_all_models" do
require 'yaml/store'
locale = (ENV['LOCALE'] || I18n.locale).to_s
- path = (ENV['YAML_PATH'] || File.join(RAILS_ROOT, "config/locales/#{locale}.yml"))
+ path = (ENV['YAML_PATH'] || Rails.root.join("config/locales/#{locale}.yml"))
print "updating #{path}..."
begin
db = YAML::Store.new(path)
db.transaction do