rails3_sequel 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,16 +1,17 @@
1
1
  Rails 3 Sequel integration
2
2
  ==========================
3
3
 
4
- *What works so far:*
4
+ Features:
5
5
 
6
6
  + Generators
7
- - Models
8
- - Migrations
9
- - Observers
10
- - Scaffolding Controllers - Sequel specific methods
7
+ - Models - models and migrations
8
+ - Migrations - for table alters
9
+ - Scaffold
10
+ - Controller uses Sequel specific methods.
11
+ - Views recognize migration data types.
11
12
 
12
13
  + Rake tasks
13
- - mostly everything except anything that has to do with database creation (db:create, test:prepare, etc)
14
+ - mostly everything except db:test:prepare, db:setup, and db:create:all (should be ready next version)
14
15
 
15
16
  + Railties
16
17
  - uses database.yml configuration
@@ -26,18 +27,20 @@ Rails 3 Sequel integration
26
27
  + More testing
27
28
  + i18n
28
29
  + Session Store
30
+ + Observers
29
31
  + more rake tasks
32
+ + adapter specific after_create proc
30
33
 
31
34
  Installation
32
35
  ------------
33
36
 
34
- Build from the gem spec:
37
+ gem install rails3_sequel
35
38
 
36
- gem build rails3_sequel.gemspec
39
+ OR, in your Gemfile
37
40
 
38
- Install:
41
+ gem 'rails3_sequel'
39
42
 
40
- gem install rails3_sequel-x.x.x.gem
43
+ then run bundle install.
41
44
 
42
45
  Usage - Railties
43
46
  ----------------
@@ -64,30 +67,82 @@ Config options:
64
67
  config.loggers << Logger.new('test.log')
65
68
 
66
69
  # shortcut to log_warn_duration in Sequel
70
+ # you can also set this option in database.yml
67
71
  config.log_warn_duration
68
72
 
69
73
  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
74
 
71
- Rake tasks usage.... todo
75
+ Rake tasks usage:
76
+
77
+ db:create
78
+ Creates the database defined in your Rails environment. Unlike AR, this does not create test database with your development. You must specify your Rails environment manually.
79
+ ex. RAILS_ENV=test rake db:create
80
+ db:migrate
81
+ You know what this does.
82
+ db:migrate:up
83
+ Alias to db:migrate.
84
+ db:migrate:down
85
+ Define either VERSION or STEP. VERSION takes precedence if both are defined. STEP=1 if neither are defined.
86
+ db:migrate:redo
87
+ Migrates down 1 version, then runs db:migrate.
88
+ db:migrate:rollback
89
+ Alias to db:migrate:down. Can use VERSION and STEP also.
90
+ db:schema:dump
91
+ Uses Sequel's schema_dumper. Stores output in db/schema.rb.
92
+ db:schema:load
93
+ Does not work yet, but you can use just run the migrator on the schema file.
94
+ db:seed
95
+ Load the seed data from db/seeds.rb
96
+ db:version
97
+ Shows the current migration version
98
+ db:setup, db:test:load, db:test:purge
99
+ Not implemented yet
100
+
101
+ Please note that db:create currently only works with PostgreSQL, MySQL, and SQLite. If you have other DBs, please contribute if you can!
102
+
72
103
 
73
104
  Usage - Generators
74
105
  ------------------
75
106
 
76
107
  Basics:
77
108
 
109
+ rails g [scaffold, model, migration] <name> <field_name:data_type[:primary_key?]> [...]
110
+
111
+ Example:
112
+
78
113
  rails g scaffold cat name:String:pk specie:String:pk age:Integer
79
114
 
80
- Will use name and specie as composite primary key.
115
+ Will use name and specie as composite primary keys. Data types are as specified in Sequel's documentation, which means if you use Ruby's classes, Sequel will try to convert it for your particular database, otherwise, it will take the type as is. With that said, there are 2 special types that are not Ruby classes (mainly to help out with view scaffolding):
116
+
117
+ Boolean - will use a TrueClass in your migration and a checkbox in your view.
118
+ Text - will use a String with the :text option set to true and a text_area in your view.
119
+
120
+ Example:
121
+
122
+ rails g scaffold cat name:String:pk description:Text ugly:Boolean location:geocode
81
123
 
124
+ Note that the "location" field's type will not be translated and geocode will be used as the type in the database.
82
125
 
83
- Generator options (set in config/application.rb):
126
+
127
+ Generator options (set in config/application.rb for defaults):
84
128
 
85
129
  config.generators do |g|
86
130
  g.orm :sequel, :autoincrement => true, :migration => true, :timestamps => false
131
+ ...
132
+ end
133
+
134
+ The above will always generate migration files, with autoincrement/serial field named "id", but no automatic timstamp fields updated_at or created_at. Defaults are :autoincrement => false, :migration => :true, :timestamps => false. In the commandline, you can override these on a case-by-case basis.
135
+
136
+ Example:
137
+
138
+ rails g model dog name:String specie:String --autoincrement
139
+
140
+
141
+ BUGS / ISSUES / QUESTIONS
142
+ -------------------------
87
143
 
88
- The above will always generate migration files, with autoincrement/serial field named "id", but no automatic timstamp fields updated_at or created_at.
144
+ Please feel free to email me with any issues or message me on github. janechii at gmail.
89
145
 
90
- more to come...
91
146
 
92
147
  License
93
148
  -------
@@ -98,3 +153,4 @@ Credits
98
153
  -------
99
154
 
100
155
  Based partially on rails_sequel by Piotr Usewicz: http://github.com/pusewicz/rails_sequel
156
+ Thanks to ActiveRecord's and dm-rails' railties
@@ -1,7 +1,7 @@
1
1
  module Rails
2
2
  module Sequel
3
3
  module Database
4
- mattr_reader :configurations
4
+ mattr_reader :configurations, :db
5
5
 
6
6
  def self.configurations= (config)
7
7
  @@configurations = config
@@ -16,10 +16,107 @@ module Rails
16
16
  end
17
17
  end
18
18
 
19
- # Connects to database using constructed Database Connection URI
20
- def self.connect
21
- ::Sequel.connect(self.configurations[Rails.env])
19
+ # Connects to database
20
+ def self.connect (options = {})
21
+ @@db = ::Sequel.connect(self.configurations[Rails.env].merge(options))
22
22
  end
23
+
24
+ def self.create_database (options = {})
25
+ adapter.new.create_database(self.configurations[Rails.env]['database'], options)
26
+ end
27
+
28
+ def self.drop_database
29
+ adapter.new.drop_database(self.configurations[Rails.env]['database'])
30
+ end
31
+
32
+ class << self
33
+ private
34
+
35
+ def adapter
36
+ a = configurations[Rails.env]['adapter'].camelize.to_sym
37
+
38
+ unless const_defined?(a)
39
+ raise "Adapter #{a} not supported."
40
+ end
41
+
42
+ const_get(a)
43
+ end
44
+ end
45
+
46
+ class Postgres
47
+ def initialize
48
+ @db = Rails::Sequel::Database.connect({ :database => 'postgres', :after_connect => (
49
+ proc do |conn|
50
+ conn.execute('SET search_path = public')
51
+ end
52
+ )
53
+ })
54
+ end
55
+
56
+ # from ActiveRecord
57
+ def create_database (name, options = {})
58
+ options = options.reverse_merge(:encoding => "utf8")
59
+
60
+ option_string = options.sum do |key, value|
61
+ case key
62
+ when 'owner'
63
+ " OWNER = \"#{value}\""
64
+ when 'template'
65
+ " TEMPLATE = \"#{value}\""
66
+ when 'encoding'
67
+ " ENCODING = '#{value}'"
68
+ when 'tablespace'
69
+ " TABLESPACE = \"#{value}\""
70
+ when :connection_limit
71
+ " CONNECTION LIMIT = #{value}"
72
+ else
73
+ ""
74
+ end
75
+ end
76
+
77
+ # TODO: quote table name
78
+ @db.execute "CREATE DATABASE #{name} #{option_string}"
79
+ end
80
+
81
+ def drop_database (name)
82
+ begin
83
+ @db.execute "DROP DATABASE #{name}"
84
+ rescue Sequel::DatabaseError
85
+ raise 'Cannot drop database'
86
+ end
87
+ end
88
+ end
89
+
90
+ class Mysql
91
+ def initialize
92
+ Rails::Sequel::Database.connect({ :database => nil })
93
+ end
94
+
95
+ def create_database (name, options = {})
96
+ if options[:collation]
97
+ @db.execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
98
+ else
99
+ @db.execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
100
+ end
101
+ end
102
+
103
+ def drop_database (name)
104
+ @db.execute "DROP DATABASE IF EXISTS `#{name}`"
105
+ end
106
+ end
107
+
108
+ class Sqlite
109
+ def initialize
110
+ Rails::Sequel::Database.connect
111
+ end
112
+
113
+ def create_database (*args)
114
+ end
115
+
116
+ def drop_database (*args)
117
+ end
118
+ end
119
+
23
120
  end
24
121
  end
25
122
  end
@@ -9,7 +9,7 @@ module Rails
9
9
  :name => 'SQL',
10
10
  :duration => duration * 1000
11
11
  )
12
- super(duration, message)
12
+ super
13
13
  end
14
14
 
15
15
  def log_each (level, message)
@@ -24,12 +24,13 @@ namespace :db do
24
24
 
25
25
  namespace :create do
26
26
  task :all do
27
+ # cant choose which db to create yet.
27
28
  puts 'Pending implementation'
28
29
  end
29
30
  end
30
31
 
31
- task :create do
32
- puts 'Pending implementation'
32
+ task :create => :environment do
33
+ Rails::Sequel::Database.create_database
33
34
  end
34
35
 
35
36
  namespace :migrate do
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rachot Moragraan
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-19 00:00:00 -07:00
17
+ date: 2010-05-24 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency