rails3_sequel 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +100 -0
- data/VERSION +1 -0
- data/lib/rails/generators/sequel/active_model.rb +40 -0
- data/lib/rails/generators/sequel/generated_attribute.rb +64 -0
- data/lib/rails/generators/sequel/migration/migration_generator.rb +26 -0
- data/lib/rails/generators/sequel/migration/templates/migration.rb +17 -0
- data/lib/rails/generators/sequel/model/model_generator.rb +28 -0
- data/lib/rails/generators/sequel/model/templates/migration.rb +22 -0
- data/lib/rails/generators/sequel/model/templates/model.rb +2 -0
- data/lib/rails/generators/sequel/observer/observer_generator.rb +15 -0
- data/lib/rails/generators/sequel/observer/templates/observer.rb +2 -0
- data/lib/rails/generators/sequel.rb +41 -0
- data/lib/rails3_sequel/database.rb +25 -0
- data/lib/rails3_sequel/logging.rb +27 -0
- data/lib/rails3_sequel/railtie.rb +58 -0
- data/lib/rails3_sequel/railties/controller_runtime.rb +36 -0
- data/lib/rails3_sequel/railties/database.rake +115 -0
- data/lib/rails3_sequel/railties/log_subscriber.rb +30 -0
- data/lib/rails3_sequel.rb +1 -0
- metadata +109 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2010 Rachot Moragraan <janechii@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
Rails 3 Sequel integration
|
2
|
+
==========================
|
3
|
+
|
4
|
+
*What works so far:*
|
5
|
+
|
6
|
+
+ Generators
|
7
|
+
- Models
|
8
|
+
- Migrations
|
9
|
+
- Observers
|
10
|
+
- Scaffolding Controllers - Sequel specific methods
|
11
|
+
|
12
|
+
+ Rake tasks
|
13
|
+
- mostly everything except anything that has to do with database creation (db:create, test:prepare, etc)
|
14
|
+
|
15
|
+
+ Railties
|
16
|
+
- uses database.yml configuration
|
17
|
+
- db connection
|
18
|
+
- query logging
|
19
|
+
- controller logging
|
20
|
+
- sane default sequel options and plugins for Rails
|
21
|
+
|
22
|
+
+ Gemspec
|
23
|
+
|
24
|
+
*What is still need done:*
|
25
|
+
|
26
|
+
+ More testing
|
27
|
+
+ i18n
|
28
|
+
+ Session Store
|
29
|
+
+ more rake tasks
|
30
|
+
|
31
|
+
Installation
|
32
|
+
------------
|
33
|
+
|
34
|
+
Build from the gem spec:
|
35
|
+
|
36
|
+
gem build rails3_sequel.gemspec
|
37
|
+
|
38
|
+
Install:
|
39
|
+
|
40
|
+
gem install rails3_sequel-x.x.x.gem
|
41
|
+
|
42
|
+
Usage - Railties
|
43
|
+
----------------
|
44
|
+
|
45
|
+
In your config/application.rb, take out the require "all" line and choose what frameworks you want to include like this:
|
46
|
+
|
47
|
+
require "action_controller/railtie"
|
48
|
+
require "action_mailer/railtie"
|
49
|
+
require "active_resource/railtie"
|
50
|
+
require "rails/test_unit/railtie"
|
51
|
+
|
52
|
+
# most importantly :)
|
53
|
+
require 'rails3_sequel/railtie'
|
54
|
+
|
55
|
+
This way Rails wont load activerecord.
|
56
|
+
|
57
|
+
Config options:
|
58
|
+
|
59
|
+
# set false to turn off Rails SQL logging
|
60
|
+
# true by default
|
61
|
+
config.rails_fancy_pants_logging = false
|
62
|
+
|
63
|
+
# specify your own loggers
|
64
|
+
config.loggers << Logger.new('test.log')
|
65
|
+
|
66
|
+
# shortcut to log_warn_duration in Sequel
|
67
|
+
config.log_warn_duration
|
68
|
+
|
69
|
+
These options may be useful in the production configuration file. Rails does not log any SQL in production mode, but you may want to still log long running queries or queries with errors (which are supported by Sequel).
|
70
|
+
|
71
|
+
Rake tasks usage.... todo
|
72
|
+
|
73
|
+
Usage - Generators
|
74
|
+
------------------
|
75
|
+
|
76
|
+
Basics:
|
77
|
+
|
78
|
+
rails g scaffold cat name:String:pk specie:String:pk age:Integer
|
79
|
+
|
80
|
+
Will use name and specie as composite primary key.
|
81
|
+
|
82
|
+
|
83
|
+
Generator options (set in config/application.rb):
|
84
|
+
|
85
|
+
config.generators do |g|
|
86
|
+
g.orm :sequel, :autoincrement => true, :migration => true, :timestamps => false
|
87
|
+
|
88
|
+
The above will always generate migration files, with autoincrement/serial field named "id", but no automatic timstamp fields updated_at or created_at.
|
89
|
+
|
90
|
+
more to come...
|
91
|
+
|
92
|
+
License
|
93
|
+
-------
|
94
|
+
|
95
|
+
MIT
|
96
|
+
|
97
|
+
Credits
|
98
|
+
-------
|
99
|
+
|
100
|
+
Based partially on rails_sequel by Piotr Usewicz: http://github.com/pusewicz/rails_sequel
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
alpha
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rails/generators/active_model'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Generators
|
5
|
+
class ActiveModel < Rails::Generators::ActiveModel
|
6
|
+
def self.all(klass)
|
7
|
+
"#{klass}.all"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.find(klass, params=nil)
|
11
|
+
"#{klass}[#{params}]"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.build(klass, params=nil)
|
15
|
+
if params then
|
16
|
+
"#{klass}.new(#{params})"
|
17
|
+
else
|
18
|
+
"#{klass}.new"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def save
|
23
|
+
# probably will set raise_on_save_failure to false by default when using Rails
|
24
|
+
"#{name}.save"
|
25
|
+
end
|
26
|
+
|
27
|
+
def update_attributes(params=nil)
|
28
|
+
"#{name}.update(#{params}) != false"
|
29
|
+
end
|
30
|
+
|
31
|
+
def errors
|
32
|
+
"#{name}.errors"
|
33
|
+
end
|
34
|
+
|
35
|
+
def destroy
|
36
|
+
"#{name}.destroy"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Rails
|
2
|
+
module Generators
|
3
|
+
class GeneratedAttribute
|
4
|
+
attr_accessor :name, :type
|
5
|
+
|
6
|
+
def initialize(name, type)
|
7
|
+
@name = name
|
8
|
+
@type = type.to_sym
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_definition
|
12
|
+
case type
|
13
|
+
when :Text then "String :#{name}, :text => true"
|
14
|
+
when :Boolean then "TrueClass :#{name}"
|
15
|
+
else "#{type} :#{name}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def alter_definition
|
20
|
+
case type
|
21
|
+
when :Text then "String, :text => true"
|
22
|
+
when :Boolean then "TrueClass"
|
23
|
+
else type
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def field_type
|
28
|
+
@field_type ||= case type
|
29
|
+
when :Integer, :Float, :BigDecimal, :FixNum, :Numeric then :text_field
|
30
|
+
when :DateTime, :Time then :datetime_select
|
31
|
+
when :Date then :date_select
|
32
|
+
when :String then :text_field
|
33
|
+
when :Text then :text_area
|
34
|
+
when :Boolean, :TrueClass, :FalseClass then :check_box
|
35
|
+
else
|
36
|
+
:text_field
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def default
|
41
|
+
@default ||= case type
|
42
|
+
when :integer then 1
|
43
|
+
when :float then 1.5
|
44
|
+
when :decimal then "9.99"
|
45
|
+
when :datetime, :timestamp, :time then Time.now.to_s(:db)
|
46
|
+
when :date then Date.today.to_s(:db)
|
47
|
+
when :string then "MyString"
|
48
|
+
when :text then "MyText"
|
49
|
+
when :boolean then false
|
50
|
+
else
|
51
|
+
""
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def human_name
|
56
|
+
name.to_s.humanize
|
57
|
+
end
|
58
|
+
|
59
|
+
def reference?
|
60
|
+
false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rails/generators/sequel'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Generators
|
5
|
+
class MigrationGenerator < Base
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
7
|
+
|
8
|
+
def create_migration_file
|
9
|
+
set_local_assigns!
|
10
|
+
migration_template 'migration.rb', "db/migrate/#{file_name}.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
attr_reader :migration_action
|
16
|
+
|
17
|
+
def set_local_assigns!
|
18
|
+
if file_name =~ /^(add|drop)_.*_(?:to|from)_(.*)/
|
19
|
+
@migration_action = $1
|
20
|
+
@table_name = $2.pluralize
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Class.new(Sequel::Migration) do
|
2
|
+
def up
|
3
|
+
Sequel::Model.db.alter_table :<%= table_name %> do
|
4
|
+
<% for a in attributes do -%>
|
5
|
+
<%= migration_action %>_column :<%= a.name %><% if migration_action == 'add' %>, <%= a.alter_definition %><% end %>
|
6
|
+
<% end -%>
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def down
|
11
|
+
Sequel::Model.db.alter_table :<%= table_name %> do
|
12
|
+
<% for a in attributes.reverse do -%>
|
13
|
+
<%= migration_action == 'add' ? 'drop' : 'add' %>_column :<%= a.name %><% if migration_action == 'drop' %>, <%= a.alter_definition %><% end %>
|
14
|
+
<% end -%>
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails/generators/sequel'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Generators
|
5
|
+
class ModelGenerator < Base
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
7
|
+
|
8
|
+
check_class_collision
|
9
|
+
|
10
|
+
class_option :autoincrement, :type => :boolean
|
11
|
+
class_option :migration, :type => :boolean
|
12
|
+
class_option :timestamps, :type => :boolean
|
13
|
+
# TODO: parent option?
|
14
|
+
|
15
|
+
def create_migration_file
|
16
|
+
return unless options[:migration] && options[:parent].nil?
|
17
|
+
migration_template 'migration.rb', "db/migrate/create_#{table_name}.rb"
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_model_file
|
21
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
22
|
+
end
|
23
|
+
|
24
|
+
hook_for :test_framework
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Class.new(Sequel::Migration) do
|
2
|
+
def up
|
3
|
+
create_table :<%= table_name %> do
|
4
|
+
<% if options[:autoincrement] then -%>
|
5
|
+
primary_key :id
|
6
|
+
<% end -%>
|
7
|
+
|
8
|
+
<% for a in attributes do -%>
|
9
|
+
<%= a.create_definition %>
|
10
|
+
<% end -%>
|
11
|
+
<% if options[:timestamps] then -%>
|
12
|
+
DateTime :created_at
|
13
|
+
DateTime :updated_at
|
14
|
+
<% end -%>
|
15
|
+
<%= cpk %>
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def down
|
20
|
+
drop_table :<%= table_name %>
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators/sequel'
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
module Generators
|
5
|
+
class ObserverGenerator < Base
|
6
|
+
check_class_collision :suffix => "Observer"
|
7
|
+
|
8
|
+
def create_observer_file
|
9
|
+
template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
|
10
|
+
end
|
11
|
+
|
12
|
+
hook_for :test_framework
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
require 'rails/generators/sequel/active_model'
|
5
|
+
|
6
|
+
# override Rails::Generators::GeneratedAttribute
|
7
|
+
require 'rails/generators/sequel/generated_attribute'
|
8
|
+
|
9
|
+
module Sequel
|
10
|
+
module Generators
|
11
|
+
class Base < Rails::Generators::NamedBase
|
12
|
+
include Rails::Generators::Migration
|
13
|
+
|
14
|
+
def self.source_root
|
15
|
+
@source_root ||= File.expand_path(File.join(base_name, generator_name, 'templates'), File.dirname(__FILE__))
|
16
|
+
end
|
17
|
+
|
18
|
+
def cpk
|
19
|
+
if @primary_keys and !@primary_keys.empty? then
|
20
|
+
"primary_key([#{@primary_keys.map {|pk| ':' + pk}.join(', ')}])"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def parse_attributes!
|
27
|
+
@primary_keys ||= []
|
28
|
+
|
29
|
+
self.attributes = (attributes || []).map do |key_value|
|
30
|
+
name, type, pk = key_value.split(':')
|
31
|
+
@primary_keys << name unless pk.nil?
|
32
|
+
Rails::Generators::GeneratedAttribute.new(name, type)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.next_migration_number(dirname)
|
37
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rails
|
2
|
+
module Sequel
|
3
|
+
module Database
|
4
|
+
mattr_reader :configurations
|
5
|
+
|
6
|
+
def self.configurations= (config)
|
7
|
+
@@configurations = config
|
8
|
+
|
9
|
+
for key,env in @@configurations do
|
10
|
+
# some translations
|
11
|
+
env['adapter'] = case env['adapter']
|
12
|
+
when 'postgresql' then 'postgres'
|
13
|
+
when 'sqlite3' then 'sqlite'
|
14
|
+
else env['adapter']
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Connects to database using constructed Database Connection URI
|
20
|
+
def self.connect
|
21
|
+
::Sequel.connect(self.configurations[Rails.env])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Rails
|
2
|
+
module Sequel
|
3
|
+
module Logging
|
4
|
+
def log_duration (duration, message)
|
5
|
+
@controller_runtime ||= 0
|
6
|
+
@controller_runtime += duration
|
7
|
+
ActiveSupport::Notifications.instrument('sequel.sql',
|
8
|
+
:sql => message,
|
9
|
+
:name => 'SQL',
|
10
|
+
:duration => duration * 1000
|
11
|
+
)
|
12
|
+
super(duration, message)
|
13
|
+
end
|
14
|
+
|
15
|
+
def log_each (level, message)
|
16
|
+
# Rails logging is handled by the log subscriber
|
17
|
+
less_rails = @loggers - [Rails.logger]
|
18
|
+
less_rails.each { |logger| logger.send(level, message) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def reset_runtime
|
22
|
+
rt, @controller_runtime = @controller_runtime, 0
|
23
|
+
rt.to_f * 1000
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'active_model/railtie'
|
3
|
+
|
4
|
+
# For now, action_controller must always be present with
|
5
|
+
# rails, so let's make sure that it gets required before
|
6
|
+
# here. This is needed for correctly setting up the middleware.
|
7
|
+
# In the future, this might become an optional require.
|
8
|
+
require "action_controller/railtie"
|
9
|
+
|
10
|
+
require 'sequel'
|
11
|
+
require 'rails3_sequel/database'
|
12
|
+
require 'rails3_sequel/logging'
|
13
|
+
require 'rails3_sequel/railties/controller_runtime'
|
14
|
+
require 'rails3_sequel/railties/log_subscriber'
|
15
|
+
|
16
|
+
module Rails
|
17
|
+
module Sequel
|
18
|
+
class Railtie < Rails::Railtie
|
19
|
+
log_subscriber :sequel, Rails::Sequel::Railties::LogSubscriber.new
|
20
|
+
|
21
|
+
config.generators.orm :sequel, :autoincrement => false, :migration => true, :timestamps => false
|
22
|
+
|
23
|
+
config.log_warn_duration = nil
|
24
|
+
config.rails_fancy_pants_logging = true
|
25
|
+
config.loggers = []
|
26
|
+
|
27
|
+
rake_tasks do
|
28
|
+
load 'rails3_sequel/railties/database.rake'
|
29
|
+
end
|
30
|
+
|
31
|
+
initializer 'sequel.initialize_database' do |app|
|
32
|
+
Rails::Sequel::Database.configurations = app.config.database_configuration
|
33
|
+
Rails::Sequel::Database.connect
|
34
|
+
end
|
35
|
+
|
36
|
+
initializer 'sequel.logging' do |app|
|
37
|
+
::Sequel::Model.db.log_warn_duration = 0.1
|
38
|
+
|
39
|
+
if app.config.rails_fancy_pants_logging then
|
40
|
+
::Sequel::Model.db.loggers << Rails.logger
|
41
|
+
::Sequel::Model.db.extend Rails::Sequel::Logging
|
42
|
+
ActionController::Base.send :include, Rails::Sequel::Railties::ControllerRuntime
|
43
|
+
end
|
44
|
+
|
45
|
+
# additional loggers
|
46
|
+
::Sequel::Model.db.loggers.concat(app.config.loggers)
|
47
|
+
end
|
48
|
+
|
49
|
+
config.after_initialize do
|
50
|
+
# set some sensible Rails defaults
|
51
|
+
::Sequel::Model.plugin :active_model
|
52
|
+
::Sequel::Model.plugin :validation_helpers
|
53
|
+
|
54
|
+
::Sequel::Model.raise_on_save_failure = false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Rails
|
2
|
+
module Sequel
|
3
|
+
module Railties
|
4
|
+
|
5
|
+
module ControllerRuntime
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
attr_internal :db_runtime
|
11
|
+
|
12
|
+
def cleanup_view_runtime
|
13
|
+
db_rt_before_render = ::Sequel::Model.db.reset_runtime
|
14
|
+
runtime = super
|
15
|
+
db_rt_after_render = ::Sequel::Model.db.reset_runtime
|
16
|
+
self.db_runtime = db_rt_before_render + db_rt_after_render
|
17
|
+
runtime - db_rt_after_render
|
18
|
+
end
|
19
|
+
|
20
|
+
def append_info_to_payload (payload)
|
21
|
+
super
|
22
|
+
payload[:db_runtime] = db_runtime
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def log_process_action (payload)
|
27
|
+
messages, db_runtime = super, payload[:db_runtime]
|
28
|
+
messages << ("DB: %.1fms" % db_runtime.to_f) if db_runtime
|
29
|
+
messages
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# dont spit out SELECT messages
|
2
|
+
# maybe there's a better way to do this?
|
3
|
+
class PickyLogger < Logger
|
4
|
+
def add (severity, message = nil, progname = nil, &block)
|
5
|
+
# using progname because that's where the levels put the message
|
6
|
+
return if progname =~ /SELECT/
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :db do
|
12
|
+
def migrations_path
|
13
|
+
File.join(RAILS_ROOT, 'db', 'migrate')
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Create the database, load the schema, and initialize with the seed data'
|
17
|
+
task :setup => ['db:create', 'db:schema:load', 'db:seed']
|
18
|
+
|
19
|
+
desc 'Load the seed data from db/seeds.rb'
|
20
|
+
task :seed => :environment do
|
21
|
+
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
|
22
|
+
load(seed_file) if File.exist?(seed_file)
|
23
|
+
end
|
24
|
+
|
25
|
+
namespace :create do
|
26
|
+
task :all do
|
27
|
+
puts 'Pending implementation'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
task :create do
|
32
|
+
puts 'Pending implementation'
|
33
|
+
end
|
34
|
+
|
35
|
+
namespace :migrate do
|
36
|
+
task :sequel_migration => :environment do
|
37
|
+
Sequel.extension :migration
|
38
|
+
# outputs statements to screen also
|
39
|
+
Sequel::Model.db.loggers << PickyLogger.new(STDOUT)
|
40
|
+
end
|
41
|
+
|
42
|
+
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x.'
|
43
|
+
task :redo => [ 'db:migrate:down', 'db:migrate' ]
|
44
|
+
|
45
|
+
desc 'Migrate up. Alias to db:migrate.'
|
46
|
+
task :up => 'db:migrate'
|
47
|
+
|
48
|
+
desc 'Migrate down. Define either VERSION or STEP. VERSION takes precedence if both are defined. STEP=1 if neither are defined'
|
49
|
+
task :down, :needs => :sequel_migration do
|
50
|
+
if ENV['VERSION'] then
|
51
|
+
version = ENV['VERSION'].to_i
|
52
|
+
else
|
53
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
54
|
+
version = Sequel::Migrator.get_current_migration_version(Sequel::Model.db) - step
|
55
|
+
end
|
56
|
+
|
57
|
+
Sequel::Migrator.apply(Sequel::Model.db, migrations_path, version)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
desc "Retrieves the current schema version number."
|
62
|
+
task :version, :needs => 'migrate:sequel_migration' do
|
63
|
+
puts "Current version: #{Sequel::Migrator.get_current_migration_version(Sequel::Model.db)}"
|
64
|
+
end
|
65
|
+
|
66
|
+
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x."
|
67
|
+
task :migrate, :needs => 'migrate:sequel_migration' do
|
68
|
+
if ENV['VERSION']
|
69
|
+
Sequel::Migrator.apply(Sequel::Model.db, migrations_path, ENV['VERSION'].to_i)
|
70
|
+
else
|
71
|
+
Sequel::Migrator.apply(Sequel::Model.db, migrations_path)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n. This is an alias to db:migrate:down.'
|
76
|
+
task :rollback => 'db:migrate:down'
|
77
|
+
|
78
|
+
|
79
|
+
namespace :schema do
|
80
|
+
desc "Create a db/schema.rb file that can be portably used against any DB supported by Sequel."
|
81
|
+
task :dump => :environment do
|
82
|
+
Sequel.extension :schema_dumper
|
83
|
+
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
|
84
|
+
file.puts Sequel::Model.db.dump_schema_migration
|
85
|
+
end
|
86
|
+
|
87
|
+
# needs also away to store current schema version
|
88
|
+
end
|
89
|
+
|
90
|
+
desc "Load a schema.rb file into the database."
|
91
|
+
task :load => :environment do
|
92
|
+
file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
|
93
|
+
if File.exists?(file) then
|
94
|
+
load(file)
|
95
|
+
# probably needs to also run "up: here
|
96
|
+
else
|
97
|
+
abort "#{file} doesn't exist."
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
namespace :test do
|
103
|
+
desc "Recreate the test database from the current schema.rb"
|
104
|
+
task :load => 'db:test:purge' do
|
105
|
+
Sequel.connect(Rails::Sequel::Database.configurations['test'])
|
106
|
+
Rake::Task['db:schema:load'].invoke
|
107
|
+
end
|
108
|
+
|
109
|
+
desc 'Empty the test database'
|
110
|
+
task :purge => :environment do
|
111
|
+
raise NotImplementedError
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Rails
|
2
|
+
module Sequel
|
3
|
+
module Railties
|
4
|
+
class LogSubscriber < Rails::LogSubscriber
|
5
|
+
def sql(event)
|
6
|
+
name = '%s (%.1fms)' % [event.payload[:name], event.payload[:duration]]
|
7
|
+
sql = event.payload[:sql].squeeze(' ')
|
8
|
+
|
9
|
+
if odd?
|
10
|
+
name = color(name, :cyan, true)
|
11
|
+
sql = color(sql, nil, true)
|
12
|
+
else
|
13
|
+
name = color(name, :magenta, true)
|
14
|
+
end
|
15
|
+
|
16
|
+
warn " #{name} #{sql}"
|
17
|
+
end
|
18
|
+
|
19
|
+
# by default, Rails.logger will be used
|
20
|
+
# if users want additional loggers, they can specify through the yml file
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def odd?
|
25
|
+
@odd_or_even = !@odd_or_even
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rails3_sequel/railtie'
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails3_sequel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Rachot Moragraan
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-19 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: sequel
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 11
|
30
|
+
- 0
|
31
|
+
version: 3.11.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rails
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 3
|
43
|
+
- 0
|
44
|
+
- 0
|
45
|
+
- beta3
|
46
|
+
version: 3.0.0.beta3
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Rails 3 integration with Sequel
|
50
|
+
email: janechii@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- lib/rails/generators/sequel/observer/templates/observer.rb
|
59
|
+
- lib/rails/generators/sequel/observer/observer_generator.rb
|
60
|
+
- lib/rails/generators/sequel/model/model_generator.rb
|
61
|
+
- lib/rails/generators/sequel/model/templates/migration.rb
|
62
|
+
- lib/rails/generators/sequel/model/templates/model.rb
|
63
|
+
- lib/rails/generators/sequel/active_model.rb
|
64
|
+
- lib/rails/generators/sequel/migration/templates/migration.rb
|
65
|
+
- lib/rails/generators/sequel/migration/migration_generator.rb
|
66
|
+
- lib/rails/generators/sequel/generated_attribute.rb
|
67
|
+
- lib/rails/generators/sequel.rb
|
68
|
+
- lib/rails3_sequel/railtie.rb
|
69
|
+
- lib/rails3_sequel/database.rb
|
70
|
+
- lib/rails3_sequel/logging.rb
|
71
|
+
- lib/rails3_sequel/railties/controller_runtime.rb
|
72
|
+
- lib/rails3_sequel/railties/log_subscriber.rb
|
73
|
+
- lib/rails3_sequel.rb
|
74
|
+
- lib/rails3_sequel/railties/database.rake
|
75
|
+
- README.md
|
76
|
+
- VERSION
|
77
|
+
- LICENSE
|
78
|
+
has_rdoc: true
|
79
|
+
homepage: http://github.com/mooman/rails3_sequel
|
80
|
+
licenses: []
|
81
|
+
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.6
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Rails 3 integration with Sequel
|
108
|
+
test_files: []
|
109
|
+
|