Rake task to clear Rails log files
# Truncates all *.log files in log/ to zero bytes rake log:clear
12303 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
# Truncates all *.log files in log/ to zero bytes rake log:clear
class ActiveRecord::ConnectionAdapters::MysqlAdapter def native_database_types #:nodoc: { :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", :int64_pk => "bigint DEFAULT NULL auto_increment PRIMARY KEY", :int64 => { :name => "bigint" }, :string => { :name => "varchar", :limit => 255 }, :text => { :name => "text" }, :integer => { :name => "int", :limit => 11 }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "datetime" }, :timestamp => { :name => "datetime" }, :time => { :name => "time" }, :date => { :name => "date" }, :binary => { :name => "blob" }, :boolean => { :name => "tinyint", :limit => 1 }, } end end
create_table :slices, :id => false do |t| t.column :cheese_id, :integer t.column :plant, :string t.column :date, :datetime end add_column :events, :id, :int64_pk end
create_table :fondues do |t| t.column :cheese_id, :int64 t.column :party_id, :integer t.column :kirsch_id, :integer end
return INT2NUM(mysql_insert_id(GetHandler(obj)));
return LL2NUM(mysql_insert_id(GetHandler(obj)));
# Use SQL instead of Active Record's schema dumper when creating the test database. # This is necessary if your schema can't be completely dumped by the schema dumper, # like if you have constraints or database-specific column types config.active_record.schema_format = :sql
class ActiveRecord::ConnectionAdapters::MysqlAdapter def native_database_types #:nodoc: { :primary_key => "bigint DEFAULT NULL auto_increment PRIMARY KEY", . . .
namespace :user do desc 'Reset user (USER=username) password to that of username "tester" (or FROM=username env)' task :reset => :environment do reset_username = ENV['USER'] unless ENV['USER'] puts 'Require "USER=username" for user to be reset' next end reset_user = User.find_by_username reset_username unless reset_user puts "Cannot find user: #{reset_username}" next end from_username = ENV['FROM'] || 'tester' from_user = User.find_by_username from_username unless from_user puts "Cannot find user: #{from_username}" next end reset_user.crypted_password = from_user.crypted_password reset_user.salt = from_user.salt reset_user .save puts 'User password reset' end end
desc "Remove captcha images" task :remove_captcha_files do captcha_img_path = "#{RAILS_ROOT}/public/images/captcha_img/" Dir.foreach(captcha_img_path){|file| File.delete(captcha_img_path+file) if (/^.*.jpg$/).match(file)} if File.exist?(captcha_img_path) end
if ENV["SERVER"] && ENV["SERVER"] == "production" set :primary_server, "production.com" set :user, "ben" elsif ENV["SERVER"] && ENV["SERVER"] == "staging" set :primary_server, "staging.local" set :user, "ben" else ... end role :web, primary_server role :app, primary_server role :db, primary_server, :primary => true
namespace :db do task :fix_clobs => [:environment] do |t| @outfile = File.expand_path(File.join(RAILS_ROOT,"db","fix_clobs.sql")) File.open(@outfile, "w") do |file| ActiveRecord::Base.connection.tables.each do |table_name| begin model = eval(table_name.classify) model.columns.each do |column| if column.sql_type == "CLOB" file.write("ALTER TABLE #{table_name} ADD #{column.name}_temp VARCHAR2(4000);\n") file.write("UPDATE #{table_name} SET #{column.name}_temp = #{column.name};\n") file.write("COMMIT;\n") file.write("ALTER TABLE #{table_name} DROP COLUMN #{column.name};\n") file.write("ALTER TABLE #{table_name} RENAME COLUMN #{column.name}_temp TO #{column.name};\n") file.write("\n") end end rescue => ex puts "Failed for #{table_name} with #{ex.class}" end end end end end
namespace :db do task :adjust_sequences => :environment do ActiveRecord::Base.connection.tables.each do |table| begin count = ActiveRecord::Base.count_by_sql("SELECT MAX(id) FROM #{table}") seq = "#{table}_seq" ActiveRecord::Base.connection.execute("DROP SEQUENCE #{seq}") ActiveRecord::Base.connection.execute("CREATE SEQUENCE #{seq} START WITH #{count+1}") rescue => ex puts "Failed for #{table} with #{ex.class}" end end end end
desc "Configure Subversion for Rails" task :configure_for_svn do system "svn remove log/*" system "svn commit -m 'removing all log files from subversion'" system 'svn propset svn:ignore "*.log" log/' system "svn update log/" system "svn commit -m 'Ignoring all files in /log/ ending in .log'" system 'svn propset svn:ignore "*.db" db/' system "svn update db/" system "svn commit -m 'Ignoring all files in /db/ ending in .db'" system "svn move config/database.yml config/database.example" system "svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'" system 'svn propset svn:ignore "database.yml" config/' system "svn update config/" system "svn commit -m 'Ignoring database.yml'" system "svn remove tmp/*" system "svn commit -m 'Removing /tmp/ folder'" system 'svn propset svn:ignore "*" tmp/' end desc "Add new files to subversion" task :add_new_files do system "svn status | grep '^\?' | sed -e 's/? *//' | sed -e 's/ /\ /g' | xargs svn add" end desc "shortcut for adding new files" task :add => [ :add_new_files ]
echo "Subject: [rake deploy] Deployed revision `cd /home/www/current ; svn info | grep Revision | sed "s/Revision: //"` on `hostname` at `date`" > ~/sendmail_tmp
desc 'Create YAML test fixtures from data in an existing database. Defaults to development database. Set RAILS_ENV to override.' task :extract_fixtures => :environment do sql = "SELECT * FROM %s" skip_tables = ["schema_info"] ActiveRecord::Base.establish_connection (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| i = "000" File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file| data = ActiveRecord::Base.connection.select_all(sql % table_name) file.write data.inject({}) { |hash, record| hash["#{table_name}_#{i.succ!}"] = record hash }.to_yaml end end end