sequel-rails 0.1.8 → 0.4.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +3 -0
- data/.travis.yml +12 -0
- data/Gemfile +13 -16
- data/History.md +142 -0
- data/README.md +124 -0
- data/Rakefile +6 -30
- data/config.ru +7 -0
- data/lib/generators/sequel.rb +11 -8
- data/lib/generators/sequel/migration/migration_generator.rb +36 -11
- data/lib/generators/sequel/migration/templates/migration.rb.erb +48 -0
- data/lib/generators/sequel/model/model_generator.rb +8 -2
- data/lib/generators/sequel/model/templates/migration.rb.erb +16 -0
- data/lib/generators/sequel/model/templates/{model.rb → model.rb.erb} +4 -1
- data/lib/generators/sequel/observer/observer_generator.rb +2 -2
- data/lib/generators/sequel/observer/templates/{observer.rb → observer.rb.erb} +0 -0
- data/lib/sequel-rails.rb +1 -1
- data/lib/sequel_rails.rb +2 -0
- data/lib/sequel_rails/configuration.rb +64 -0
- data/lib/sequel_rails/migrations.rb +22 -0
- data/lib/sequel_rails/railtie.rb +94 -0
- data/lib/sequel_rails/railties/controller_runtime.rb +40 -0
- data/lib/sequel_rails/railties/database.rake +175 -0
- data/lib/sequel_rails/railties/i18n_support.rb +10 -0
- data/lib/sequel_rails/railties/log_subscriber.rb +56 -0
- data/lib/sequel_rails/sequel/database/active_support_notification.rb +28 -0
- data/lib/sequel_rails/sequel/plugins/rails_extensions.rb +35 -0
- data/lib/sequel_rails/session_store.rb +80 -0
- data/lib/sequel_rails/storage.rb +58 -0
- data/lib/sequel_rails/storage/abstract.rb +52 -0
- data/lib/sequel_rails/storage/jdbc.rb +45 -0
- data/lib/sequel_rails/storage/mysql.rb +31 -0
- data/lib/sequel_rails/storage/mysql2.rb +6 -0
- data/lib/sequel_rails/storage/postgres.rb +22 -0
- data/lib/sequel_rails/storage/sqlite.rb +26 -0
- data/lib/sequel_rails/version.rb +3 -0
- data/sequel-rails.gemspec +22 -86
- data/spec/internal/app/models/user.rb +2 -0
- data/spec/internal/config/database.yml +7 -0
- data/spec/internal/config/routes.rb +3 -0
- data/spec/internal/db/schema.rb +8 -0
- data/spec/internal/public/favicon.ico +0 -0
- data/spec/lib/generators/sequel/migration_spec.rb +256 -0
- data/spec/lib/sequel_rails/railtie_spec.rb +85 -0
- data/spec/lib/sequel_rails/railties/log_subscriber_spec.rb +29 -0
- data/spec/lib/sequel_rails/storage_spec.rb +108 -0
- data/spec/spec_helper.rb +30 -16
- data/tasks/spec.rake +63 -29
- metadata +194 -142
- data/CHANGELOG +0 -15
- data/README.rdoc +0 -86
- data/VERSION +0 -1
- data/autotest/discover.rb +0 -1
- data/lib/generators/sequel/migration/templates/migration.rb +0 -16
- data/lib/sequel-rails/configuration.rb +0 -61
- data/lib/sequel-rails/migrations.rb +0 -30
- data/lib/sequel-rails/railtie.rb +0 -90
- data/lib/sequel-rails/railties/benchmarking_mixin.rb +0 -23
- data/lib/sequel-rails/railties/controller_runtime.rb +0 -43
- data/lib/sequel-rails/railties/database.rake +0 -148
- data/lib/sequel-rails/railties/i18n_support.rb +0 -12
- data/lib/sequel-rails/railties/log_subscriber.rb +0 -31
- data/lib/sequel-rails/runtime.rb +0 -14
- data/lib/sequel-rails/session_store.rb +0 -82
- data/lib/sequel-rails/setup.rb +0 -19
- data/lib/sequel-rails/storage.rb +0 -210
- data/spec/rcov.opts +0 -6
- data/spec/setup_spec.rb +0 -7
- data/spec/spec.opts +0 -4
- data/tasks/ci.rake +0 -1
- data/tasks/clean.rake +0 -6
- data/tasks/metrics.rake +0 -37
- data/tasks/yard.rake +0 -9
- data/tasks/yardstick.rake +0 -20
@@ -0,0 +1,48 @@
|
|
1
|
+
Sequel.migration do
|
2
|
+
<%- if use_change -%>
|
3
|
+
change do
|
4
|
+
<%= table_action %>_table :<%= table_name %> do
|
5
|
+
<%- if table_action == 'create' -%>
|
6
|
+
primary_key :id
|
7
|
+
<%- end -%>
|
8
|
+
<%- attributes.each do |attribute| -%>
|
9
|
+
<%- if table_action == 'create' -%>
|
10
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
11
|
+
<%- else -%>
|
12
|
+
<%= column_action %>_column :<%= attribute.name %><% if column_action == 'add' %>, <%= attribute.type_class %><% end %>
|
13
|
+
<%- end -%>
|
14
|
+
<%- end -%>
|
15
|
+
end
|
16
|
+
end
|
17
|
+
<%- else -%>
|
18
|
+
up do
|
19
|
+
<%- if table_action == 'drop' -%>
|
20
|
+
drop_table :<%= table_name %>
|
21
|
+
<%- else -%>
|
22
|
+
<%= table_action %>_table :<%= table_name %> do
|
23
|
+
<%- attributes.each do |attribute| -%>
|
24
|
+
<%- if table_action == 'create' -%>
|
25
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
26
|
+
<%- else -%>
|
27
|
+
<%= column_action %>_column :<%= attribute.name %><% if column_action == 'add' %>, <%= attribute.type_class %><% end %>
|
28
|
+
<%- end -%>
|
29
|
+
<%- end -%>
|
30
|
+
end
|
31
|
+
<%- end -%>
|
32
|
+
end
|
33
|
+
|
34
|
+
down do
|
35
|
+
<%- alter_table_action = (table_action == 'drop') ? 'create' : table_action -%>
|
36
|
+
<%- alter_column_action = (column_action == 'add') ? 'drop' : 'add' -%>
|
37
|
+
<%= alter_table_action %>_table :<%= table_name %> do
|
38
|
+
<%- attributes.each do |attribute| -%>
|
39
|
+
<%- if alter_table_action == 'create' -%>
|
40
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
41
|
+
<%- else -%>
|
42
|
+
<%= alter_column_action %>_column :<%= attribute.name %><% if alter_column_action == 'add' %>, <%= attribute.type_class %><% end %>
|
43
|
+
<%- end -%>
|
44
|
+
<%- end -%>
|
45
|
+
end
|
46
|
+
end
|
47
|
+
<%- end -%>
|
48
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "generators/sequel"
|
2
2
|
|
3
3
|
module Sequel
|
4
4
|
module Generators
|
@@ -8,11 +8,17 @@ module Sequel
|
|
8
8
|
|
9
9
|
check_class_collision
|
10
10
|
|
11
|
+
class_option :migration, :type => :boolean
|
11
12
|
class_option :timestamps, :type => :boolean
|
12
13
|
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
|
13
14
|
|
15
|
+
def create_migration_file
|
16
|
+
return unless options[:migration]
|
17
|
+
migration_template "migration.rb.erb", "db/migrate/create_#{table_name}.rb"
|
18
|
+
end
|
19
|
+
|
14
20
|
def create_model_file
|
15
|
-
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
21
|
+
template 'model.rb.erb', File.join('app/models', class_path, "#{file_name}.rb")
|
16
22
|
end
|
17
23
|
|
18
24
|
hook_for :test_framework
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Sequel.migration do
|
2
|
+
change do
|
3
|
+
|
4
|
+
create_table :<%= table_name %> do
|
5
|
+
primary_key :id
|
6
|
+
<%- if options[:timestamps] -%>
|
7
|
+
DateTime :created_at
|
8
|
+
DateTime :updated_at
|
9
|
+
<%- end -%>
|
10
|
+
<%- attributes.each do |attribute| -%>
|
11
|
+
<%= attribute.type_class %> :<%= attribute.name %>
|
12
|
+
<%- end -%>
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "generators/sequel"
|
2
2
|
|
3
3
|
module Sequel
|
4
4
|
module Generators
|
@@ -8,7 +8,7 @@ module Sequel
|
|
8
8
|
check_class_collision :suffix => "Observer"
|
9
9
|
|
10
10
|
def create_observer_file
|
11
|
-
template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
|
11
|
+
template 'observer.rb.erb', File.join('app/models', class_path, "#{file_name}_observer.rb")
|
12
12
|
end
|
13
13
|
|
14
14
|
hook_for :test_framework
|
File without changes
|
data/lib/sequel-rails.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require "sequel_rails"
|
data/lib/sequel_rails.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
2
|
+
|
3
|
+
module SequelRails
|
4
|
+
|
5
|
+
mattr_accessor :configuration
|
6
|
+
|
7
|
+
def self.setup(environment)
|
8
|
+
::Sequel.connect configuration.environment_for environment.to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
class Configuration
|
12
|
+
|
13
|
+
def self.for(root, database_yml_hash)
|
14
|
+
::SequelRails.configuration ||= new(root, database_yml_hash)
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :root, :raw
|
18
|
+
attr_accessor :logger
|
19
|
+
attr_accessor :migration_dir
|
20
|
+
|
21
|
+
def environment_for(name)
|
22
|
+
environments[name.to_s] || environments[name.to_sym]
|
23
|
+
end
|
24
|
+
|
25
|
+
def environments
|
26
|
+
@environments ||= @raw.inject({}) do |normalized, environment|
|
27
|
+
name, config = environment.first, environment.last
|
28
|
+
normalized[name] = normalize_repository_config(config)
|
29
|
+
normalized
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def initialize(root, database_yml_hash)
|
36
|
+
@root, @raw = root, database_yml_hash
|
37
|
+
end
|
38
|
+
|
39
|
+
def normalize_repository_config(hash)
|
40
|
+
config = {}
|
41
|
+
hash.each do |key, value|
|
42
|
+
config[key.to_s] =
|
43
|
+
if key.to_s == 'port'
|
44
|
+
value.to_i
|
45
|
+
elsif key.to_s == 'adapter' && value == 'sqlite3'
|
46
|
+
'sqlite'
|
47
|
+
elsif key.to_s == 'database' && (hash['adapter'] == 'sqlite3' ||
|
48
|
+
hash['adapter'] == 'sqlite' ||
|
49
|
+
hash[:adapter] == 'sqlite3' ||
|
50
|
+
hash[:adapter] == 'sqlite')
|
51
|
+
value == ':memory:' ? value : File.expand_path((hash['database'] || hash[:database]), root)
|
52
|
+
elsif key.to_s == 'adapter' && value == 'postgresql'
|
53
|
+
'postgres'
|
54
|
+
else
|
55
|
+
value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
config
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'sequel/extensions/migration'
|
2
|
+
|
3
|
+
module SequelRails
|
4
|
+
class Migrations
|
5
|
+
def self.migrate_up!(version=nil)
|
6
|
+
opts = {}
|
7
|
+
opts[:target] = version.to_i if version
|
8
|
+
::Sequel::Migrator.run(::Sequel::Model.db, "db/migrate", opts)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.migrate_down!(version=nil)
|
12
|
+
opts = {}
|
13
|
+
opts[:target] = version.to_i if version
|
14
|
+
::Sequel::Migrator.run(::Sequel::Model.db, "db/migrate", opts)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.pending_migrations?
|
18
|
+
return false unless File.exists?("db/migrate")
|
19
|
+
!::Sequel::Migrator.is_current?(::Sequel::Model.db, "db/migrate")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require "sequel"
|
2
|
+
|
3
|
+
require "rails"
|
4
|
+
require "active_model/railtie"
|
5
|
+
|
6
|
+
# Comment taken from active_record/railtie.rb
|
7
|
+
#
|
8
|
+
# For now, action_controller must always be present with
|
9
|
+
# rails, so let's make sure that it gets required before
|
10
|
+
# here. This is needed for correctly setting up the middleware.
|
11
|
+
# In the future, this might become an optional require.
|
12
|
+
require "action_controller/railtie"
|
13
|
+
|
14
|
+
require "sequel_rails/configuration"
|
15
|
+
require "sequel_rails/migrations"
|
16
|
+
require "sequel_rails/railties/log_subscriber"
|
17
|
+
require "sequel_rails/railties/i18n_support"
|
18
|
+
require "sequel_rails/railties/controller_runtime"
|
19
|
+
require "sequel_rails/sequel/plugins/rails_extensions"
|
20
|
+
require "sequel_rails/sequel/database/active_support_notification"
|
21
|
+
|
22
|
+
module SequelRails
|
23
|
+
|
24
|
+
class Railtie < Rails::Railtie
|
25
|
+
|
26
|
+
::SequelRails::Railties::LogSubscriber.attach_to :sequel
|
27
|
+
|
28
|
+
config.app_generators.orm :sequel, :migration => true
|
29
|
+
config.rails_fancy_pants_logging = true
|
30
|
+
|
31
|
+
config.action_dispatch.rescue_responses.merge!(
|
32
|
+
"Sequel::Plugins::RailsExtensions::ModelNotFound" => :not_found,
|
33
|
+
"Sequel::ValidationFailed" => :unprocessable_entity,
|
34
|
+
"Sequel::NoExistingObject" => :unprocessable_entity
|
35
|
+
)
|
36
|
+
|
37
|
+
rake_tasks do
|
38
|
+
load "sequel_rails/railties/database.rake"
|
39
|
+
end
|
40
|
+
|
41
|
+
initializer 'sequel.configuration' do |app|
|
42
|
+
configure_sequel app
|
43
|
+
end
|
44
|
+
|
45
|
+
initializer 'sequel.logger' do |app|
|
46
|
+
setup_logger app, ::Rails.logger
|
47
|
+
end
|
48
|
+
|
49
|
+
initializer 'sequel.i18n_support' do |app|
|
50
|
+
setup_i18n_support app
|
51
|
+
end
|
52
|
+
|
53
|
+
# Expose database runtime to controller for logging.
|
54
|
+
initializer 'sequel.log_runtime' do |app|
|
55
|
+
setup_controller_runtime app
|
56
|
+
end
|
57
|
+
|
58
|
+
initializer 'sequel.connect' do |app|
|
59
|
+
::SequelRails.setup ::Rails.env
|
60
|
+
end
|
61
|
+
|
62
|
+
# Run setup code after_initialize to make sure all config/initializers
|
63
|
+
# are in effect once we setup the connection. This is especially necessary
|
64
|
+
# for the cascaded adapter wrappers that need to be declared before setup.
|
65
|
+
config.after_initialize do |app|
|
66
|
+
::Sequel::Model.plugin :active_model
|
67
|
+
::Sequel::Model.plugin :validation_helpers
|
68
|
+
::Sequel::Model.plugin :rails_extensions
|
69
|
+
::Sequel::Model.raise_on_save_failure = false
|
70
|
+
end
|
71
|
+
|
72
|
+
# Support overwriting crucial steps in subclasses
|
73
|
+
def configure_sequel(app)
|
74
|
+
app.config.sequel = ::SequelRails::Configuration.for(
|
75
|
+
::Rails.root, app.config.database_configuration
|
76
|
+
)
|
77
|
+
end
|
78
|
+
|
79
|
+
def setup_i18n_support(app)
|
80
|
+
::Sequel::Model.send :include, ::SequelRails::I18nSupport
|
81
|
+
end
|
82
|
+
|
83
|
+
def setup_controller_runtime(app)
|
84
|
+
require 'sequel_rails/railties/controller_runtime'
|
85
|
+
ActionController::Base.send :include, SequelRails::Railties::ControllerRuntime
|
86
|
+
end
|
87
|
+
|
88
|
+
def setup_logger(app, logger)
|
89
|
+
app.config.sequel.logger = logger
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "active_support/core_ext/module/attr_internal"
|
2
|
+
|
3
|
+
module SequelRails
|
4
|
+
module Railties
|
5
|
+
|
6
|
+
module ControllerRuntime
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
attr_internal :db_runtime
|
13
|
+
|
14
|
+
def cleanup_view_runtime
|
15
|
+
db_rt_before_render = ::SequelRails::Railties::LogSubscriber.reset_runtime
|
16
|
+
runtime = super
|
17
|
+
db_rt_after_render = ::SequelRails::Railties::LogSubscriber.reset_runtime
|
18
|
+
self.db_runtime = db_rt_before_render + db_rt_after_render
|
19
|
+
runtime - db_rt_after_render
|
20
|
+
end
|
21
|
+
|
22
|
+
def append_info_to_payload(payload)
|
23
|
+
super
|
24
|
+
payload[:db_runtime] = db_runtime
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
|
29
|
+
def log_process_action(payload)
|
30
|
+
messages, db_runtime = super, payload[:db_runtime]
|
31
|
+
messages << ("Models: %.1fms" % db_runtime.to_f) if db_runtime
|
32
|
+
messages
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'sequel_rails/storage'
|
2
|
+
|
3
|
+
# TODO: DRY these up
|
4
|
+
namespace :db do
|
5
|
+
def db_for_current_env
|
6
|
+
@db_for_current_env ||= {}
|
7
|
+
@db_for_current_env[Rails.env] ||= begin
|
8
|
+
config = ::SequelRails.configuration.environment_for(Rails.env)
|
9
|
+
::Sequel.connect(config)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# desc "Raises an error if there are pending migrations"
|
14
|
+
task :abort_if_pending_migrations => [:environment, "db:migrate:load"] do
|
15
|
+
if SequelRails::Migrations.pending_migrations?
|
16
|
+
puts "You have pending migrations:"
|
17
|
+
abort %{Run `rake db:migrate` to update your database then try again.}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
namespace :schema do
|
22
|
+
desc "Create a db/schema.rb file that can be portably used against any DB supported by Sequel"
|
23
|
+
task :dump => :environment do
|
24
|
+
::Sequel.extension :schema_dumper
|
25
|
+
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
|
26
|
+
file.write db_for_current_env.dump_schema_migration(same_db: true)
|
27
|
+
end
|
28
|
+
Rake::Task["db:schema:dump"].reenable
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "Load a schema.rb file into the database"
|
32
|
+
task :load => :environment do
|
33
|
+
file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
|
34
|
+
if File.exists?(file)
|
35
|
+
require 'sequel/extensions/migration'
|
36
|
+
load(file)
|
37
|
+
SequelRails::Migration.descendants.first.apply(db_for_current_env, :up)
|
38
|
+
else
|
39
|
+
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}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
namespace :create do
|
45
|
+
desc 'Create all the local databases defined in config/database.yml'
|
46
|
+
task :all => :environment do
|
47
|
+
SequelRails::Storage.create_all
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
desc "Create the database defined in config/database.yml for the current Rails.env"
|
52
|
+
task :create, [:env] => :environment do |t, args|
|
53
|
+
args.with_defaults(:env => Rails.env)
|
54
|
+
|
55
|
+
SequelRails::Storage.adapter_for(args.env).create
|
56
|
+
end
|
57
|
+
|
58
|
+
namespace :drop do
|
59
|
+
desc 'Drops all the local databases defined in config/database.yml'
|
60
|
+
task :all => :environment do
|
61
|
+
SequelRails::Storage.drop_all
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
desc "Create the database defined in config/database.yml for the current Rails.env"
|
66
|
+
task :drop, [:env] => :environment do |t, args|
|
67
|
+
args.with_defaults(:env => Rails.env)
|
68
|
+
|
69
|
+
Rails::Sequel::Storage.adapter_for(args.env).drop
|
70
|
+
end
|
71
|
+
|
72
|
+
namespace :migrate do
|
73
|
+
task :load => :environment do
|
74
|
+
require 'sequel_rails/migrations'
|
75
|
+
end
|
76
|
+
|
77
|
+
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
|
78
|
+
task :redo => :load do
|
79
|
+
if ENV["VERSION"]
|
80
|
+
Rake::Task["db:migrate:down"].invoke
|
81
|
+
Rake::Task["db:migrate:up"].invoke
|
82
|
+
else
|
83
|
+
Rake::Task["db:rollback"].invoke
|
84
|
+
Rake::Task["db:migrate"].invoke
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
desc 'Resets your database using your migrations for the current environment'
|
90
|
+
task :reset => ["db:drop", "db:create", "db:migrate"]
|
91
|
+
|
92
|
+
desc 'Runs the "up" for a given migration VERSION.'
|
93
|
+
task :up => :load do
|
94
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
95
|
+
raise "VERSION is required" unless version
|
96
|
+
SequelRails::Migrations.migrate_up!(version)
|
97
|
+
Rake::Task["db:schema:dump"].invoke unless Rails.env.test?
|
98
|
+
end
|
99
|
+
|
100
|
+
desc 'Runs the "down" for a given migration VERSION.'
|
101
|
+
task :down => :load do
|
102
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
103
|
+
raise "VERSION is required" unless version
|
104
|
+
SequelRails::Migrations.migrate_down!(version)
|
105
|
+
Rake::Task["db:schema:dump"].invoke unless Rails.env.test?
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
desc 'Migrate the database to the latest version'
|
110
|
+
task :migrate => "migrate:load" do
|
111
|
+
SequelRails::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
112
|
+
Rake::Task["db:schema:dump"].invoke unless Rails.env.test?
|
113
|
+
end
|
114
|
+
|
115
|
+
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
|
116
|
+
task :rollback => "migrate:load" do
|
117
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
118
|
+
Sequel::Migrator.rollback('db/migrate/', step)
|
119
|
+
Rake::Task["db:schema:dump"].invoke unless Rails.env.test?
|
120
|
+
end
|
121
|
+
|
122
|
+
desc 'Pushes the schema to the next version. Specify the number of steps with STEP=n'
|
123
|
+
task :forward => "migrate:load" do
|
124
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
125
|
+
Sequel::Migrator.forward('db/migrate/', step)
|
126
|
+
Rake::Task["db:schema:dump"].invoke unless Rails.env.test?
|
127
|
+
end
|
128
|
+
|
129
|
+
desc 'Load the seed data from db/seeds.rb'
|
130
|
+
task :seed => :abort_if_pending_migrations do
|
131
|
+
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
|
132
|
+
load(seed_file) if File.exist?(seed_file)
|
133
|
+
end
|
134
|
+
|
135
|
+
desc 'Create the database, load the schema, and initialize with the seed data'
|
136
|
+
task :setup => [ 'db:create', 'db:schema:load', 'db:seed' ]
|
137
|
+
|
138
|
+
desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
|
139
|
+
task :reset => [ 'db:drop', 'db:setup' ]
|
140
|
+
|
141
|
+
desc 'Forcibly close any open connections to the current env database (PostgreSQL specific)'
|
142
|
+
task :force_close_open_connections => :environment do
|
143
|
+
if db_for_current_env.database_type==:postgres
|
144
|
+
begin
|
145
|
+
# Will only work on Postgres > 8.4
|
146
|
+
pid_column = db_for_current_env.server_version < 90200 ? "procpid" : "pid"
|
147
|
+
db_for_current_env.execute <<-SQL.gsub(/^\s{9}/,'')
|
148
|
+
SELECT COUNT(pg_terminate_backend(#{pid_column}))
|
149
|
+
FROM pg_stat_activity
|
150
|
+
WHERE datname = '#{db_for_current_env.opts[:database]}';
|
151
|
+
SQL
|
152
|
+
rescue => e
|
153
|
+
#Will raise an error as it kills existing process running this command
|
154
|
+
#Seems to be only way to ensure *all* test connections are closed
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
namespace :test do
|
160
|
+
desc "Prepare test database (ensure all migrations ran, drop and re-create database then load schema). This task can be run in the same invocation as other task (eg: rake db:migrate db:test:prepare)."
|
161
|
+
task :prepare => "db:abort_if_pending_migrations" do
|
162
|
+
previous_env, Rails.env = Rails.env, 'test'
|
163
|
+
Rake::Task['db:force_close_open_connections'].execute
|
164
|
+
Rake::Task['db:drop'].execute
|
165
|
+
Rake::Task['db:create'].execute
|
166
|
+
Rake::Task['db:schema:load'].execute
|
167
|
+
Sequel::DATABASES.each do |db|
|
168
|
+
db.disconnect
|
169
|
+
end
|
170
|
+
Rails.env = previous_env
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
task "test:prepare" => "db:test:prepare"
|