rails3_sequel 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ === HEAD
2
+
3
+ * Can now do adapter specific database configurations. Specifically this addressed Postgres schema_search_path option.
4
+ * Create database in Postgres will also create schema.
5
+ * MySql and SQLite adapters
6
+ * Rake tasks db:create:all, db:drop:all, db:drop
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  Rails 3 Sequel integration
2
2
  ==========================
3
3
 
4
+ **There has been some confusions with how the logging works. if you are using Rails 3 beta 3, please use the released gem or the beta3 branch on github. if you are using edge Rails 3, then please use this master branch**
5
+
4
6
  Features:
5
7
 
6
8
  + Generators
@@ -11,7 +13,6 @@ Features:
11
13
  - Views recognize migration data types.
12
14
 
13
15
  + Rake tasks
14
- - mostly everything except db:test:prepare, db:setup, and db:create:all (should be ready next version)
15
16
 
16
17
  + Railties
17
18
  - uses database.yml configuration
@@ -24,12 +25,13 @@ Features:
24
25
 
25
26
  *What is still need done:*
26
27
 
27
- + More testing
28
+ + Write tests
28
29
  + i18n
29
30
  + Session Store
30
31
  + Observers
31
32
  + more rake tasks
32
- + adapter specific after_create proc
33
+ + adapter specific encoding / charset options
34
+ + namespaced config (config.sequel.xxxx)
33
35
 
34
36
  Installation
35
37
  ------------
@@ -42,6 +44,8 @@ OR, in your Gemfile
42
44
 
43
45
  then run bundle install.
44
46
 
47
+ Please see the note at the top of this README for what version you should use.
48
+
45
49
  Usage - Railties
46
50
  ----------------
47
51
 
@@ -76,7 +80,9 @@ Rake tasks usage:
76
80
 
77
81
  db:create
78
82
  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
83
+ ex. rake db:create[test] or RAILS_ENV=test rake db:create
84
+ db:create:all
85
+ Does the above for all environments
80
86
  db:migrate
81
87
  You know what this does.
82
88
  db:migrate:up
@@ -90,13 +96,18 @@ Rake tasks usage:
90
96
  db:schema:dump
91
97
  Uses Sequel's schema_dumper. Stores output in db/schema.rb.
92
98
  db:schema:load
93
- Does not work yet, but you can just run Sequel::Migrator on the schema file.
99
+ Uses Sequel's migration. Reads from db/schema.rb.
94
100
  db:seed
95
- Load the seed data from db/seeds.rb
101
+ Load the seed data from db/seeds.rb.
96
102
  db:version
97
- Shows the current migration version
98
- db:setup, db:test:load, db:test:purge
99
- Not implemented yet
103
+ Shows the current migration version.
104
+ db:setup
105
+ Create the database, load the schema, and initialize with the seed data.
106
+ db:test:load
107
+ Recreate the test database from the current schema.rb.
108
+ db:test:purge
109
+ Empty the test database.
110
+
100
111
 
101
112
  Please note that db:create currently only works with PostgreSQL, MySQL, and SQLite. If you have other DBs, please contribute if you can!
102
113
 
@@ -0,0 +1,41 @@
1
+ module Rails
2
+ module Sequel
3
+ class Database
4
+
5
+ class Mysql
6
+ def initialize (env)
7
+ @env = env
8
+ @config = Database.configurations[@env]
9
+ end
10
+
11
+ def connect (options = {})
12
+ ::Sequel.connect(@config.merge(options))
13
+ end
14
+
15
+ def create_database (options = {})
16
+ db = management_connect
17
+ name = @config['database']
18
+
19
+ if options[:collation]
20
+ db.execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
21
+ else
22
+ db.execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
23
+ end
24
+ end
25
+
26
+ def drop_database
27
+ db = management_connect
28
+ name = @config['database']
29
+ db.execute "DROP DATABASE IF EXISTS `#{name}`"
30
+ end
31
+
32
+ private
33
+
34
+ def management_connect
35
+ connect('database' => nil)
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,89 @@
1
+ module Rails
2
+ module Sequel
3
+ class Database
4
+
5
+ class Postgres
6
+ def initialize (env)
7
+ @env = env
8
+ @config = parse_special_options(Database.configurations[@env])
9
+ end
10
+
11
+ def connect (options = {})
12
+ options = parse_special_options(options)
13
+ ::Sequel.connect(@config.merge(options))
14
+ end
15
+
16
+ # from ActiveRecord
17
+ def create_database (options = {})
18
+ db = management_connect
19
+ name = @config['database']
20
+
21
+ if name.nil? or name.strip == '' then
22
+ raise "No valid database specified for #{@env} environment"
23
+ end
24
+
25
+ options = options.reverse_merge(:encoding => "utf8")
26
+
27
+ option_string = options.sum do |key, value|
28
+ case key
29
+ when 'owner'
30
+ " OWNER = \"#{value}\""
31
+ when 'template'
32
+ " TEMPLATE = \"#{value}\""
33
+ when 'encoding'
34
+ " ENCODING = '#{value}'"
35
+ when 'tablespace'
36
+ " TABLESPACE = \"#{value}\""
37
+ when :connection_limit
38
+ " CONNECTION LIMIT = #{value}"
39
+ else
40
+ ""
41
+ end
42
+ end
43
+
44
+ # TODO: quote table name
45
+ db.execute "CREATE DATABASE #{name} #{option_string}"
46
+
47
+ # create schema too, if the previous command succeeds, this should be fine
48
+ # first connect without schema specified
49
+ db = connect('schema_search_path' => nil)
50
+ for schema in @config['schema_search_path'].split(',') do
51
+ next if schema.strip == 'public'
52
+ db.execute "CREATE SCHEMA #{schema}"
53
+ end
54
+ end
55
+
56
+ def drop_database
57
+ db = management_connect
58
+ name = @config['database']
59
+
60
+ db.execute "DROP DATABASE #{name}"
61
+ end
62
+
63
+ private
64
+
65
+ def management_connect
66
+ connect({ 'database' => 'postgres', 'schema_search_path' => 'public' })
67
+ end
68
+
69
+ def parse_special_options (opts)
70
+ # not a deep dup
71
+ options = opts.dup
72
+
73
+ if options['schema_search_path'].nil? then
74
+ options['after_connect'] = nil
75
+ else
76
+ options['after_connect'] = (
77
+ proc do |conn|
78
+ conn.execute("SET search_path = #{options['schema_search_path']}")
79
+ end
80
+ )
81
+ end
82
+
83
+ return options
84
+ end
85
+ end
86
+
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,28 @@
1
+ module Rails
2
+ module Sequel
3
+ class Database
4
+
5
+ class Sqlite
6
+ def initialize (env)
7
+ @env = env
8
+ @config = Database.configurations[@env]
9
+ end
10
+
11
+ def connect (options = {})
12
+ ::Sequel.connect(@config.merge(options))
13
+ end
14
+
15
+ def create_database (*args)
16
+ connect
17
+ puts 'Warning: sqlite file may not have been created until there are some operations on it'
18
+ end
19
+
20
+ def drop_database (*args)
21
+ dbfile = @config['database']
22
+ File.delete(dbfile) if File.exists?(dbfile)
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -1,119 +1,73 @@
1
1
  module Rails
2
2
  module Sequel
3
- module Database
4
- mattr_reader :configurations, :db
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
20
- def self.connect (options = {})
21
- @@db = ::Sequel.connect(self.configurations[Rails.env].merge(options))
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
3
+ class Database
31
4
 
32
5
  class << self
33
- private
6
+ attr_reader :configurations
7
+
8
+ def configurations= (config)
9
+ @configurations = config
10
+
11
+ for key,env in @configurations do
12
+ # some translations
13
+ env['adapter'] = case env['adapter']
14
+ when 'postgresql' then 'postgres'
15
+ when 'sqlite3' then 'sqlite'
16
+ else env['adapter']
17
+ end
18
+ end
19
+ end
34
20
 
35
- def adapter
36
- a = configurations[Rails.env]['adapter'].camelize.to_sym
21
+ def adapter(env)
22
+ a = configurations[env]['adapter']
23
+ a_file = File.join(File.dirname(__FILE__), 'adapters', "#{a}.rb")
37
24
 
38
- unless const_defined?(a)
25
+ unless File.exists?(a_file)
39
26
  raise "Adapter #{a} not supported."
40
27
  end
41
28
 
42
- const_get(a)
29
+ load a_file
30
+ const_get(a.camelize.to_sym).new(env)
43
31
  end
44
- end
45
32
 
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
- })
33
+ # convenient methods
34
+
35
+ def connect (env, options = {})
36
+ adapter(env).connect(options)
54
37
  end
55
38
 
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}"
39
+ def create_database (env, options = {})
40
+ local_database?(env) { adapter(env).create_database(options) }
79
41
  end
80
42
 
81
- def drop_database (name)
82
- begin
83
- @db.execute "DROP DATABASE #{name}"
84
- rescue Sequel::DatabaseError
85
- raise 'Cannot drop database'
43
+ def create_all (options = {})
44
+ for env,config in configurations do
45
+ next unless config['database']
46
+ create_database(env, options)
86
47
  end
87
48
  end
88
- end
89
-
90
- class Mysql
91
- def initialize
92
- Rails::Sequel::Database.connect({ :database => nil })
93
- end
94
49
 
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
50
+ def drop_database (env)
51
+ local_database?(env) { adapter(env).drop_database }
101
52
  end
102
53
 
103
- def drop_database (name)
104
- @db.execute "DROP DATABASE IF EXISTS `#{name}`"
54
+ def drop_all
55
+ for env,config in configurations do
56
+ next unless config['database']
57
+ drop_database(env)
58
+ end
105
59
  end
106
- end
107
60
 
108
- class Sqlite
109
- def initialize
110
- Rails::Sequel::Database.connect
111
- end
61
+ private
112
62
 
113
- def create_database (*args)
114
- end
63
+ def local_database? (env, &block)
64
+ config = configurations[env]
115
65
 
116
- def drop_database (*args)
66
+ if %w( 127.0.0.1 localhost ).include?(config['host']) || config['host'].blank?
67
+ yield
68
+ else
69
+ $stderr.puts "This task only modifies local databases. #{config['database']} is on a remote host."
70
+ end
117
71
  end
118
72
  end
119
73
 
@@ -4,7 +4,7 @@ module Rails
4
4
  def log_duration (duration, message)
5
5
  @controller_runtime ||= 0
6
6
  @controller_runtime += duration
7
- ActiveSupport::Notifications.instrument('sql.sequel',
7
+ ActiveSupport::Notifications.instrument('sequel.sql',
8
8
  :sql => message,
9
9
  :name => 'SQL',
10
10
  :duration => duration * 1000
@@ -30,7 +30,7 @@ module Rails
30
30
 
31
31
  initializer 'sequel.initialize_database' do |app|
32
32
  Rails::Sequel::Database.configurations = app.config.database_configuration
33
- Rails::Sequel::Database.connect
33
+ Rails::Sequel::Database.connect(Rails.env)
34
34
  end
35
35
 
36
36
  initializer 'sequel.logging' do |app|
@@ -23,14 +23,28 @@ namespace :db do
23
23
  end
24
24
 
25
25
  namespace :create do
26
- task :all do
27
- # cant choose which db to create yet.
28
- puts 'Pending implementation'
26
+ task :all => :environment do
27
+ Rails::Sequel::Database.create_all
28
+ end
29
+ end
30
+
31
+ desc '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.'
32
+ task :create, :env, :needs => :environment do |t, args|
33
+ args.with_defaults(:env => Rails.env)
34
+ Rails::Sequel::Database.create_database(args.env)
35
+ end
36
+
37
+ namespace :drop do
38
+ task :all => :environment do
39
+ Rails::Sequel::Database.drop_all
29
40
  end
30
41
  end
31
42
 
32
- task :create => :environment do
33
- Rails::Sequel::Database.create_database
43
+ desc 'Opposite of db:create'
44
+ task :drop, :env, :needs => :environment do |t, args|
45
+ args.with_defaults(:env => Rails.env)
46
+ # TODO: what happens if database doesn't exist?
47
+ Rails::Sequel::Database.drop_database(args.env)
34
48
  end
35
49
 
36
50
  namespace :migrate do
@@ -85,15 +99,18 @@ namespace :db do
85
99
  file.puts Sequel::Model.db.dump_schema_migration
86
100
  end
87
101
 
88
- # needs also away to store current schema version
102
+ # TODO: needs also away to store current schema version
89
103
  end
90
104
 
91
105
  desc "Load a schema.rb file into the database."
92
- task :load => :environment do
106
+ task :load, :db, :needs => :environment do |t, args|
107
+ args.with_defaults(:db => Sequel::Model.db)
108
+
93
109
  file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
94
110
  if File.exists?(file) then
95
- load(file)
96
- # probably needs to also run "up: here
111
+ Sequel.extension :migration
112
+ schema_migration = eval(File.read(file))
113
+ schema_migration.apply(args.db, :up)
97
114
  else
98
115
  abort "#{file} doesn't exist."
99
116
  end
@@ -103,14 +120,20 @@ namespace :db do
103
120
  namespace :test do
104
121
  desc "Recreate the test database from the current schema.rb"
105
122
  task :load => 'db:test:purge' do
106
- Sequel.connect(Rails::Sequel::Database.configurations['test'])
107
- Rake::Task['db:schema:load'].invoke
123
+ db = Rails::Sequel::Database.connect('test')
124
+ Rake::Task['db:schema:load'].invoke(db)
108
125
  end
109
126
 
127
+ desc 'Runs db:test:load'
128
+ task :prepare => :load
129
+
110
130
  desc 'Empty the test database'
111
131
  task :purge => :environment do
112
- raise NotImplementedError
132
+ Rake::Task['db:drop'].invoke('test')
133
+ Rake::Task['db:create'].invoke('test')
113
134
  end
114
135
  end
115
136
 
116
137
  end
138
+
139
+ task 'test:prepare' => 'db:test:prepare'
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails3_sequel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Rachot Moragraan
@@ -15,18 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-05-29 00:00:00 -07:00
17
+ date: 2010-06-13 00:00:00 -07:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: sequel
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
25
  - - ">="
28
26
  - !ruby/object:Gem::Version
29
- hash: 43
30
27
  segments:
31
28
  - 3
32
29
  - 11
@@ -38,11 +35,9 @@ dependencies:
38
35
  name: rails
39
36
  prerelease: false
40
37
  requirement: &id002 !ruby/object:Gem::Requirement
41
- none: false
42
38
  requirements:
43
39
  - - ">="
44
40
  - !ruby/object:Gem::Version
45
- hash: 222005035
46
41
  segments:
47
42
  - 3
48
43
  - 0
@@ -70,6 +65,9 @@ files:
70
65
  - lib/rails/generators/sequel/migration/migration_generator.rb
71
66
  - lib/rails/generators/sequel/generated_attribute.rb
72
67
  - lib/rails/generators/sequel.rb
68
+ - lib/rails3_sequel/adapters/mysql.rb
69
+ - lib/rails3_sequel/adapters/sqlite.rb
70
+ - lib/rails3_sequel/adapters/postgres.rb
73
71
  - lib/rails3_sequel/railtie.rb
74
72
  - lib/rails3_sequel/database.rb
75
73
  - lib/rails3_sequel/logging.rb
@@ -77,6 +75,7 @@ files:
77
75
  - lib/rails3_sequel/railties/log_subscriber.rb
78
76
  - lib/rails3_sequel.rb
79
77
  - lib/rails3_sequel/railties/database.rake
78
+ - CHANGELOG
80
79
  - README.md
81
80
  - VERSION
82
81
  - LICENSE
@@ -90,27 +89,23 @@ rdoc_options: []
90
89
  require_paths:
91
90
  - lib
92
91
  required_ruby_version: !ruby/object:Gem::Requirement
93
- none: false
94
92
  requirements:
95
93
  - - ">="
96
94
  - !ruby/object:Gem::Version
97
- hash: 3
98
95
  segments:
99
96
  - 0
100
97
  version: "0"
101
98
  required_rubygems_version: !ruby/object:Gem::Requirement
102
- none: false
103
99
  requirements:
104
100
  - - ">="
105
101
  - !ruby/object:Gem::Version
106
- hash: 3
107
102
  segments:
108
103
  - 0
109
104
  version: "0"
110
105
  requirements: []
111
106
 
112
107
  rubyforge_project:
113
- rubygems_version: 1.3.7
108
+ rubygems_version: 1.3.6
114
109
  signing_key:
115
110
  specification_version: 3
116
111
  summary: Rails 3 integration with Sequel