lib/sequel-rails/railties/database.rake in vizzuality-sequel-rails-0.3.4 vs lib/sequel-rails/railties/database.rake in vizzuality-sequel-rails-0.3.5

- old
+ new

@@ -1,14 +1,35 @@ # TODO: DRY these up +###################################################### +# BEWARE: Regular Expressions of DOOOOOOOOOOOOOOOOOM # +###################################################### namespace :db do namespace :schema do desc "Create a db/schema.rb file that can be portably used against any DB supported by Sequel" task :dump => :environment do Sequel.extension :schema_dumper db = Sequel.connect(::Rails::Sequel.configuration.environment_for(Rails.env)) File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file| - file.write db.dump_schema_migration(same_db: true) + database = db.dump_schema_migration(same_db: true) + # 0. Add schema_migrations info (mucho importante!) + filenames = db[:schema_migrations].map{|x| x[:filename]} + statements = filenames.map do |f| + "self[:schema_migrations].insert(:filename => \"#{f}\")" + end + + inserts = statements.map do |s| + " #{s}" + end.join("\n") + + database.gsub!(/(create_table\(:schema_migrations\) do.+?end)/m, '\1'+"\n\n#{inserts}\n") + + # 1. Fuck arbitrary whitespaces. + database.gsub!(/\s+$/,"\n") + + # 2. Add new line at end of file + database += "\n" + file.write database end Rake::Task["db:schema:dump"].reenable end desc "Load a schema.rb file into the database" @@ -17,10 +38,11 @@ Rails::Sequel::Storage.new(Rails.env).create file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb" if File.exists?(file) load(file) + Sequel::Migration.descendants.first.apply(::Sequel::Model.db, :up) else abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/boot.rb to limit the frameworks that will be loaded} end end end @@ -127,10 +149,10 @@ seed_file = File.join(Rails.root, 'db', 'seeds.rb') load(seed_file) if File.exist?(seed_file) end desc 'Create the database, load the schema, and initialize with the seed data' - task :setup => [ 'db:create', 'db:migrate', 'db:seed' ] + task :setup => [ 'db:create', 'db:schema:load', 'db:seed' ] desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.' task :reset => [ 'db:drop', 'db:setup' ] desc 'Forcibly close any open connections to the test database'