viking-sequel 3.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3134 -0
- data/COPYING +19 -0
- data/README.rdoc +723 -0
- data/Rakefile +193 -0
- data/bin/sequel +196 -0
- data/doc/advanced_associations.rdoc +644 -0
- data/doc/cheat_sheet.rdoc +218 -0
- data/doc/dataset_basics.rdoc +106 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/opening_databases.rdoc +296 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/reflection.rdoc +84 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.11.0.txt +215 -0
- data/doc/release_notes/2.12.0.txt +534 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/release_notes/3.0.0.txt +221 -0
- data/doc/release_notes/3.1.0.txt +406 -0
- data/doc/release_notes/3.10.0.txt +286 -0
- data/doc/release_notes/3.2.0.txt +268 -0
- data/doc/release_notes/3.3.0.txt +192 -0
- data/doc/release_notes/3.4.0.txt +325 -0
- data/doc/release_notes/3.5.0.txt +510 -0
- data/doc/release_notes/3.6.0.txt +366 -0
- data/doc/release_notes/3.7.0.txt +179 -0
- data/doc/release_notes/3.8.0.txt +151 -0
- data/doc/release_notes/3.9.0.txt +233 -0
- data/doc/schema.rdoc +36 -0
- data/doc/sharding.rdoc +113 -0
- data/doc/virtual_rows.rdoc +205 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel/adapters/ado.rb +90 -0
- data/lib/sequel/adapters/ado/mssql.rb +30 -0
- data/lib/sequel/adapters/amalgalite.rb +176 -0
- data/lib/sequel/adapters/db2.rb +139 -0
- data/lib/sequel/adapters/dbi.rb +113 -0
- data/lib/sequel/adapters/do.rb +188 -0
- data/lib/sequel/adapters/do/mysql.rb +49 -0
- data/lib/sequel/adapters/do/postgres.rb +91 -0
- data/lib/sequel/adapters/do/sqlite.rb +40 -0
- data/lib/sequel/adapters/firebird.rb +283 -0
- data/lib/sequel/adapters/informix.rb +77 -0
- data/lib/sequel/adapters/jdbc.rb +587 -0
- data/lib/sequel/adapters/jdbc/as400.rb +58 -0
- data/lib/sequel/adapters/jdbc/h2.rb +133 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +57 -0
- data/lib/sequel/adapters/jdbc/mysql.rb +78 -0
- data/lib/sequel/adapters/jdbc/oracle.rb +50 -0
- data/lib/sequel/adapters/jdbc/postgresql.rb +108 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +55 -0
- data/lib/sequel/adapters/mysql.rb +421 -0
- data/lib/sequel/adapters/odbc.rb +143 -0
- data/lib/sequel/adapters/odbc/mssql.rb +42 -0
- data/lib/sequel/adapters/openbase.rb +64 -0
- data/lib/sequel/adapters/oracle.rb +131 -0
- data/lib/sequel/adapters/postgres.rb +504 -0
- data/lib/sequel/adapters/shared/mssql.rb +490 -0
- data/lib/sequel/adapters/shared/mysql.rb +498 -0
- data/lib/sequel/adapters/shared/oracle.rb +195 -0
- data/lib/sequel/adapters/shared/postgres.rb +830 -0
- data/lib/sequel/adapters/shared/progress.rb +44 -0
- data/lib/sequel/adapters/shared/sqlite.rb +389 -0
- data/lib/sequel/adapters/sqlite.rb +224 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +84 -0
- data/lib/sequel/connection_pool.rb +99 -0
- data/lib/sequel/connection_pool/sharded_single.rb +84 -0
- data/lib/sequel/connection_pool/sharded_threaded.rb +211 -0
- data/lib/sequel/connection_pool/single.rb +29 -0
- data/lib/sequel/connection_pool/threaded.rb +150 -0
- data/lib/sequel/core.rb +293 -0
- data/lib/sequel/core_sql.rb +241 -0
- data/lib/sequel/database.rb +1079 -0
- data/lib/sequel/database/schema_generator.rb +327 -0
- data/lib/sequel/database/schema_methods.rb +203 -0
- data/lib/sequel/database/schema_sql.rb +320 -0
- data/lib/sequel/dataset.rb +32 -0
- data/lib/sequel/dataset/actions.rb +441 -0
- data/lib/sequel/dataset/features.rb +86 -0
- data/lib/sequel/dataset/graph.rb +254 -0
- data/lib/sequel/dataset/misc.rb +119 -0
- data/lib/sequel/dataset/mutation.rb +64 -0
- data/lib/sequel/dataset/prepared_statements.rb +227 -0
- data/lib/sequel/dataset/query.rb +709 -0
- data/lib/sequel/dataset/sql.rb +996 -0
- data/lib/sequel/exceptions.rb +51 -0
- data/lib/sequel/extensions/blank.rb +43 -0
- data/lib/sequel/extensions/inflector.rb +242 -0
- data/lib/sequel/extensions/looser_typecasting.rb +21 -0
- data/lib/sequel/extensions/migration.rb +239 -0
- data/lib/sequel/extensions/named_timezones.rb +61 -0
- data/lib/sequel/extensions/pagination.rb +100 -0
- data/lib/sequel/extensions/pretty_table.rb +82 -0
- data/lib/sequel/extensions/query.rb +52 -0
- data/lib/sequel/extensions/schema_dumper.rb +271 -0
- data/lib/sequel/extensions/sql_expr.rb +122 -0
- data/lib/sequel/extensions/string_date_time.rb +46 -0
- data/lib/sequel/extensions/thread_local_timezones.rb +48 -0
- data/lib/sequel/metaprogramming.rb +9 -0
- data/lib/sequel/model.rb +120 -0
- data/lib/sequel/model/associations.rb +1514 -0
- data/lib/sequel/model/base.rb +1069 -0
- data/lib/sequel/model/default_inflections.rb +45 -0
- data/lib/sequel/model/errors.rb +39 -0
- data/lib/sequel/model/exceptions.rb +21 -0
- data/lib/sequel/model/inflections.rb +162 -0
- data/lib/sequel/model/plugins.rb +70 -0
- data/lib/sequel/plugins/active_model.rb +59 -0
- data/lib/sequel/plugins/association_dependencies.rb +103 -0
- data/lib/sequel/plugins/association_proxies.rb +41 -0
- data/lib/sequel/plugins/boolean_readers.rb +53 -0
- data/lib/sequel/plugins/caching.rb +141 -0
- data/lib/sequel/plugins/class_table_inheritance.rb +214 -0
- data/lib/sequel/plugins/composition.rb +138 -0
- data/lib/sequel/plugins/force_encoding.rb +72 -0
- data/lib/sequel/plugins/hook_class_methods.rb +126 -0
- data/lib/sequel/plugins/identity_map.rb +116 -0
- data/lib/sequel/plugins/instance_filters.rb +98 -0
- data/lib/sequel/plugins/instance_hooks.rb +57 -0
- data/lib/sequel/plugins/lazy_attributes.rb +77 -0
- data/lib/sequel/plugins/many_through_many.rb +208 -0
- data/lib/sequel/plugins/nested_attributes.rb +206 -0
- data/lib/sequel/plugins/optimistic_locking.rb +81 -0
- data/lib/sequel/plugins/rcte_tree.rb +281 -0
- data/lib/sequel/plugins/schema.rb +66 -0
- data/lib/sequel/plugins/serialization.rb +166 -0
- data/lib/sequel/plugins/single_table_inheritance.rb +74 -0
- data/lib/sequel/plugins/subclasses.rb +45 -0
- data/lib/sequel/plugins/tactical_eager_loading.rb +61 -0
- data/lib/sequel/plugins/timestamps.rb +87 -0
- data/lib/sequel/plugins/touch.rb +118 -0
- data/lib/sequel/plugins/typecast_on_load.rb +72 -0
- data/lib/sequel/plugins/validation_class_methods.rb +405 -0
- data/lib/sequel/plugins/validation_helpers.rb +223 -0
- data/lib/sequel/sql.rb +1020 -0
- data/lib/sequel/timezones.rb +161 -0
- data/lib/sequel/version.rb +12 -0
- data/lib/sequel_core.rb +1 -0
- data/lib/sequel_model.rb +1 -0
- data/spec/adapters/firebird_spec.rb +407 -0
- data/spec/adapters/informix_spec.rb +97 -0
- data/spec/adapters/mssql_spec.rb +403 -0
- data/spec/adapters/mysql_spec.rb +1019 -0
- data/spec/adapters/oracle_spec.rb +286 -0
- data/spec/adapters/postgres_spec.rb +969 -0
- data/spec/adapters/spec_helper.rb +51 -0
- data/spec/adapters/sqlite_spec.rb +432 -0
- data/spec/core/connection_pool_spec.rb +808 -0
- data/spec/core/core_sql_spec.rb +417 -0
- data/spec/core/database_spec.rb +1662 -0
- data/spec/core/dataset_spec.rb +3827 -0
- data/spec/core/expression_filters_spec.rb +595 -0
- data/spec/core/object_graph_spec.rb +296 -0
- data/spec/core/schema_generator_spec.rb +159 -0
- data/spec/core/schema_spec.rb +830 -0
- data/spec/core/spec_helper.rb +56 -0
- data/spec/core/version_spec.rb +7 -0
- data/spec/extensions/active_model_spec.rb +76 -0
- data/spec/extensions/association_dependencies_spec.rb +127 -0
- data/spec/extensions/association_proxies_spec.rb +50 -0
- data/spec/extensions/blank_spec.rb +67 -0
- data/spec/extensions/boolean_readers_spec.rb +92 -0
- data/spec/extensions/caching_spec.rb +250 -0
- data/spec/extensions/class_table_inheritance_spec.rb +252 -0
- data/spec/extensions/composition_spec.rb +194 -0
- data/spec/extensions/force_encoding_spec.rb +117 -0
- data/spec/extensions/hook_class_methods_spec.rb +470 -0
- data/spec/extensions/identity_map_spec.rb +202 -0
- data/spec/extensions/inflector_spec.rb +181 -0
- data/spec/extensions/instance_filters_spec.rb +55 -0
- data/spec/extensions/instance_hooks_spec.rb +133 -0
- data/spec/extensions/lazy_attributes_spec.rb +153 -0
- data/spec/extensions/looser_typecasting_spec.rb +39 -0
- data/spec/extensions/many_through_many_spec.rb +884 -0
- data/spec/extensions/migration_spec.rb +332 -0
- data/spec/extensions/named_timezones_spec.rb +72 -0
- data/spec/extensions/nested_attributes_spec.rb +396 -0
- data/spec/extensions/optimistic_locking_spec.rb +100 -0
- data/spec/extensions/pagination_spec.rb +99 -0
- data/spec/extensions/pretty_table_spec.rb +91 -0
- data/spec/extensions/query_spec.rb +85 -0
- data/spec/extensions/rcte_tree_spec.rb +205 -0
- data/spec/extensions/schema_dumper_spec.rb +357 -0
- data/spec/extensions/schema_spec.rb +127 -0
- data/spec/extensions/serialization_spec.rb +209 -0
- data/spec/extensions/single_table_inheritance_spec.rb +96 -0
- data/spec/extensions/spec_helper.rb +91 -0
- data/spec/extensions/sql_expr_spec.rb +89 -0
- data/spec/extensions/string_date_time_spec.rb +93 -0
- data/spec/extensions/subclasses_spec.rb +52 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +65 -0
- data/spec/extensions/thread_local_timezones_spec.rb +45 -0
- data/spec/extensions/timestamps_spec.rb +150 -0
- data/spec/extensions/touch_spec.rb +155 -0
- data/spec/extensions/typecast_on_load_spec.rb +69 -0
- data/spec/extensions/validation_class_methods_spec.rb +984 -0
- data/spec/extensions/validation_helpers_spec.rb +438 -0
- data/spec/integration/associations_test.rb +281 -0
- data/spec/integration/database_test.rb +26 -0
- data/spec/integration/dataset_test.rb +963 -0
- data/spec/integration/eager_loader_test.rb +734 -0
- data/spec/integration/model_test.rb +130 -0
- data/spec/integration/plugin_test.rb +814 -0
- data/spec/integration/prepared_statement_test.rb +213 -0
- data/spec/integration/schema_test.rb +361 -0
- data/spec/integration/spec_helper.rb +73 -0
- data/spec/integration/timezone_test.rb +55 -0
- data/spec/integration/transaction_test.rb +122 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/model/association_reflection_spec.rb +175 -0
- data/spec/model/associations_spec.rb +2633 -0
- data/spec/model/base_spec.rb +418 -0
- data/spec/model/dataset_methods_spec.rb +78 -0
- data/spec/model/eager_loading_spec.rb +1391 -0
- data/spec/model/hooks_spec.rb +240 -0
- data/spec/model/inflector_spec.rb +26 -0
- data/spec/model/model_spec.rb +593 -0
- data/spec/model/plugins_spec.rb +236 -0
- data/spec/model/record_spec.rb +1500 -0
- data/spec/model/spec_helper.rb +97 -0
- data/spec/model/validations_spec.rb +153 -0
- data/spec/rcov.opts +6 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +346 -0
@@ -0,0 +1,830 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
context "DB#create_table" do
|
4
|
+
before do
|
5
|
+
@db = SchemaDummyDatabase.new
|
6
|
+
end
|
7
|
+
|
8
|
+
specify "should accept the table name" do
|
9
|
+
@db.create_table(:cats) {}
|
10
|
+
@db.sqls.should == ['CREATE TABLE cats ()']
|
11
|
+
end
|
12
|
+
|
13
|
+
specify "should accept the table name in multiple formats" do
|
14
|
+
@db.create_table(:cats__cats) {}
|
15
|
+
@db.create_table("cats__cats1") {}
|
16
|
+
@db.create_table(:cats__cats2.identifier) {}
|
17
|
+
@db.create_table(:cats.qualify(:cats3)) {}
|
18
|
+
@db.sqls.should == ['CREATE TABLE cats.cats ()', 'CREATE TABLE cats__cats1 ()', 'CREATE TABLE cats__cats2 ()', 'CREATE TABLE cats3.cats ()']
|
19
|
+
end
|
20
|
+
|
21
|
+
specify "should raise an error if the table name argument is not valid" do
|
22
|
+
proc{@db.create_table(1) {}}.should raise_error(Sequel::Error)
|
23
|
+
proc{@db.create_table(:cats.as(:c)) {}}.should raise_error(Sequel::Error)
|
24
|
+
end
|
25
|
+
|
26
|
+
specify "should accept multiple columns" do
|
27
|
+
@db.create_table(:cats) do
|
28
|
+
column :id, :integer
|
29
|
+
column :name, :text
|
30
|
+
end
|
31
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer, name text)']
|
32
|
+
end
|
33
|
+
|
34
|
+
specify "should accept method calls as data types" do
|
35
|
+
@db.create_table(:cats) do
|
36
|
+
integer :id
|
37
|
+
text :name
|
38
|
+
end
|
39
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer, name text)']
|
40
|
+
end
|
41
|
+
|
42
|
+
specify "should transform types given as ruby classes to database-specific types" do
|
43
|
+
@db.create_table(:cats) do
|
44
|
+
String :a
|
45
|
+
Integer :b
|
46
|
+
Fixnum :c
|
47
|
+
Bignum :d
|
48
|
+
Float :e
|
49
|
+
BigDecimal :f
|
50
|
+
Date :g
|
51
|
+
DateTime :h
|
52
|
+
Time :i
|
53
|
+
Numeric :j
|
54
|
+
File :k
|
55
|
+
TrueClass :l
|
56
|
+
FalseClass :m
|
57
|
+
column :n, Fixnum
|
58
|
+
primary_key :o, :type=>String
|
59
|
+
foreign_key :p, :f, :type=>Date
|
60
|
+
end
|
61
|
+
@db.sqls.should == ['CREATE TABLE cats (o varchar(255) PRIMARY KEY AUTOINCREMENT, a varchar(255), b integer, c integer, d bigint, e double precision, f numeric, g date, h timestamp, i timestamp, j numeric, k blob, l boolean, m boolean, n integer, p date REFERENCES f)']
|
62
|
+
end
|
63
|
+
|
64
|
+
specify "should allow the use of modifiers with ruby class types" do
|
65
|
+
@db.create_table(:cats) do
|
66
|
+
String :a, :size=>50
|
67
|
+
String :b, :text=>true
|
68
|
+
String :c, :fixed=>true, :size=>40
|
69
|
+
Time :d, :only_time=>true
|
70
|
+
BigDecimal :e, :size=>[11,2]
|
71
|
+
end
|
72
|
+
@db.sqls.should == ['CREATE TABLE cats (a varchar(50), b text, c char(40), d time, e numeric(11, 2))']
|
73
|
+
end
|
74
|
+
|
75
|
+
specify "should raise an error if you use a ruby class that isn't handled" do
|
76
|
+
proc{@db.create_table(:cats){column :a, Class}}.should raise_error(Sequel::Error)
|
77
|
+
end
|
78
|
+
|
79
|
+
specify "should accept primary key definition" do
|
80
|
+
@db.create_table(:cats) do
|
81
|
+
primary_key :id
|
82
|
+
end
|
83
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT)']
|
84
|
+
|
85
|
+
@db.sqls.clear
|
86
|
+
@db.create_table(:cats) do
|
87
|
+
primary_key :id, :serial, :auto_increment => false
|
88
|
+
end
|
89
|
+
@db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
90
|
+
|
91
|
+
@db.sqls.clear
|
92
|
+
@db.create_table(:cats) do
|
93
|
+
primary_key :id, :type => :serial, :auto_increment => false
|
94
|
+
end
|
95
|
+
@db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)']
|
96
|
+
end
|
97
|
+
|
98
|
+
specify "should accept and literalize default values" do
|
99
|
+
@db.create_table(:cats) do
|
100
|
+
integer :id, :default => 123
|
101
|
+
text :name, :default => "abc'def"
|
102
|
+
end
|
103
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer DEFAULT 123, name text DEFAULT 'abc''def')"]
|
104
|
+
end
|
105
|
+
|
106
|
+
specify "should accept not null definition" do
|
107
|
+
@db.create_table(:cats) do
|
108
|
+
integer :id
|
109
|
+
text :name, :null => false
|
110
|
+
text :name2, :allow_null => false
|
111
|
+
end
|
112
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer, name text NOT NULL, name2 text NOT NULL)"]
|
113
|
+
end
|
114
|
+
|
115
|
+
specify "should accept null definition" do
|
116
|
+
@db.create_table(:cats) do
|
117
|
+
integer :id
|
118
|
+
text :name, :null => true
|
119
|
+
text :name2, :allow_null => true
|
120
|
+
end
|
121
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer, name text NULL, name2 text NULL)"]
|
122
|
+
end
|
123
|
+
|
124
|
+
specify "should accept unique definition" do
|
125
|
+
@db.create_table(:cats) do
|
126
|
+
integer :id
|
127
|
+
text :name, :unique => true
|
128
|
+
end
|
129
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer, name text UNIQUE)"]
|
130
|
+
end
|
131
|
+
|
132
|
+
specify "should accept unsigned definition" do
|
133
|
+
@db.create_table(:cats) do
|
134
|
+
integer :value, :unsigned => true
|
135
|
+
end
|
136
|
+
@db.sqls.should == ["CREATE TABLE cats (value integer UNSIGNED)"]
|
137
|
+
end
|
138
|
+
|
139
|
+
specify "should accept [SET|ENUM](...) types" do
|
140
|
+
@db.create_table(:cats) do
|
141
|
+
set :color, :elements => ['black', 'tricolor', 'grey']
|
142
|
+
end
|
143
|
+
@db.sqls.should == ["CREATE TABLE cats (color set('black', 'tricolor', 'grey'))"]
|
144
|
+
end
|
145
|
+
|
146
|
+
specify "should accept varchar size" do
|
147
|
+
@db.create_table(:cats) do
|
148
|
+
varchar :name
|
149
|
+
end
|
150
|
+
@db.sqls.should == ["CREATE TABLE cats (name varchar(255))"]
|
151
|
+
@db.sqls.clear
|
152
|
+
@db.create_table(:cats) do
|
153
|
+
varchar :name, :size => 51
|
154
|
+
end
|
155
|
+
@db.sqls.should == ["CREATE TABLE cats (name varchar(51))"]
|
156
|
+
end
|
157
|
+
|
158
|
+
specify "should use double precision for double type" do
|
159
|
+
@db.create_table(:cats) do
|
160
|
+
double :name
|
161
|
+
end
|
162
|
+
@db.sqls.should == ["CREATE TABLE cats (name double precision)"]
|
163
|
+
end
|
164
|
+
|
165
|
+
specify "should accept foreign keys without options" do
|
166
|
+
@db.create_table(:cats) do
|
167
|
+
foreign_key :project_id
|
168
|
+
end
|
169
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer)"]
|
170
|
+
end
|
171
|
+
|
172
|
+
specify "should accept foreign keys with options" do
|
173
|
+
@db.create_table(:cats) do
|
174
|
+
foreign_key :project_id, :table => :projects
|
175
|
+
end
|
176
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects)"]
|
177
|
+
end
|
178
|
+
|
179
|
+
specify "should accept foreign keys with separate table argument" do
|
180
|
+
@db.create_table(:cats) do
|
181
|
+
foreign_key :project_id, :projects, :default=>3
|
182
|
+
end
|
183
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer DEFAULT 3 REFERENCES projects)"]
|
184
|
+
end
|
185
|
+
|
186
|
+
specify "should raise an error if the table argument to foreign_key isn't a hash, symbol, or nil" do
|
187
|
+
proc{@db.create_table(:cats){foreign_key :project_id, Object.new, :default=>3}}.should raise_error(Sequel::Error)
|
188
|
+
end
|
189
|
+
|
190
|
+
specify "should accept foreign keys with arbitrary keys" do
|
191
|
+
@db.create_table(:cats) do
|
192
|
+
foreign_key :project_id, :table => :projects, :key => :id
|
193
|
+
end
|
194
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(id))"]
|
195
|
+
|
196
|
+
@db.sqls.clear
|
197
|
+
@db.create_table(:cats) do
|
198
|
+
foreign_key :project_id, :table => :projects, :key => :zzz
|
199
|
+
end
|
200
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(zzz))"]
|
201
|
+
end
|
202
|
+
|
203
|
+
specify "should accept foreign keys with ON DELETE clause" do
|
204
|
+
@db.create_table(:cats) do
|
205
|
+
foreign_key :project_id, :table => :projects, :on_delete => :restrict
|
206
|
+
end
|
207
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE RESTRICT)"]
|
208
|
+
|
209
|
+
@db.sqls.clear
|
210
|
+
@db.create_table(:cats) do
|
211
|
+
foreign_key :project_id, :table => :projects, :on_delete => :cascade
|
212
|
+
end
|
213
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)"]
|
214
|
+
|
215
|
+
@db.sqls.clear
|
216
|
+
@db.create_table(:cats) do
|
217
|
+
foreign_key :project_id, :table => :projects, :on_delete => :no_action
|
218
|
+
end
|
219
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION)"]
|
220
|
+
@db.sqls.clear
|
221
|
+
|
222
|
+
@db.sqls.clear
|
223
|
+
@db.create_table(:cats) do
|
224
|
+
foreign_key :project_id, :table => :projects, :on_delete => :set_null
|
225
|
+
end
|
226
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET NULL)"]
|
227
|
+
@db.sqls.clear
|
228
|
+
|
229
|
+
@db.sqls.clear
|
230
|
+
@db.create_table(:cats) do
|
231
|
+
foreign_key :project_id, :table => :projects, :on_delete => :set_default
|
232
|
+
end
|
233
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET DEFAULT)"]
|
234
|
+
@db.sqls.clear
|
235
|
+
end
|
236
|
+
|
237
|
+
specify "should accept foreign keys with ON UPDATE clause" do
|
238
|
+
@db.create_table(:cats) do
|
239
|
+
foreign_key :project_id, :table => :projects, :on_update => :restrict
|
240
|
+
end
|
241
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE RESTRICT)"]
|
242
|
+
|
243
|
+
@db.sqls.clear
|
244
|
+
@db.create_table(:cats) do
|
245
|
+
foreign_key :project_id, :table => :projects, :on_update => :cascade
|
246
|
+
end
|
247
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE CASCADE)"]
|
248
|
+
|
249
|
+
@db.sqls.clear
|
250
|
+
@db.create_table(:cats) do
|
251
|
+
foreign_key :project_id, :table => :projects, :on_update => :no_action
|
252
|
+
end
|
253
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE NO ACTION)"]
|
254
|
+
@db.sqls.clear
|
255
|
+
|
256
|
+
@db.sqls.clear
|
257
|
+
@db.create_table(:cats) do
|
258
|
+
foreign_key :project_id, :table => :projects, :on_update => :set_null
|
259
|
+
end
|
260
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET NULL)"]
|
261
|
+
@db.sqls.clear
|
262
|
+
|
263
|
+
@db.sqls.clear
|
264
|
+
@db.create_table(:cats) do
|
265
|
+
foreign_key :project_id, :table => :projects, :on_update => :set_default
|
266
|
+
end
|
267
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET DEFAULT)"]
|
268
|
+
@db.sqls.clear
|
269
|
+
end
|
270
|
+
|
271
|
+
specify "should accept inline index definition" do
|
272
|
+
@db.create_table(:cats) do
|
273
|
+
integer :id, :index => true
|
274
|
+
end
|
275
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
|
276
|
+
end
|
277
|
+
|
278
|
+
specify "should accept inline index definition for foreign keys" do
|
279
|
+
@db.create_table(:cats) do
|
280
|
+
foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => true
|
281
|
+
end
|
282
|
+
@db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
|
283
|
+
"CREATE INDEX cats_project_id_index ON cats (project_id)"]
|
284
|
+
end
|
285
|
+
|
286
|
+
specify "should accept index definitions" do
|
287
|
+
@db.create_table(:cats) do
|
288
|
+
integer :id
|
289
|
+
index :id
|
290
|
+
end
|
291
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
|
292
|
+
end
|
293
|
+
|
294
|
+
specify "should accept unique index definitions" do
|
295
|
+
@db.create_table(:cats) do
|
296
|
+
text :name
|
297
|
+
unique :name
|
298
|
+
end
|
299
|
+
@db.sqls.should == ["CREATE TABLE cats (name text, UNIQUE (name))"]
|
300
|
+
end
|
301
|
+
|
302
|
+
specify "should raise on full-text index definitions" do
|
303
|
+
proc {
|
304
|
+
@db.create_table(:cats) do
|
305
|
+
text :name
|
306
|
+
full_text_index :name
|
307
|
+
end
|
308
|
+
}.should raise_error(Sequel::Error)
|
309
|
+
end
|
310
|
+
|
311
|
+
specify "should raise on spatial index definitions" do
|
312
|
+
proc {
|
313
|
+
@db.create_table(:cats) do
|
314
|
+
point :geom
|
315
|
+
spatial_index :geom
|
316
|
+
end
|
317
|
+
}.should raise_error(Sequel::Error)
|
318
|
+
end
|
319
|
+
|
320
|
+
specify "should raise on partial index definitions" do
|
321
|
+
proc {
|
322
|
+
@db.create_table(:cats) do
|
323
|
+
text :name
|
324
|
+
index :name, :where => {:something => true}
|
325
|
+
end
|
326
|
+
}.should raise_error(Sequel::Error)
|
327
|
+
end
|
328
|
+
|
329
|
+
specify "should raise index definitions with type" do
|
330
|
+
proc {
|
331
|
+
@db.create_table(:cats) do
|
332
|
+
text :name
|
333
|
+
index :name, :type => :hash
|
334
|
+
end
|
335
|
+
}.should raise_error(Sequel::Error)
|
336
|
+
end
|
337
|
+
|
338
|
+
specify "should ignore errors if the database raises an error on an index creation statement and the :ignore_index_errors option is used" do
|
339
|
+
@db.meta_def(:execute_ddl){|*a| raise Sequel::DatabaseError if /blah/.match(a.first); super(*a)}
|
340
|
+
lambda{@db.create_table(:cats){Integer :id; index :blah; index :id}}.should raise_error(Sequel::DatabaseError)
|
341
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer)']
|
342
|
+
@db.sqls.clear
|
343
|
+
lambda{@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}}.should_not raise_error(Sequel::DatabaseError)
|
344
|
+
@db.sqls.should == ['CREATE TABLE cats (id integer)', 'CREATE INDEX cats_id_index ON cats (id)']
|
345
|
+
end
|
346
|
+
|
347
|
+
specify "should accept multiple index definitions" do
|
348
|
+
@db.create_table(:cats) do
|
349
|
+
integer :id
|
350
|
+
index :id
|
351
|
+
index :name
|
352
|
+
end
|
353
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)", "CREATE INDEX cats_name_index ON cats (name)"]
|
354
|
+
end
|
355
|
+
|
356
|
+
specify "should accept functional indexes" do
|
357
|
+
@db.create_table(:cats) do
|
358
|
+
integer :id
|
359
|
+
index :lower.sql_function(:name)
|
360
|
+
end
|
361
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower_name__index ON cats (lower(name))"]
|
362
|
+
end
|
363
|
+
|
364
|
+
specify "should accept indexes with identifiers" do
|
365
|
+
@db.create_table(:cats) do
|
366
|
+
integer :id
|
367
|
+
index :lower__name.identifier
|
368
|
+
end
|
369
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower__name_index ON cats (lower__name)"]
|
370
|
+
end
|
371
|
+
|
372
|
+
specify "should accept custom index names" do
|
373
|
+
@db.create_table(:cats) do
|
374
|
+
integer :id
|
375
|
+
index :id, :name => 'abc'
|
376
|
+
end
|
377
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX abc ON cats (id)"]
|
378
|
+
end
|
379
|
+
|
380
|
+
specify "should accept unique index definitions" do
|
381
|
+
@db.create_table(:cats) do
|
382
|
+
integer :id
|
383
|
+
index :id, :unique => true
|
384
|
+
end
|
385
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
|
386
|
+
end
|
387
|
+
|
388
|
+
specify "should accept composite index definitions" do
|
389
|
+
@db.create_table(:cats) do
|
390
|
+
integer :id
|
391
|
+
index [:id, :name], :unique => true
|
392
|
+
end
|
393
|
+
@db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_name_index ON cats (id, name)"]
|
394
|
+
end
|
395
|
+
|
396
|
+
specify "should accept unnamed constraint definitions with blocks" do
|
397
|
+
@db.create_table(:cats) do
|
398
|
+
integer :score
|
399
|
+
check {(:x.sql_number > 0) & (:y.sql_number < 1)}
|
400
|
+
end
|
401
|
+
@db.sqls.should == ["CREATE TABLE cats (score integer, CHECK ((x > 0) AND (y < 1)))"]
|
402
|
+
end
|
403
|
+
|
404
|
+
specify "should accept unnamed constraint definitions" do
|
405
|
+
@db.create_table(:cats) do
|
406
|
+
check 'price < ?', 100
|
407
|
+
end
|
408
|
+
@db.sqls.should == ["CREATE TABLE cats (CHECK (price < 100))"]
|
409
|
+
end
|
410
|
+
|
411
|
+
specify "should accept hash constraints" do
|
412
|
+
@db.create_table(:cats) do
|
413
|
+
check :price=>100
|
414
|
+
end
|
415
|
+
@db.sqls.should == ["CREATE TABLE cats (CHECK (price = 100))"]
|
416
|
+
end
|
417
|
+
|
418
|
+
specify "should accept named constraint definitions" do
|
419
|
+
@db.create_table(:cats) do
|
420
|
+
integer :score
|
421
|
+
constraint :valid_score, 'score <= 100'
|
422
|
+
end
|
423
|
+
@db.sqls.should == ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100))"]
|
424
|
+
end
|
425
|
+
|
426
|
+
specify "should accept named constraint definitions with block" do
|
427
|
+
@db.create_table(:cats) do
|
428
|
+
constraint(:blah_blah) {(:x.sql_number > 0) & (:y.sql_number < 1)}
|
429
|
+
end
|
430
|
+
@db.sqls.should == ["CREATE TABLE cats (CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1)))"]
|
431
|
+
end
|
432
|
+
|
433
|
+
specify "should raise an error if an invalid constraint type is used" do
|
434
|
+
proc{@db.create_table(:cats){unique [:a, :b], :type=>:bb}}.should raise_error(Sequel::Error)
|
435
|
+
end
|
436
|
+
|
437
|
+
specify "should accept composite primary keys" do
|
438
|
+
@db.create_table(:cats) do
|
439
|
+
integer :a
|
440
|
+
integer :b
|
441
|
+
primary_key [:a, :b]
|
442
|
+
end
|
443
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, PRIMARY KEY (a, b))"]
|
444
|
+
end
|
445
|
+
|
446
|
+
specify "should accept named composite primary keys" do
|
447
|
+
@db.create_table(:cats) do
|
448
|
+
integer :a
|
449
|
+
integer :b
|
450
|
+
primary_key [:a, :b], :name => :cpk
|
451
|
+
end
|
452
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cpk PRIMARY KEY (a, b))"]
|
453
|
+
end
|
454
|
+
|
455
|
+
specify "should accept composite foreign keys" do
|
456
|
+
@db.create_table(:cats) do
|
457
|
+
integer :a
|
458
|
+
integer :b
|
459
|
+
foreign_key [:a, :b], :abc
|
460
|
+
end
|
461
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc)"]
|
462
|
+
end
|
463
|
+
|
464
|
+
specify "should accept named composite foreign keys" do
|
465
|
+
@db.create_table(:cats) do
|
466
|
+
integer :a
|
467
|
+
integer :b
|
468
|
+
foreign_key [:a, :b], :abc, :name => :cfk
|
469
|
+
end
|
470
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cfk FOREIGN KEY (a, b) REFERENCES abc)"]
|
471
|
+
end
|
472
|
+
|
473
|
+
specify "should accept composite foreign keys with arbitrary keys" do
|
474
|
+
@db.create_table(:cats) do
|
475
|
+
integer :a
|
476
|
+
integer :b
|
477
|
+
foreign_key [:a, :b], :abc, :key => [:real_a, :real_b]
|
478
|
+
end
|
479
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(real_a, real_b))"]
|
480
|
+
@db.sqls.clear
|
481
|
+
|
482
|
+
@db.create_table(:cats) do
|
483
|
+
integer :a
|
484
|
+
integer :b
|
485
|
+
foreign_key [:a, :b], :abc, :key => [:z, :x]
|
486
|
+
end
|
487
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(z, x))"]
|
488
|
+
end
|
489
|
+
|
490
|
+
specify "should accept composite foreign keys with on delete and on update clauses" do
|
491
|
+
@db.create_table(:cats) do
|
492
|
+
integer :a
|
493
|
+
integer :b
|
494
|
+
foreign_key [:a, :b], :abc, :on_delete => :cascade
|
495
|
+
end
|
496
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE CASCADE)"]
|
497
|
+
|
498
|
+
@db.sqls.clear
|
499
|
+
@db.create_table(:cats) do
|
500
|
+
integer :a
|
501
|
+
integer :b
|
502
|
+
foreign_key [:a, :b], :abc, :on_update => :no_action
|
503
|
+
end
|
504
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON UPDATE NO ACTION)"]
|
505
|
+
|
506
|
+
@db.sqls.clear
|
507
|
+
@db.create_table(:cats) do
|
508
|
+
integer :a
|
509
|
+
integer :b
|
510
|
+
foreign_key [:a, :b], :abc, :on_delete => :restrict, :on_update => :set_default
|
511
|
+
end
|
512
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE RESTRICT ON UPDATE SET DEFAULT)"]
|
513
|
+
|
514
|
+
@db.sqls.clear
|
515
|
+
@db.create_table(:cats) do
|
516
|
+
integer :a
|
517
|
+
integer :b
|
518
|
+
foreign_key [:a, :b], :abc, :key => [:x, :y], :on_delete => :set_null, :on_update => :set_null
|
519
|
+
end
|
520
|
+
@db.sqls.should == ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(x, y) ON DELETE SET NULL ON UPDATE SET NULL)"]
|
521
|
+
end
|
522
|
+
end
|
523
|
+
|
524
|
+
context "DB#create_table!" do
|
525
|
+
before do
|
526
|
+
@db = SchemaDummyDatabase.new
|
527
|
+
end
|
528
|
+
|
529
|
+
specify "should drop the table and then create it" do
|
530
|
+
@db.create_table!(:cats) {}
|
531
|
+
@db.sqls.should == ['DROP TABLE cats', 'CREATE TABLE cats ()']
|
532
|
+
end
|
533
|
+
end
|
534
|
+
|
535
|
+
context "DB#create_table?" do
|
536
|
+
before do
|
537
|
+
@db = SchemaDummyDatabase.new
|
538
|
+
end
|
539
|
+
|
540
|
+
specify "should not create the table if the table already exists" do
|
541
|
+
@db.meta_def(:table_exists?){|a| true}
|
542
|
+
@db.create_table?(:cats){|*a|}
|
543
|
+
@db.sqls.should == nil
|
544
|
+
end
|
545
|
+
|
546
|
+
specify "should create the table if the table doesn't already exist" do
|
547
|
+
@db.meta_def(:table_exists?){|a| false}
|
548
|
+
@db.create_table?(:cats){|*a|}
|
549
|
+
@db.sqls.should == ['CREATE TABLE cats ()']
|
550
|
+
end
|
551
|
+
end
|
552
|
+
|
553
|
+
context "DB#drop_table" do
|
554
|
+
before do
|
555
|
+
@db = SchemaDummyDatabase.new
|
556
|
+
end
|
557
|
+
|
558
|
+
specify "should generate a DROP TABLE statement" do
|
559
|
+
@db.drop_table :cats
|
560
|
+
@db.sqls.should == ['DROP TABLE cats']
|
561
|
+
end
|
562
|
+
end
|
563
|
+
|
564
|
+
context "DB#alter_table" do
|
565
|
+
before do
|
566
|
+
@db = SchemaDummyDatabase.new
|
567
|
+
end
|
568
|
+
|
569
|
+
specify "should allow adding not null constraint" do
|
570
|
+
@db.alter_table(:cats) do
|
571
|
+
set_column_allow_null :score, false
|
572
|
+
end
|
573
|
+
@db.sqls.should == ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
|
574
|
+
end
|
575
|
+
|
576
|
+
specify "should allow droping not null constraint" do
|
577
|
+
@db.alter_table(:cats) do
|
578
|
+
set_column_allow_null :score, true
|
579
|
+
end
|
580
|
+
@db.sqls.should == ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
|
581
|
+
end
|
582
|
+
|
583
|
+
specify "should support add_column" do
|
584
|
+
@db.alter_table(:cats) do
|
585
|
+
add_column :score, :integer
|
586
|
+
end
|
587
|
+
@db.sqls.should == ["ALTER TABLE cats ADD COLUMN score integer"]
|
588
|
+
end
|
589
|
+
|
590
|
+
specify "should support add_constraint" do
|
591
|
+
@db.alter_table(:cats) do
|
592
|
+
add_constraint :valid_score, 'score <= 100'
|
593
|
+
end
|
594
|
+
@db.sqls.should == ["ALTER TABLE cats ADD CONSTRAINT valid_score CHECK (score <= 100)"]
|
595
|
+
end
|
596
|
+
|
597
|
+
specify "should support add_constraint with block" do
|
598
|
+
@db.alter_table(:cats) do
|
599
|
+
add_constraint(:blah_blah) {(:x.sql_number > 0) & (:y.sql_number < 1)}
|
600
|
+
end
|
601
|
+
@db.sqls.should == ["ALTER TABLE cats ADD CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1))"]
|
602
|
+
end
|
603
|
+
|
604
|
+
specify "should support add_unique_constraint" do
|
605
|
+
@db.alter_table(:cats) do
|
606
|
+
add_unique_constraint [:a, :b]
|
607
|
+
end
|
608
|
+
@db.sqls.should == ["ALTER TABLE cats ADD UNIQUE (a, b)"]
|
609
|
+
|
610
|
+
@db.sqls.clear
|
611
|
+
@db.alter_table(:cats) do
|
612
|
+
add_unique_constraint [:a, :b], :name => :ab_uniq
|
613
|
+
end
|
614
|
+
@db.sqls.should == ["ALTER TABLE cats ADD CONSTRAINT ab_uniq UNIQUE (a, b)"]
|
615
|
+
end
|
616
|
+
|
617
|
+
specify "should support add_foreign_key" do
|
618
|
+
@db.alter_table(:cats) do
|
619
|
+
add_foreign_key :node_id, :nodes
|
620
|
+
end
|
621
|
+
@db.sqls.should == ["ALTER TABLE cats ADD COLUMN node_id integer REFERENCES nodes"]
|
622
|
+
end
|
623
|
+
|
624
|
+
specify "should support add_foreign_key with composite foreign keys" do
|
625
|
+
@db.alter_table(:cats) do
|
626
|
+
add_foreign_key [:node_id, :prop_id], :nodes_props
|
627
|
+
end
|
628
|
+
@db.sqls.should == ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
|
629
|
+
|
630
|
+
@db.sqls.clear
|
631
|
+
@db.alter_table(:cats) do
|
632
|
+
add_foreign_key [:node_id, :prop_id], :nodes_props, :name => :cfk
|
633
|
+
end
|
634
|
+
@db.sqls.should == ["ALTER TABLE cats ADD CONSTRAINT cfk FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
|
635
|
+
|
636
|
+
@db.sqls.clear
|
637
|
+
@db.alter_table(:cats) do
|
638
|
+
add_foreign_key [:node_id, :prop_id], :nodes_props, :key => [:nid, :pid]
|
639
|
+
end
|
640
|
+
@db.sqls.should == ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props(nid, pid)"]
|
641
|
+
|
642
|
+
@db.sqls.clear
|
643
|
+
@db.alter_table(:cats) do
|
644
|
+
add_foreign_key [:node_id, :prop_id], :nodes_props, :on_delete => :restrict, :on_update => :cascade
|
645
|
+
end
|
646
|
+
@db.sqls.should == ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props ON DELETE RESTRICT ON UPDATE CASCADE"]
|
647
|
+
end
|
648
|
+
|
649
|
+
specify "should support add_index" do
|
650
|
+
@db.alter_table(:cats) do
|
651
|
+
add_index :name
|
652
|
+
end
|
653
|
+
@db.sqls.should == ["CREATE INDEX cats_name_index ON cats (name)"]
|
654
|
+
end
|
655
|
+
|
656
|
+
specify "should ignore errors if the database raises an error on an add_index call and the :ignore_errors option is used" do
|
657
|
+
@db.meta_def(:execute_ddl){|*a| raise Sequel::DatabaseError}
|
658
|
+
lambda{@db.add_index(:cats, :id)}.should raise_error(Sequel::DatabaseError)
|
659
|
+
lambda{@db.add_index(:cats, :id, :ignore_errors=>true)}.should_not raise_error(Sequel::DatabaseError)
|
660
|
+
@db.sqls.should == nil
|
661
|
+
end
|
662
|
+
|
663
|
+
specify "should support add_primary_key" do
|
664
|
+
@db.alter_table(:cats) do
|
665
|
+
add_primary_key :id
|
666
|
+
end
|
667
|
+
@db.sqls.should == ["ALTER TABLE cats ADD COLUMN id integer PRIMARY KEY AUTOINCREMENT"]
|
668
|
+
end
|
669
|
+
|
670
|
+
specify "should support add_primary_key with composite primary keys" do
|
671
|
+
@db.alter_table(:cats) do
|
672
|
+
add_primary_key [:id, :type]
|
673
|
+
end
|
674
|
+
@db.sqls.should == ["ALTER TABLE cats ADD PRIMARY KEY (id, type)"]
|
675
|
+
|
676
|
+
@db.sqls.clear
|
677
|
+
@db.alter_table(:cats) do
|
678
|
+
add_primary_key [:id, :type], :name => :cpk
|
679
|
+
end
|
680
|
+
@db.sqls.should == ["ALTER TABLE cats ADD CONSTRAINT cpk PRIMARY KEY (id, type)"]
|
681
|
+
end
|
682
|
+
|
683
|
+
specify "should support drop_column" do
|
684
|
+
@db.alter_table(:cats) do
|
685
|
+
drop_column :score
|
686
|
+
end
|
687
|
+
@db.sqls.should == ["ALTER TABLE cats DROP COLUMN score"]
|
688
|
+
end
|
689
|
+
|
690
|
+
specify "should support drop_constraint" do
|
691
|
+
@db.alter_table(:cats) do
|
692
|
+
drop_constraint :valid_score
|
693
|
+
end
|
694
|
+
@db.sqls.should == ["ALTER TABLE cats DROP CONSTRAINT valid_score"]
|
695
|
+
end
|
696
|
+
|
697
|
+
specify "should support drop_index" do
|
698
|
+
@db.alter_table(:cats) do
|
699
|
+
drop_index :name
|
700
|
+
end
|
701
|
+
@db.sqls.should == ["DROP INDEX cats_name_index"]
|
702
|
+
end
|
703
|
+
|
704
|
+
specify "should support drop_index with a given name" do
|
705
|
+
@db.alter_table(:cats) do
|
706
|
+
drop_index :name, :name=>:blah_blah
|
707
|
+
end
|
708
|
+
@db.sqls.should == ["DROP INDEX blah_blah"]
|
709
|
+
end
|
710
|
+
|
711
|
+
specify "should support rename_column" do
|
712
|
+
@db.alter_table(:cats) do
|
713
|
+
rename_column :name, :old_name
|
714
|
+
end
|
715
|
+
@db.sqls.should == ["ALTER TABLE cats RENAME COLUMN name TO old_name"]
|
716
|
+
end
|
717
|
+
|
718
|
+
specify "should support set_column_default" do
|
719
|
+
@db.alter_table(:cats) do
|
720
|
+
set_column_default :score, 3
|
721
|
+
end
|
722
|
+
@db.sqls.should == ["ALTER TABLE cats ALTER COLUMN score SET DEFAULT 3"]
|
723
|
+
end
|
724
|
+
|
725
|
+
specify "should support set_column_type" do
|
726
|
+
@db.alter_table(:cats) do
|
727
|
+
set_column_type :score, :real
|
728
|
+
end
|
729
|
+
@db.sqls.should == ["ALTER TABLE cats ALTER COLUMN score TYPE real"]
|
730
|
+
end
|
731
|
+
|
732
|
+
specify "should support set_column_type with options" do
|
733
|
+
@db.alter_table(:cats) do
|
734
|
+
set_column_type :score, :integer, :unsigned=>true
|
735
|
+
set_column_type :score, :varchar, :size=>30
|
736
|
+
set_column_type :score, :enum, :elements=>['a', 'b']
|
737
|
+
end
|
738
|
+
@db.sqls.should == ["ALTER TABLE cats ALTER COLUMN score TYPE integer UNSIGNED",
|
739
|
+
"ALTER TABLE cats ALTER COLUMN score TYPE varchar(30)",
|
740
|
+
"ALTER TABLE cats ALTER COLUMN score TYPE enum('a', 'b')"]
|
741
|
+
end
|
742
|
+
end
|
743
|
+
|
744
|
+
context "Schema Parser" do
|
745
|
+
before do
|
746
|
+
@sqls = []
|
747
|
+
@db = Sequel::Database.new
|
748
|
+
end
|
749
|
+
|
750
|
+
specify "should raise an error if there are no columns" do
|
751
|
+
@db.meta_def(:schema_parse_table) do |t, opts|
|
752
|
+
[]
|
753
|
+
end
|
754
|
+
proc{@db.schema(:x)}.should raise_error(Sequel::Error)
|
755
|
+
end
|
756
|
+
|
757
|
+
specify "should parse the schema correctly for a single table" do
|
758
|
+
sqls = @sqls
|
759
|
+
proc{@db.schema(:x)}.should raise_error(Sequel::Error)
|
760
|
+
@db.meta_def(:schema_parse_table) do |t, opts|
|
761
|
+
sqls << t
|
762
|
+
[[:a, {:db_type=>t.to_s}]]
|
763
|
+
end
|
764
|
+
@db.schema(:x).should == [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
765
|
+
@sqls.should == ['x']
|
766
|
+
@db.schema(:x).should == [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
767
|
+
@sqls.should == ['x']
|
768
|
+
@db.schema(:x, :reload=>true).should == [[:a, {:db_type=>"x", :ruby_default=>nil}]]
|
769
|
+
@sqls.should == ['x', 'x']
|
770
|
+
end
|
771
|
+
|
772
|
+
specify "should convert various types of table name arguments" do
|
773
|
+
@db.meta_def(:schema_parse_table) do |t, opts|
|
774
|
+
[[t, {:db_type=>t}]]
|
775
|
+
end
|
776
|
+
s1 = @db.schema(:x)
|
777
|
+
s1.should == [['x', {:db_type=>'x', :ruby_default=>nil}]]
|
778
|
+
@db.schema(:x).object_id.should == s1.object_id
|
779
|
+
@db.schema(:x.identifier).object_id.should == s1.object_id
|
780
|
+
s2 = @db.schema(:x__y)
|
781
|
+
s2.should == [['y', {:db_type=>'y', :ruby_default=>nil}]]
|
782
|
+
@db.schema(:x__y).object_id.should == s2.object_id
|
783
|
+
@db.schema(:y.qualify(:x)).object_id.should == s2.object_id
|
784
|
+
end
|
785
|
+
|
786
|
+
specify "should correctly parse all supported data types" do
|
787
|
+
@db.meta_def(:schema_parse_table) do |t, opts|
|
788
|
+
[[:x, {:type=>schema_column_type(t.to_s)}]]
|
789
|
+
end
|
790
|
+
@db.schema(:tinyint).first.last[:type].should == :integer
|
791
|
+
@db.schema(:interval).first.last[:type].should == :interval
|
792
|
+
@db.schema(:int).first.last[:type].should == :integer
|
793
|
+
@db.schema(:integer).first.last[:type].should == :integer
|
794
|
+
@db.schema(:bigint).first.last[:type].should == :integer
|
795
|
+
@db.schema(:smallint).first.last[:type].should == :integer
|
796
|
+
@db.schema(:character).first.last[:type].should == :string
|
797
|
+
@db.schema(:"character varying").first.last[:type].should == :string
|
798
|
+
@db.schema(:varchar).first.last[:type].should == :string
|
799
|
+
@db.schema(:"varchar(255)").first.last[:type].should == :string
|
800
|
+
@db.schema(:text).first.last[:type].should == :string
|
801
|
+
@db.schema(:date).first.last[:type].should == :date
|
802
|
+
@db.schema(:datetime).first.last[:type].should == :datetime
|
803
|
+
@db.schema(:timestamp).first.last[:type].should == :datetime
|
804
|
+
@db.schema(:"timestamp with time zone").first.last[:type].should == :datetime
|
805
|
+
@db.schema(:"timestamp without time zone").first.last[:type].should == :datetime
|
806
|
+
@db.schema(:time).first.last[:type].should == :time
|
807
|
+
@db.schema(:"time with time zone").first.last[:type].should == :time
|
808
|
+
@db.schema(:"time without time zone").first.last[:type].should == :time
|
809
|
+
@db.schema(:boolean).first.last[:type].should == :boolean
|
810
|
+
@db.schema(:bit).first.last[:type].should == :boolean
|
811
|
+
@db.schema(:real).first.last[:type].should == :float
|
812
|
+
@db.schema(:float).first.last[:type].should == :float
|
813
|
+
@db.schema(:double).first.last[:type].should == :float
|
814
|
+
@db.schema(:"double precision").first.last[:type].should == :float
|
815
|
+
@db.schema(:numeric).first.last[:type].should == :decimal
|
816
|
+
@db.schema(:decimal).first.last[:type].should == :decimal
|
817
|
+
@db.schema(:money).first.last[:type].should == :decimal
|
818
|
+
@db.schema(:bytea).first.last[:type].should == :blob
|
819
|
+
@db.schema(:blob).first.last[:type].should == :blob
|
820
|
+
@db.schema(:image).first.last[:type].should == :blob
|
821
|
+
@db.schema(:nchar).first.last[:type].should == :string
|
822
|
+
@db.schema(:nvarchar).first.last[:type].should == :string
|
823
|
+
@db.schema(:ntext).first.last[:type].should == :string
|
824
|
+
@db.schema(:smalldatetime).first.last[:type].should == :datetime
|
825
|
+
@db.schema(:smallmoney).first.last[:type].should == :decimal
|
826
|
+
@db.schema(:binary).first.last[:type].should == :blob
|
827
|
+
@db.schema(:varbinary).first.last[:type].should == :blob
|
828
|
+
@db.schema(:enum).first.last[:type].should == :enum
|
829
|
+
end
|
830
|
+
end
|