sequel 3.34.1 → 3.35.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/CHANGELOG +52 -0
  2. data/README.rdoc +3 -1
  3. data/Rakefile +2 -10
  4. data/doc/active_record.rdoc +1 -0
  5. data/doc/migration.rdoc +18 -7
  6. data/doc/model_hooks.rdoc +6 -0
  7. data/doc/opening_databases.rdoc +3 -0
  8. data/doc/prepared_statements.rdoc +0 -1
  9. data/doc/release_notes/3.35.0.txt +144 -0
  10. data/doc/schema_modification.rdoc +16 -1
  11. data/doc/thread_safety.rdoc +17 -0
  12. data/lib/sequel/adapters/do.rb +2 -2
  13. data/lib/sequel/adapters/do/postgres.rb +1 -52
  14. data/lib/sequel/adapters/do/sqlite.rb +0 -5
  15. data/lib/sequel/adapters/firebird.rb +1 -1
  16. data/lib/sequel/adapters/ibmdb.rb +2 -2
  17. data/lib/sequel/adapters/jdbc.rb +23 -19
  18. data/lib/sequel/adapters/jdbc/db2.rb +0 -5
  19. data/lib/sequel/adapters/jdbc/derby.rb +29 -2
  20. data/lib/sequel/adapters/jdbc/firebird.rb +0 -5
  21. data/lib/sequel/adapters/jdbc/h2.rb +1 -1
  22. data/lib/sequel/adapters/jdbc/hsqldb.rb +7 -0
  23. data/lib/sequel/adapters/jdbc/informix.rb +0 -5
  24. data/lib/sequel/adapters/jdbc/jtds.rb +0 -5
  25. data/lib/sequel/adapters/jdbc/mysql.rb +0 -5
  26. data/lib/sequel/adapters/jdbc/postgresql.rb +4 -35
  27. data/lib/sequel/adapters/jdbc/sqlite.rb +0 -5
  28. data/lib/sequel/adapters/jdbc/sqlserver.rb +0 -5
  29. data/lib/sequel/adapters/jdbc/transactions.rb +4 -4
  30. data/lib/sequel/adapters/mysql2.rb +1 -1
  31. data/lib/sequel/adapters/odbc.rb +3 -3
  32. data/lib/sequel/adapters/odbc/mssql.rb +14 -1
  33. data/lib/sequel/adapters/oracle.rb +6 -18
  34. data/lib/sequel/adapters/postgres.rb +36 -53
  35. data/lib/sequel/adapters/shared/db2.rb +16 -2
  36. data/lib/sequel/adapters/shared/mssql.rb +40 -9
  37. data/lib/sequel/adapters/shared/mysql.rb +16 -4
  38. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +2 -2
  39. data/lib/sequel/adapters/shared/oracle.rb +2 -0
  40. data/lib/sequel/adapters/shared/postgres.rb +135 -211
  41. data/lib/sequel/adapters/sqlite.rb +2 -2
  42. data/lib/sequel/adapters/swift.rb +1 -1
  43. data/lib/sequel/adapters/swift/postgres.rb +1 -71
  44. data/lib/sequel/adapters/tinytds.rb +3 -3
  45. data/lib/sequel/core.rb +27 -4
  46. data/lib/sequel/database/connecting.rb +7 -8
  47. data/lib/sequel/database/logging.rb +6 -1
  48. data/lib/sequel/database/misc.rb +20 -4
  49. data/lib/sequel/database/query.rb +38 -18
  50. data/lib/sequel/database/schema_generator.rb +5 -2
  51. data/lib/sequel/database/schema_methods.rb +34 -8
  52. data/lib/sequel/dataset/prepared_statements.rb +1 -1
  53. data/lib/sequel/dataset/sql.rb +18 -24
  54. data/lib/sequel/extensions/core_extensions.rb +0 -23
  55. data/lib/sequel/extensions/migration.rb +22 -8
  56. data/lib/sequel/extensions/pg_auto_parameterize.rb +4 -0
  57. data/lib/sequel/extensions/schema_dumper.rb +1 -1
  58. data/lib/sequel/model.rb +2 -2
  59. data/lib/sequel/model/associations.rb +95 -70
  60. data/lib/sequel/model/base.rb +16 -18
  61. data/lib/sequel/plugins/dirty.rb +214 -0
  62. data/lib/sequel/plugins/identity_map.rb +1 -1
  63. data/lib/sequel/plugins/json_serializer.rb +16 -1
  64. data/lib/sequel/plugins/many_through_many.rb +22 -32
  65. data/lib/sequel/plugins/many_to_one_pk_lookup.rb +2 -2
  66. data/lib/sequel/plugins/prepared_statements.rb +22 -8
  67. data/lib/sequel/plugins/prepared_statements_associations.rb +2 -3
  68. data/lib/sequel/plugins/prepared_statements_with_pk.rb +1 -1
  69. data/lib/sequel/plugins/single_table_inheritance.rb +1 -1
  70. data/lib/sequel/plugins/subclasses.rb +10 -2
  71. data/lib/sequel/plugins/timestamps.rb +1 -1
  72. data/lib/sequel/plugins/xml_serializer.rb +12 -1
  73. data/lib/sequel/sql.rb +1 -1
  74. data/lib/sequel/version.rb +2 -2
  75. data/spec/adapters/postgres_spec.rb +30 -79
  76. data/spec/core/database_spec.rb +46 -2
  77. data/spec/core/dataset_spec.rb +28 -22
  78. data/spec/core/schema_generator_spec.rb +1 -1
  79. data/spec/core/schema_spec.rb +51 -0
  80. data/spec/extensions/arbitrary_servers_spec.rb +0 -4
  81. data/spec/extensions/association_autoreloading_spec.rb +17 -0
  82. data/spec/extensions/association_proxies_spec.rb +4 -4
  83. data/spec/extensions/core_extensions_spec.rb +1 -24
  84. data/spec/extensions/dirty_spec.rb +155 -0
  85. data/spec/extensions/json_serializer_spec.rb +13 -0
  86. data/spec/extensions/migration_spec.rb +28 -15
  87. data/spec/extensions/named_timezones_spec.rb +6 -8
  88. data/spec/extensions/pg_auto_parameterize_spec.rb +6 -5
  89. data/spec/extensions/schema_dumper_spec.rb +3 -1
  90. data/spec/extensions/xml_serializer_spec.rb +13 -0
  91. data/spec/files/{transactionless_migrations → transaction_specified_migrations}/001_create_alt_basic.rb +1 -1
  92. data/spec/files/{transactionless_migrations → transaction_specified_migrations}/002_create_basic.rb +0 -0
  93. data/spec/files/{transaction_migrations → transaction_unspecified_migrations}/001_create_alt_basic.rb +0 -0
  94. data/spec/files/{transaction_migrations → transaction_unspecified_migrations}/002_create_basic.rb +0 -0
  95. data/spec/integration/associations_test.rb +5 -7
  96. data/spec/integration/dataset_test.rb +25 -7
  97. data/spec/integration/plugin_test.rb +1 -1
  98. data/spec/integration/schema_test.rb +16 -1
  99. data/spec/model/associations_spec.rb +2 -2
  100. metadata +14 -9
  101. data/lib/sequel/adapters/odbc/db2.rb +0 -17
data/CHANGELOG CHANGED
@@ -1,3 +1,55 @@
1
+ === 3.35.0 (2012-05-01)
2
+
3
+ * Correctly handle parsing schema for tables in other databases on MySQL (jeremyevans)
4
+
5
+ * Add DSL support for the modulus operator (%), similar to the bitwise operators (jeremyevans)
6
+
7
+ * Fix possible thread-safety issues on non-GVL ruby implementations (jeremyevans)
8
+
9
+ * Allow truncation of multiple tables at the same time on PostgreSQL (jeremyevans)
10
+
11
+ * Allow truncate to take a :cascade, :only, and :restart options on PostgreSQL (hgimenez, jeremyevans)
12
+
13
+ * Allow json and xml serializers to support :array option in class to_json method to serialize existing array of model instances (jeremyevans)
14
+
15
+ * Add dirty plugin, which saves the initial value of the column when the value is changed (jeremyevans)
16
+
17
+ * create_table now supports an :as option to create a table directly from the results of a query (jeremyevans)
18
+
19
+ * The :index option when creating columns in the schema generator can now be a hash of options passed to index (jeremyevans)
20
+
21
+ * Parsing the default column values in the oracle adapter no longer requires superuser privileges (Jason Hines)
22
+
23
+ * Add Database#cache_schema to allow schema caching to be turned of, useful for development modes where models are reloaded (jeremyevans)
24
+
25
+ * Correctly handle errors that occur when rolling back transactions (jeremyevans)
26
+
27
+ * Recognize identity type in the schema dumper (jeremyevans) (#468)
28
+
29
+ * Don't assign instance variables to Java objects, for future JRuby 2.0 support (jeremyevans) (#466)
30
+
31
+ * Use date and timestamp formats that are multilanguage and not DATEFORMAT dependent on Microsoft SQL Server (jeremyevans)
32
+
33
+ * Add Database#log_exception, which logs when a query raises an exception, for easier overriding (jeremyevans) (#465)
34
+
35
+ * Make the migrators only use transactions by default if the database supports transactional DDL (jeremyevans)
36
+
37
+ * Add Database#supports_transactional_ddl? for checking if DDL statements can be rolled back in transactions (jeremyevans)
38
+
39
+ * Don't use auto parameterization when using cursors in the pg_auto_parameterize extension (jeremyevans) (#463)
40
+
41
+ * No longer escape backslashes in strings by default, fixes doubled backslashes on some adapters (jeremyevans)
42
+
43
+ * Escape blackslash-carriage return-line feed in strings on Microsoft SQL Server (mluu, jeremyevans) (#462, #461)
44
+
45
+ * Remove Array#all_two_pairs? (jeremyevans)
46
+
47
+ * Remove Dataset#disable_insert_returning on PostgreSQL (jeremyevans)
48
+
49
+ * Remove support for PostgreSQL <8.2 (jeremyevans)
50
+
51
+ * Remove support for Ruby <1.8.7 (jeremyevans)
52
+
1
53
  === 3.34.1 (2012-04-02)
2
54
 
3
55
  * Fix bug in optimization of primary key lookup (jeremyevans) (#460)
data/README.rdoc CHANGED
@@ -64,12 +64,14 @@ If you have any comments or suggestions please post to the Google group.
64
64
 
65
65
  == The Sequel Console
66
66
 
67
- Sequel includes an IRB console for quick access to databases. You can use it like this:
67
+ Sequel includes an IRB console for quick access to databases (usually referred to as <tt>bin/sequel</tt>). You can use it like this:
68
68
 
69
69
  sequel sqlite://test.db # test.db in current directory
70
70
 
71
71
  You get an IRB session with the database object stored in DB.
72
72
 
73
+ In addition to providing an IRB shell (the default behavior), bin/sequel also has support for migrating databases (-m and -M), dumping schema migrations (-d and -D), and copying databases (-C).
74
+
73
75
  == An Introduction
74
76
 
75
77
  Sequel is designed to take the hassle away from connecting to databases and manipulating them. Sequel deals with all the boring stuff like maintaining connections, formatting SQL correctly and fetching records so you can concentrate on your application.
data/Rakefile CHANGED
@@ -118,23 +118,15 @@ begin
118
118
  end
119
119
  end
120
120
 
121
- sc = spec_with_cov = lambda do |name, files, d|
121
+ spec_with_cov = lambda do |name, files, d, &b|
122
122
  spec.call(name, files, d)
123
123
  t = spec.call("#{name}_cov", files, "#{d} with coverage")
124
124
  t.rcov = true
125
125
  t.rcov_opts = File.read("spec/rcov.opts").split("\n")
126
+ b.call(t) if b
126
127
  t
127
128
  end
128
129
 
129
- if RUBY_VERSION >= '1.8.7'
130
- eval <<-END
131
- spec_with_cov = lambda do |*x, &b|
132
- t = sc.call(*x)
133
- b.call(t) if b
134
- end
135
- END
136
- end
137
-
138
130
  task :default => [:spec]
139
131
  spec_with_cov.call("spec", Dir["spec/{core,model}/*_spec.rb"], "Run core and model specs"){|t| t.rcov_opts.concat(%w'--exclude "lib/sequel/adapters/([a-ln-z]|m[a-np-z])"')}
140
132
  spec.call("spec_core", Dir["spec/core/*_spec.rb"], "Run core specs")
@@ -694,6 +694,7 @@ ActiveRecord Method :: Sequel Method
694
694
  +set_primary_key+ :: +set_primary_key+
695
695
  +sum+ :: +sum+
696
696
  +table_name+ :: +table_name+
697
+ +unescoped+ :: +unfiltered+
697
698
 
698
699
  === Class Methods without an Equivalent
699
700
 
data/doc/migration.rdoc CHANGED
@@ -173,9 +173,10 @@ which just executes the code on the underlying database.
173
173
 
174
174
  == Errors when running migrations
175
175
 
176
- Sequel attempts to run migrations inside of a transaction. Some databases do not support
177
- schema modifications made in transactions, and if the migration raises an error, it will
178
- not rollback the previous schema changes made by the migration. In that case, you will
176
+ Sequel attempts to run migrations inside of a transaction if the database supports
177
+ transactional DDL statements. On the databases that don't support transactional DDL
178
+ statements, if there is an error while running a migration, it will not rollback the
179
+ previous schema changes made by the migration. In that case, you will
179
180
  need to update the database by hand.
180
181
 
181
182
  It's recommended to always run migrations on a test database and ensure they work
@@ -183,9 +184,18 @@ before running them on any production database.
183
184
 
184
185
  == Transactions
185
186
 
186
- As mentioned above, Sequel attempts to run migrations inside of a transaction by default.
187
- However, you can disable this on a per migration basis by calling +no_transaction+ inside
188
- of the Sequel.migration block:
187
+ You can manually specify to use transactions on a per migration basis. For example,
188
+ if you want to force transaction use for a particular migration, call the transaction
189
+ method in the Sequel.migration block:
190
+
191
+ Sequel.migration do
192
+ transaction
193
+ change do
194
+ # ...
195
+ end
196
+ end
197
+
198
+ Likewise, you can disable transaction use via no_transaction:
189
199
 
190
200
  Sequel.migration do
191
201
  no_transaction
@@ -195,7 +205,8 @@ of the Sequel.migration block:
195
205
  end
196
206
 
197
207
  This is necessary in some cases, such as when attempting to use CREATE INDEX CONCURRENTLY
198
- on PostgreSQL.
208
+ on PostgreSQL (which supports transactional schema, but not that statement inside a
209
+ transaction).
199
210
 
200
211
  You can also override the transactions setting at the migrator level, either by forcing
201
212
  transactions even if no_transaction is set, or by disabling transactions all together:
data/doc/model_hooks.rdoc CHANGED
@@ -69,6 +69,12 @@ Note that these hooks are only called when using <tt>Model#destroy</tt>, they ar
69
69
 
70
70
  <tt>Sequel::Model</tt> does support a few additional hooks. One is +after_intialize+, which is called after the model object has been initalized. It can be used to set default attribute values for new objects, since by default new <tt>Sequel::Model</tt> objects have no attributes, and the attributes are not filled in until the model object is saved. You should be careful when you are using +after_initialize+, since it is called for every created record. So if you run a query that returns 1000 model objects, it will be called 1000 times. If you only want to change the behavior for new records, you can override the +initialize_set+ private method, which is called with the hash passed to +initialize+.
71
71
 
72
+ == Special Hook-Related Instance Variables
73
+
74
+ For after_save hooks, a @was_new instance variable is present that indicates whether the record was a new record that was just inserted, or an existing record that was updated. Sequel marks a record as existing as soon as it inserts the record, so in an after_save or after_create hook, the instance is no longer considered new. You have to check @was_new to see if the record was inserted. This exists so that you don't have to have separate after_create and after_update hooks that are mostly the same and only differ slightly depending on whether the record was a new record.
75
+
76
+ For after_update hooks, a @columns_updated instance variable is present that is a hash of the values used to update the row (keys are column symbols, values are column values). This should be used by any code that wants to check what columns and values were used during the update. You can't just check the current values of the instance, since Sequel offers ways to manually specify which columns to use during the save.
77
+
72
78
  == Transaction-related Hooks
73
79
 
74
80
  There are four other model hooks that <tt>Sequel::Model</tt> supports, all related to transactions. These are +after_commit+, +after_rollback+, +after_destroy_commit+, and +after_destroy_rollback+. +after_commit+ is called after the transaction in which you saved the object commits, only if it commits. +after_rollback+ is called after the transaction in which you saved the object rolls back, if it rolls back. +after_destroy_commit+ is called after the transaction in which you destroyed the object commits, if it commits. +after_destroy_rollback+ is called after the transaction in which you destroyed the object rolls back, if it rolls back.
@@ -338,6 +338,9 @@ Requires: pg (or postgres if pg is not available)
338
338
 
339
339
  The Sequel postgres adapter works with the pg, postgres, and postgres-pr ruby libraries.
340
340
  The pg library is the best supported, as it supports real bound variables and prepared statements.
341
+ If the pg library is being used, Sequel will also attempt to load the sequel_pg library, which is
342
+ a C extension that optimizes performance when Sequel is used with pg. All users of Sequel who
343
+ use pg are encouraged to install sequel_pg.
341
344
 
342
345
  The following additional options are supported:
343
346
 
@@ -82,7 +82,6 @@ and update queries, the hash to insert/update is passed to +prepare+:
82
82
  If you are using the ruby-postgres or postgres-pr driver, PostgreSQL uses the
83
83
  default emulated support. If you are using ruby-pg, there is native support,
84
84
  but it may require type specifiers on some old versions (generally not anymore).
85
- direct control over the SQL string, but since Sequel abstracts that, the types
86
85
  You can add a __* suffix to the placeholder symbol to specify a type, which
87
86
  casts to that type in the SQL (e.g. :$name__text, which will be compiled to
88
87
  "$1::text" in the SQL). Prepared statements are always server side.
@@ -0,0 +1,144 @@
1
+ = New Features
2
+
3
+ * A dirty plugin has been added, which saves the initial value of
4
+ the column when the column is changed, similar to
5
+ ActiveModel::Dirty:
6
+
7
+ artist.name # => 'Foo'
8
+ artist.name = 'Bar'
9
+ artist.initial_value(:name) # 'Foo'
10
+ artist.column_change(:name) # ['Foo', 'Bar']
11
+ artist.column_changes # {:name => ['Foo', 'Bar']}
12
+ artist.column_changed?(:name) # true
13
+ artist.reset_column(:name)
14
+ artist.name # => 'Foo'
15
+ artist.column_changed?(:name) # false
16
+ artist.update(:name=>'Bar')
17
+ artist.column_changes # => {}
18
+ artist.previous_changes # => {:name=>['Foo', 'Bar']}
19
+
20
+ * Database#create_table now respects an :as option to create a
21
+ database based on the results of a query. The :as option value
22
+ should either be an SQL string or a dataset.
23
+
24
+ DB.create_table(:new_foos, :as=>DB[:foos].where(:new=>true))
25
+
26
+ * The json_serializer and xml_serializer plugins can now serialize
27
+ arbitrary arrays of model objects by passing an :array option
28
+ to the to_json class method. This works around an issue in
29
+ ruby's JSON library where Array#to_json does not pass arguments
30
+ given to it to the members of the array.
31
+
32
+ Artist.to_json(:array=>[Artist[1]], :include=>:albums)
33
+
34
+ * You can now use the % (modulus) operator in the same way you
35
+ can use the bitwise operators in Sequel:
36
+
37
+ :column.sql_number % 1 # (column % 1)
38
+
39
+ * On PostgreSQL, you can now provide :only, :cascade, and :restart
40
+ options to Dataset#truncate to use ONLY, CASCADE, and
41
+ RESTART IDENTITY. Additionally, you can now truncate multiple
42
+ tables at the same time:
43
+
44
+ DB.from(:table1, :table2).truncate(:cascade=>true)
45
+
46
+ * The :index option when creating columns in the schema generator
47
+ can now take a hash of index options:
48
+
49
+ DB.create_table(:foo){Integer :bar, :index=>{:unique=>true}}
50
+
51
+ * A Database#cache_schema accessor has been added, it can be set
52
+ to false to have the Database never cache schema results. This
53
+ can be useful in Rails development mode, so that you don't need to
54
+ restart a running server to have models pick up the new schema.
55
+
56
+ * Database#log_exception has been added for easier instrumentation.
57
+ It is called with the exception and SQL query string for all
58
+ queries that raise an exception.
59
+
60
+ * The Sequel.migration DSL now has a transaction method that forces
61
+ transaction use for the given migration.
62
+
63
+ = Other Improvements
64
+
65
+ * Many theoretical thread-safety issues have been fixed for ruby
66
+ implementations that don't use a global interpreter lock.
67
+ Previously, Sequel relied on MRI's global interpreter lock for
68
+ part of its thread safety, now it does manually locking in more
69
+ places to avoid thread-safety issues on JRuby (and other ruby
70
+ implementations without a global interpreter lock).
71
+
72
+ No Sequel user ever reported a production error related to the
73
+ previous thread-safety issues, and most of the issues fixed
74
+ were so difficult to hit that even tests specifically designed
75
+ to raise errors were unable to do so.
76
+
77
+ * Sequel.single_threaded = true now disables the mutex
78
+ synchronization that enforces thread safety for additional
79
+ performance in single threaded mode.
80
+
81
+ * Sequel's migrators now only attempt to use transactions by
82
+ default if the underlying database supports transactional DDL.
83
+ SQLite does support transactional DDL, but Sequel will not
84
+ use transactions for SQLite migrations as it causes issues
85
+ when emulating alter_table operations for tables with foreign
86
+ keys.
87
+
88
+ * Errors that occur when rolling back database transactions are
89
+ now handled correctly. Previously, the underlying exception was
90
+ raised, it wasn't correctly wrapped in a Sequel::DatabaseError,
91
+ and if it was due to a database disconnection, the connection
92
+ wasn't removed from the pool.
93
+
94
+ * Sequel no longer sets ruby instance variables on java objects,
95
+ fixing warnings on JRuby 1.7 and attempting to be forward
96
+ compatible with JRuby 2.0.
97
+
98
+ * Sequel now uses date and timestamp formats that are multilanguage
99
+ and not DATEFORMAT dependent on Microsoft SQL Server.
100
+
101
+ * Sequel now correctly escapes blackslash-carriage return-line feed
102
+ on Microsoft SQL Server.
103
+
104
+ * Parsing the column default values in the oracle adapter no longer
105
+ requires database superuser privileges.
106
+
107
+ * Sequel now correctly handles parsing schema for tables in other
108
+ databases on MySQL. Previously, it would always look in the
109
+ current database.
110
+
111
+ * Sequel no longer doubles backslashes in strings by default. It
112
+ now only does so on MySQL, since that is the only database that
113
+ appears to use backslashes for escaping. This fixes issues with
114
+ backslashes being doubled on some of the less commonly used
115
+ adapters.
116
+
117
+ * The pg_auto_parameterize extension now works correctly when
118
+ using cursors.
119
+
120
+ * Dataset#truncate now raises an Error if you attempt to do so
121
+ on a dataset that uses HAVING. Previously, it only checked for
122
+ WHERE.
123
+
124
+ * The schema dumper now recognized the identity type.
125
+
126
+ = Backwards Compatibility
127
+
128
+ * Association reflections now store cached information in a
129
+ separate subhash due to the thread-safety changes. Any code
130
+ accessing an association reflection should always call the
131
+ related method to get the cached data instead of checking
132
+ for a specific location in the hash.
133
+
134
+ * Association reflection internals for many_through_many associations
135
+ changed significantly, any code that accesses the edge information
136
+ in the reflection will need to be changed to use the new methods
137
+ instead of accessing the old values directly.
138
+
139
+ * The features deprecated in 3.34.0 have now been removed:
140
+ * Ruby <1.8.7 support
141
+ * PostgreSQL <8.2 support
142
+ * Dataset#disable_insert_returning on PostgreSQL
143
+ * Array#all_two_pairs? and #sql_expr_if_all_two_pairs
144
+
@@ -80,7 +80,8 @@ When using the type name as method, the third argument is an options hash, and w
80
80
  method, the fourth argument is the options hash. The following options are supported:
81
81
 
82
82
  :default :: The default value for the column.
83
- :index :: Create an index on this column.
83
+ :index :: Create an index on this column. If given a hash, use the hash as the
84
+ options for the index.
84
85
  :null :: Mark the column as allowing NULL values (if true),
85
86
  or not allowing NULL values (if false). If unspecified, will default
86
87
  to whatever the database default is.
@@ -175,6 +176,13 @@ column, but create a foreign key constraint:
175
176
  index :id
176
177
  end
177
178
 
179
+ create_table(:a){Integer :id, :index=>{:unique=>true}}
180
+ # Same as:
181
+ create_table(:a) do
182
+ Integer :id
183
+ index :id, :unique=>true
184
+ end
185
+
178
186
  Similar to the +primary_key+ and +foreign_key+ methods, calling +index+ with an array of symbols
179
187
  will create a multiple column index:
180
188
 
@@ -269,6 +277,13 @@ which expands to:
269
277
  index [:artist_id, :album_id]
270
278
  end
271
279
 
280
+ == <tt>create_table :as=></tt>
281
+
282
+ To create a table from the result of a SELECT query, instead of passing a block
283
+ to +create_table+, provide a dataset to the :as option:
284
+
285
+ create_table(:older_items, :as=>DB[:items].where{updated_at < Date.today << 6})
286
+
272
287
  == +alter_table+
273
288
 
274
289
  +alter_table+ is used to alter existing tables, changing their columns, indexes,
@@ -0,0 +1,17 @@
1
+ = Thread Safety
2
+
3
+ Most Sequel usage (and all common Sequel usage) is thread safe by default. Specifically, multiple threads can operate on Database instances, Dataset instances, and Model classes concurrently without problems. In general, Database instance and Model classes are not modified after application startup, and modifying Dataset instances returns modified copies of the dataset instead of mutating it.
4
+
5
+ == Connection Pool
6
+
7
+ In order to allow multiple threads to operate on the same database at the same time, Sequel uses a connection pool. The connection pool is designed so that a thread uses a connection for the minimum amount of time, returning the connection to the pool as soon as it is done using the connection. If a thread requests a connection and the pool is empty, the thread will block (actually busy-wait) until a connection is available or the the connection pool timeout has elapsed (in which case a PoolTimeout error will be raised).
8
+
9
+ == Exceptions
10
+
11
+ This is a small list of things that are specifically non thread-safe. This is not an exhaustive list, there may be cases not mentioned here.
12
+
13
+ 1) Model instances: Model instances are not thread-safe unless they are frozen first. Multiple threads should not operate on an unfrozen model instance concurrently.
14
+
15
+ 2) Model class modifications: Model class modifications, such as adding associations and loading plugins, are not designed to be thread safe. You should not modify a class in one thread if any other thread can concurrently access it. Model subclassing is designed to be thread-safe, so you create a model subclass in a thread and modify it safely.
16
+
17
+ 3) Dataset mutation methods: Dataset mutation methods are not thread safe, you should not call them on datasets that could be accessed by other threads. It is safe to clone the dataset first inside a thread and call mutation methods on the cloned dataset.
@@ -17,7 +17,7 @@ module Sequel
17
17
  Sequel.tsk_require 'do_postgres'
18
18
  Sequel.ts_require 'adapters/do/postgres'
19
19
  db.extend(Sequel::DataObjects::Postgres::DatabaseMethods)
20
- db.dataset_class = Sequel::DataObjects::Postgres::Dataset
20
+ db.extend_datasets Sequel::Postgres::DatasetMethods
21
21
  end,
22
22
  :mysql=>proc do |db|
23
23
  Sequel.tsk_require 'do_mysql'
@@ -29,7 +29,7 @@ module Sequel
29
29
  Sequel.tsk_require 'do_sqlite3'
30
30
  Sequel.ts_require 'adapters/do/sqlite'
31
31
  db.extend(Sequel::DataObjects::SQLite::DatabaseMethods)
32
- db.dataset_class = Sequel::DataObjects::SQLite::Dataset
32
+ db.extend_datasets Sequel::SQLite::DatasetMethods
33
33
  db.set_integer_booleans
34
34
  end
35
35
  }
@@ -7,40 +7,6 @@ module Sequel
7
7
  # Adapter, Database, and Dataset support for accessing a PostgreSQL
8
8
  # database via DataObjects.
9
9
  module Postgres
10
- # Methods to add to the DataObjects adapter/connection to allow it to work
11
- # with the shared PostgreSQL code.
12
- module AdapterMethods
13
- include Sequel::Postgres::AdapterMethods
14
-
15
- # Give the DataObjects adapter a direct execute method, which creates
16
- # a statement with the given sql and executes it.
17
- def execute(sql, args=nil)
18
- command = create_command(sql)
19
- begin
20
- if block_given?
21
- begin
22
- yield(reader = @db.log_yield(sql){command.execute_reader})
23
- ensure
24
- reader.close if reader
25
- end
26
- else
27
- @db.log_yield(sql){command.execute_non_query}
28
- end
29
- rescue ::DataObjects::Error => e
30
- @db.send(:raise_error, e)
31
- end
32
- end
33
-
34
- private
35
-
36
- # DataObjects specific method of getting specific values from a result set.
37
- def single_value(reader)
38
- while(reader.next!) do
39
- return reader.values.at(0)
40
- end
41
- end
42
- end
43
-
44
10
  # Methods to add to Database instances that access PostgreSQL via
45
11
  # DataObjects.
46
12
  module DatabaseMethods
@@ -55,32 +21,15 @@ module Sequel
55
21
  end
56
22
  end
57
23
 
58
- # Run the INSERT sql on the database and return the primary key
59
- # for the record.
60
- def execute_insert(sql, opts={})
61
- synchronize(opts[:server]) do |conn|
62
- com = conn.create_command(sql)
63
- log_yield(sql){com.execute_non_query}
64
- insert_result(conn, opts[:table], opts[:values])
65
- end
66
- end
67
-
68
24
  private
69
25
 
70
26
  # Extend the adapter with the DataObjects PostgreSQL AdapterMethods
71
27
  def setup_connection(conn)
72
28
  conn = super(conn)
73
- conn.extend(Sequel::DataObjects::Postgres::AdapterMethods)
74
- conn.db = self
75
- conn.apply_connection_settings
29
+ connection_configuration_sqls.each{|sql| log_yield(sql){conn.create_command(sql).execute_non_query}}
76
30
  conn
77
31
  end
78
32
  end
79
-
80
- # Dataset subclass used for datasets that connect to PostgreSQL via DataObjects.
81
- class Dataset < DataObjects::Dataset
82
- include Sequel::Postgres::DatasetMethods
83
- end
84
33
  end
85
34
  end
86
35
  end