sequel_core 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. data/CHANGELOG +1003 -0
  2. data/COPYING +18 -0
  3. data/README +81 -0
  4. data/Rakefile +176 -0
  5. data/bin/sequel +41 -0
  6. data/lib/sequel_core.rb +59 -0
  7. data/lib/sequel_core/adapters/adapter_skeleton.rb +68 -0
  8. data/lib/sequel_core/adapters/ado.rb +100 -0
  9. data/lib/sequel_core/adapters/db2.rb +158 -0
  10. data/lib/sequel_core/adapters/dbi.rb +126 -0
  11. data/lib/sequel_core/adapters/informix.rb +87 -0
  12. data/lib/sequel_core/adapters/jdbc.rb +108 -0
  13. data/lib/sequel_core/adapters/mysql.rb +269 -0
  14. data/lib/sequel_core/adapters/odbc.rb +145 -0
  15. data/lib/sequel_core/adapters/odbc_mssql.rb +93 -0
  16. data/lib/sequel_core/adapters/openbase.rb +90 -0
  17. data/lib/sequel_core/adapters/oracle.rb +99 -0
  18. data/lib/sequel_core/adapters/postgres.rb +519 -0
  19. data/lib/sequel_core/adapters/sqlite.rb +192 -0
  20. data/lib/sequel_core/array_keys.rb +296 -0
  21. data/lib/sequel_core/connection_pool.rb +152 -0
  22. data/lib/sequel_core/core_ext.rb +59 -0
  23. data/lib/sequel_core/core_sql.rb +191 -0
  24. data/lib/sequel_core/database.rb +433 -0
  25. data/lib/sequel_core/dataset.rb +409 -0
  26. data/lib/sequel_core/dataset/convenience.rb +321 -0
  27. data/lib/sequel_core/dataset/sequelizer.rb +354 -0
  28. data/lib/sequel_core/dataset/sql.rb +586 -0
  29. data/lib/sequel_core/exceptions.rb +45 -0
  30. data/lib/sequel_core/migration.rb +191 -0
  31. data/lib/sequel_core/model.rb +8 -0
  32. data/lib/sequel_core/pretty_table.rb +73 -0
  33. data/lib/sequel_core/schema.rb +8 -0
  34. data/lib/sequel_core/schema/schema_generator.rb +131 -0
  35. data/lib/sequel_core/schema/schema_sql.rb +131 -0
  36. data/lib/sequel_core/worker.rb +58 -0
  37. data/spec/adapters/informix_spec.rb +139 -0
  38. data/spec/adapters/mysql_spec.rb +330 -0
  39. data/spec/adapters/oracle_spec.rb +130 -0
  40. data/spec/adapters/postgres_spec.rb +189 -0
  41. data/spec/adapters/sqlite_spec.rb +345 -0
  42. data/spec/array_keys_spec.rb +679 -0
  43. data/spec/connection_pool_spec.rb +356 -0
  44. data/spec/core_ext_spec.rb +67 -0
  45. data/spec/core_sql_spec.rb +301 -0
  46. data/spec/database_spec.rb +812 -0
  47. data/spec/dataset_spec.rb +2381 -0
  48. data/spec/migration_spec.rb +261 -0
  49. data/spec/pretty_table_spec.rb +66 -0
  50. data/spec/rcov.opts +4 -0
  51. data/spec/schema_generator_spec.rb +86 -0
  52. data/spec/schema_spec.rb +230 -0
  53. data/spec/sequelizer_spec.rb +448 -0
  54. data/spec/spec.opts +5 -0
  55. data/spec/spec_helper.rb +44 -0
  56. data/spec/worker_spec.rb +96 -0
  57. metadata +162 -0
@@ -0,0 +1,261 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ context "Migration classes" do
4
+ setup do
5
+ Sequel::Migration.descendants.clear
6
+ end
7
+
8
+ specify "should be registred in Migration.descendants" do
9
+ @class = Class.new(Sequel::Migration)
10
+
11
+ Sequel::Migration.descendants.should == [@class]
12
+ end
13
+
14
+ specify "should be registered in the right order" do
15
+ @c1 = Class.new(Sequel::Migration)
16
+ @c2 = Class.new(Sequel::Migration)
17
+ @c3 = Class.new(Sequel::Migration)
18
+
19
+ Sequel::Migration.descendants.should == [@c1, @c2, @c3]
20
+ end
21
+ end
22
+
23
+ context "Migration#apply" do
24
+ setup do
25
+ @c = Class.new do
26
+ define_method(:one) {|x| [1111, x]}
27
+ define_method(:two) {|x| [2222, x]}
28
+ end
29
+ @db = @c.new
30
+
31
+ @migration = Class.new(Sequel::Migration) do
32
+ define_method(:up) {one(3333)}
33
+ define_method(:down) {two(4444)}
34
+ end
35
+ end
36
+
37
+ specify "should raise for an invalid direction" do
38
+ proc {@migration.apply(@db, :hahaha)}.should raise_error(ArgumentError)
39
+ end
40
+
41
+ specify "should apply the up direction correctly" do
42
+ @migration.apply(@db, :up).should == [1111, 3333]
43
+ end
44
+
45
+ specify "should apply the down direction correctly" do
46
+ @migration.apply(@db, :down).should == [2222, 4444]
47
+ end
48
+ end
49
+
50
+ class DummyMigrationDataset
51
+ attr_reader :from
52
+
53
+ def initialize(x); @from = x; end
54
+
55
+ @@version = nil
56
+
57
+ def version; @@version; end
58
+ def version=(x); @@version = x; end
59
+ def first; @@version ? {:version => @@version} : nil; end
60
+ def update(h); @@version = h[:version]; end
61
+ def <<(h); @@version = h[:version]; end
62
+ end
63
+
64
+ class DummyMigrationDB
65
+ attr_reader :creates, :drops, :table_created
66
+
67
+ def initialize
68
+ @creates = []
69
+ @drops = []
70
+ end
71
+
72
+ def create(x); @creates << x; end
73
+ def drop(x); @drops << x; end
74
+
75
+ def [](x); DummyMigrationDataset.new(x); end
76
+
77
+ def create_table(x); raise if @table_created == x; @table_created = x; end
78
+ def table_exists?(x); @table_created == x; end
79
+
80
+ def transaction; yield; end
81
+ end
82
+
83
+ MIGRATION_001 = %[
84
+ class CreateSessions < Sequel::Migration
85
+ def up
86
+ create(1111)
87
+ end
88
+
89
+ def down
90
+ drop(1111)
91
+ end
92
+ end
93
+ ]
94
+
95
+ MIGRATION_002 = %[
96
+ class CreateNodes < Sequel::Migration
97
+ def up
98
+ create(2222)
99
+ end
100
+
101
+ def down
102
+ drop(2222)
103
+ end
104
+ end
105
+ ]
106
+
107
+ MIGRATION_003 = %[
108
+ class CreateUsers < Sequel::Migration
109
+ def up
110
+ create(3333)
111
+ end
112
+
113
+ def down
114
+ drop(3333)
115
+ end
116
+ end
117
+ ]
118
+
119
+ MIGRATION_005 = %[
120
+ class CreateAttributes < Sequel::Migration
121
+ def up
122
+ create(5555)
123
+ end
124
+
125
+ def down
126
+ drop(5555)
127
+ end
128
+ end
129
+ ]
130
+
131
+ context "Sequel::Migrator" do
132
+ setup do
133
+ @db = DummyMigrationDB.new
134
+
135
+ File.open('001_create_sessions.rb', 'w') {|f| f << MIGRATION_001}
136
+ File.open('002_create_nodes.rb', 'w') {|f| f << MIGRATION_002}
137
+ File.open('003_create_users.rb', 'w') {|f| f << MIGRATION_003}
138
+ File.open('005_create_attributes.rb', 'w') {|f| f << MIGRATION_005}
139
+
140
+ @db[:schema_info].version = nil
141
+ end
142
+
143
+ teardown do
144
+ Object.send(:remove_const, "CreateSessions") if Object.const_defined?("CreateSessions")
145
+ Object.send(:remove_const, "CreateNodes") if Object.const_defined?("CreateNodes")
146
+ Object.send(:remove_const, "CreateUsers") if Object.const_defined?("CreateUsers")
147
+ Object.send(:remove_const, "CreateAttributes") if Object.const_defined?("CreateAttributes")
148
+
149
+ FileUtils.rm('001_create_sessions.rb')
150
+ FileUtils.rm('002_create_nodes.rb')
151
+ FileUtils.rm('003_create_users.rb')
152
+ FileUtils.rm('005_create_attributes.rb')
153
+ end
154
+
155
+ specify "should return the list of files for a specified version range" do
156
+ Sequel::Migrator.migration_files('.', 1..1).should == \
157
+ ['./001_create_sessions.rb']
158
+
159
+ Sequel::Migrator.migration_files('.', 1..3).should == \
160
+ ['./001_create_sessions.rb', './002_create_nodes.rb', './003_create_users.rb']
161
+
162
+ Sequel::Migrator.migration_files('.', 3..5).should == \
163
+ ['./003_create_users.rb', './005_create_attributes.rb']
164
+
165
+ Sequel::Migrator.migration_files('.', 7..8).should == []
166
+ end
167
+
168
+ specify "should return the latest version available" do
169
+ Sequel::Migrator.latest_migration_version('.').should == 5
170
+ end
171
+
172
+ specify "should load the migration classes for the specified range" do
173
+ Sequel::Migrator.migration_classes('.', 3, 0, :up).should == \
174
+ [CreateSessions, CreateNodes, CreateUsers]
175
+ end
176
+
177
+ specify "should load the migration classes for the specified range" do
178
+ Sequel::Migrator.migration_classes('.', 0, 5, :down).should == \
179
+ [CreateAttributes, CreateUsers, CreateNodes, CreateSessions]
180
+ end
181
+
182
+ specify "should start from current + 1 for the up direction" do
183
+ Sequel::Migrator.migration_classes('.', 3, 1, :up).should == \
184
+ [CreateNodes, CreateUsers]
185
+ end
186
+
187
+ specify "should end on current + 1 for the down direction" do
188
+ Sequel::Migrator.migration_classes('.', 2, 5, :down).should == \
189
+ [CreateAttributes, CreateUsers]
190
+ end
191
+
192
+ specify "should automatically create the schema_info table" do
193
+ @db.table_exists?(:schema_info).should be_false
194
+ Sequel::Migrator.schema_info_dataset(@db)
195
+ @db.table_exists?(:schema_info).should be_true
196
+
197
+ # should not raise if table already exists
198
+ proc {Sequel::Migrator.schema_info_dataset(@db)}.should_not raise_error
199
+ end
200
+
201
+ specify "should return a dataset for the schema_info table" do
202
+ d = Sequel::Migrator.schema_info_dataset(@db)
203
+ d.from.should == :schema_info
204
+ end
205
+
206
+ specify "should get the migration version stored in the database" do
207
+ # default is 0
208
+ Sequel::Migrator.get_current_migration_version(@db).should == 0
209
+
210
+ Sequel::Migrator.schema_info_dataset(@db) << {:version => 4321}
211
+
212
+ Sequel::Migrator.get_current_migration_version(@db).should == 4321
213
+ end
214
+
215
+ specify "should set the migration version stored in the database" do
216
+ Sequel::Migrator.get_current_migration_version(@db).should == 0
217
+ Sequel::Migrator.set_current_migration_version(@db, 6666)
218
+ Sequel::Migrator.get_current_migration_version(@db).should == 6666
219
+ end
220
+
221
+ specify "should apply migrations correctly in the up direction" do
222
+ Sequel::Migrator.apply(@db, '.', 3, 2)
223
+ @db.creates.should == [3333]
224
+
225
+ Sequel::Migrator.get_current_migration_version(@db).should == 3
226
+
227
+ Sequel::Migrator.apply(@db, '.', 5)
228
+ @db.creates.should == [3333, 5555]
229
+
230
+ Sequel::Migrator.get_current_migration_version(@db).should == 5
231
+ end
232
+
233
+ specify "should apply migrations correctly in the down direction" do
234
+ Sequel::Migrator.apply(@db, '.', 1, 5)
235
+ @db.drops.should == [5555, 3333, 2222]
236
+
237
+ Sequel::Migrator.get_current_migration_version(@db).should == 1
238
+ end
239
+
240
+ specify "should apply migrations up to the latest version if no target is given" do
241
+ Sequel::Migrator.apply(@db, '.')
242
+ @db.creates.should == [1111, 2222, 3333, 5555]
243
+
244
+ Sequel::Migrator.get_current_migration_version(@db).should == 5
245
+ end
246
+
247
+ specify "should apply migrations down to 0 version correctly" do
248
+ Sequel::Migrator.apply(@db, '.', 0, 5)
249
+ @db.drops.should == [5555, 3333, 2222, 1111]
250
+
251
+ Sequel::Migrator.get_current_migration_version(@db).should == 0
252
+ end
253
+
254
+ specify "should return the target version" do
255
+ Sequel::Migrator.apply(@db, '.', 3, 2).should == 3
256
+
257
+ Sequel::Migrator.apply(@db, '.', 0).should == 0
258
+
259
+ Sequel::Migrator.apply(@db, '.').should == 5
260
+ end
261
+ end
@@ -0,0 +1,66 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ require 'stringio'
4
+
5
+ context "PrettyTable" do
6
+ setup do
7
+ @data1 = [
8
+ {:x => 3, :y => 4}
9
+ ]
10
+
11
+ @data2 = [
12
+ {:a => 23, :b => 45},
13
+ {:a => 45, :b => 2377}
14
+ ]
15
+
16
+ @data3 = [
17
+ {:aaa => 1},
18
+ {:bb => 2},
19
+ {:c => 3}
20
+ ]
21
+
22
+ @output = StringIO.new
23
+ @orig_stdout = $stdout
24
+ $stdout = @output
25
+ end
26
+
27
+ teardown do
28
+ $stdout = @orig_stdout
29
+ end
30
+
31
+ specify "should infer the columns if not given" do
32
+ Sequel::PrettyTable.print(@data1)
33
+ @output.rewind
34
+ @output.read.should =~ \
35
+ /\n(\|x\|y\|)|(\|y\|x\|)\n/
36
+ end
37
+
38
+ specify "should infer columns from array with keys" do
39
+ a = [1, 2, 3]
40
+ a.keys = [:a, :b, :c]
41
+ Sequel::PrettyTable.print([a])
42
+ @output.rewind
43
+ @output.read.should =~ /\n\|a\|b\|c\|\n/
44
+ end
45
+
46
+ specify "should calculate the maximum width of each column correctly" do
47
+ Sequel::PrettyTable.print(@data2, [:a, :b])
48
+ @output.rewind
49
+ @output.read.should == \
50
+ "+--+----+\n|a |b |\n+--+----+\n|23| 45|\n|45|2377|\n+--+----+\n"
51
+ end
52
+
53
+ specify "should also take header width into account" do
54
+ Sequel::PrettyTable.print(@data3, [:aaa, :bb, :c])
55
+ @output.rewind
56
+ @output.read.should == \
57
+ "+---+--+-+\n|aaa|bb|c|\n+---+--+-+\n| 1| | |\n| | 2| |\n| | |3|\n+---+--+-+\n"
58
+ end
59
+
60
+ specify "should print only the specified columns" do
61
+ Sequel::PrettyTable.print(@data2, [:a])
62
+ @output.rewind
63
+ @output.read.should == \
64
+ "+--+\n|a |\n+--+\n|23|\n|45|\n+--+\n"
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ --exclude
2
+ gems
3
+ --exclude
4
+ spec
@@ -0,0 +1,86 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe Sequel::Schema::Generator do
4
+ before :all do
5
+ @generator = Sequel::Schema::Generator.new(SchemaDummyDatabase.new) do
6
+ string :title
7
+ column :body, :text
8
+ foreign_key :parent_id
9
+ primary_key :id
10
+ index :title
11
+ index [:title, :body]
12
+ end
13
+ @columns, @indexes = @generator.create_info
14
+ end
15
+
16
+ {:name => :id, :primary_key => true}.each do |column, expected|
17
+ it "uses default primary key #{column}" do
18
+ @columns.first[column].should == expected
19
+ end
20
+ end
21
+
22
+ it "counts primary key as column" do
23
+ @columns.size.should == 4
24
+ end
25
+
26
+ it "places primary key first" do
27
+ @columns[0][:primary_key].should be_true
28
+ @columns[1][:primary_key].should_not be_true
29
+ @columns[2][:primary_key].should_not be_true
30
+ end
31
+
32
+ it "retrieves primary key name" do
33
+ @generator.primary_key_name.should == :id
34
+ end
35
+
36
+ it "keeps columns in order" do
37
+ @columns[1][:name].should == :title
38
+ @columns[1][:type].should == :string
39
+ @columns[2][:name].should == :body
40
+ @columns[2][:type].should == :text
41
+ end
42
+
43
+ it "creates foreign key column" do
44
+ @columns[3][:name].should == :parent_id
45
+ @columns[3][:type].should == :integer
46
+ end
47
+
48
+ it "finds columns" do
49
+ [:title, :body, :parent_id, :id].each do |col|
50
+ @generator.has_column?(col).should be_true
51
+ end
52
+ @generator.has_column?(:foo).should_not be_true
53
+ end
54
+
55
+ it "creates indexes" do
56
+ @indexes[0][:columns].should include(:title)
57
+ @indexes[1][:columns].should include(:title)
58
+ @indexes[1][:columns].should include(:body)
59
+ end
60
+ end
61
+
62
+ describe Sequel::Schema::AlterTableGenerator do
63
+ before :all do
64
+ @generator = Sequel::Schema::AlterTableGenerator.new(SchemaDummyDatabase.new) do
65
+ add_column :aaa, :text
66
+ drop_column :bbb
67
+ rename_column :ccc, :ho
68
+ set_column_type :ddd, :float
69
+ set_column_default :eee, 1
70
+ add_index [:fff, :ggg]
71
+ drop_index :hhh
72
+ end
73
+ end
74
+
75
+ specify "should generate operation records" do
76
+ @generator.operations.should == [
77
+ {:op => :add_column, :name => :aaa, :type => :text},
78
+ {:op => :drop_column, :name => :bbb},
79
+ {:op => :rename_column, :name => :ccc, :new_name => :ho},
80
+ {:op => :set_column_type, :name => :ddd, :type => :float},
81
+ {:op => :set_column_default, :name => :eee, :default => 1},
82
+ {:op => :add_index, :columns => [:fff, :ggg]},
83
+ {:op => :drop_index, :columns => [:hhh]}
84
+ ]
85
+ end
86
+ end
@@ -0,0 +1,230 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ context "DB#create_table" do
4
+ setup 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 multiple columns" do
14
+ @db.create_table(:cats) do
15
+ column :id, :integer
16
+ column :name, :text
17
+ end
18
+ @db.sqls.should == ['CREATE TABLE cats (id integer, name text)']
19
+ end
20
+
21
+ specify "should accept method calls as data types" do
22
+ @db.create_table(:cats) do
23
+ integer :id
24
+ text :name
25
+ end
26
+ @db.sqls.should == ['CREATE TABLE cats (id integer, name text)']
27
+ end
28
+
29
+ specify "should accept primary key definition" do
30
+ @db.create_table(:cats) do
31
+ primary_key :id
32
+ end
33
+ @db.sqls.should == ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT)']
34
+
35
+ @db.sqls.clear
36
+ @db.create_table(:cats) do
37
+ primary_key :id, :serial, :auto_increment => false
38
+ end
39
+ @db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)']
40
+
41
+ @db.sqls.clear
42
+ @db.create_table(:cats) do
43
+ primary_key :id, :type => :serial, :auto_increment => false
44
+ end
45
+ @db.sqls.should == ['CREATE TABLE cats (id serial PRIMARY KEY)']
46
+ end
47
+
48
+ specify "should accept and literalize default values" do
49
+ @db.create_table(:cats) do
50
+ integer :id, :default => 123
51
+ text :name, :default => "abc'def"
52
+ end
53
+ @db.sqls.should == ["CREATE TABLE cats (id integer DEFAULT 123, name text DEFAULT 'abc''def')"]
54
+ end
55
+
56
+ specify "should accept not null definition" do
57
+ @db.create_table(:cats) do
58
+ integer :id
59
+ text :name, :null => false
60
+ end
61
+ @db.sqls.should == ["CREATE TABLE cats (id integer, name text NOT NULL)"]
62
+ end
63
+
64
+ specify "should accept unique definition" do
65
+ @db.create_table(:cats) do
66
+ integer :id
67
+ text :name, :unique => true
68
+ end
69
+ @db.sqls.should == ["CREATE TABLE cats (id integer, name text UNIQUE)"]
70
+ end
71
+
72
+ specify "should accept [SET|ENUM](...) types" do
73
+ @db.create_table(:cats) do
74
+ set :color, :elements => ['black', 'tricolor', 'grey']
75
+ end
76
+ @db.sqls.should == ["CREATE TABLE cats (color set('black', 'tricolor', 'grey'))"]
77
+ end
78
+
79
+ specify "should accept varchar size" do
80
+ @db.create_table(:cats) do
81
+ varchar :name
82
+ end
83
+ @db.sqls.should == ["CREATE TABLE cats (name varchar(255))"]
84
+ @db.sqls.clear
85
+ @db.create_table(:cats) do
86
+ varchar :name, :size => 51
87
+ end
88
+ @db.sqls.should == ["CREATE TABLE cats (name varchar(51))"]
89
+ end
90
+
91
+ specify "should accept foreign keys" do
92
+ @db.create_table(:cats) do
93
+ foreign_key :project_id, :table => :projects
94
+ end
95
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects)"]
96
+ end
97
+
98
+ specify "should accept foreign keys with arbitrary keys" do
99
+ @db.create_table(:cats) do
100
+ foreign_key :project_id, :table => :projects, :key => :id
101
+ end
102
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(id))"]
103
+
104
+ @db.sqls.clear
105
+ @db.create_table(:cats) do
106
+ foreign_key :project_id, :table => :projects, :key => :zzz
107
+ end
108
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects(zzz))"]
109
+ end
110
+
111
+ specify "should accept foreign keys with ON DELETE clause" do
112
+ @db.create_table(:cats) do
113
+ foreign_key :project_id, :table => :projects, :on_delete => :restrict
114
+ end
115
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE RESTRICT)"]
116
+
117
+ @db.sqls.clear
118
+ @db.create_table(:cats) do
119
+ foreign_key :project_id, :table => :projects, :on_delete => :cascade
120
+ end
121
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)"]
122
+
123
+ @db.sqls.clear
124
+ @db.create_table(:cats) do
125
+ foreign_key :project_id, :table => :projects, :on_delete => :no_action
126
+ end
127
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION)"]
128
+ @db.sqls.clear
129
+
130
+ @db.sqls.clear
131
+ @db.create_table(:cats) do
132
+ foreign_key :project_id, :table => :projects, :on_delete => :set_null
133
+ end
134
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET NULL)"]
135
+ @db.sqls.clear
136
+
137
+ @db.sqls.clear
138
+ @db.create_table(:cats) do
139
+ foreign_key :project_id, :table => :projects, :on_delete => :set_default
140
+ end
141
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET DEFAULT)"]
142
+ @db.sqls.clear
143
+ end
144
+
145
+ specify "should accept inline index definition" do
146
+ @db.create_table(:cats) do
147
+ integer :id, :index => true
148
+ end
149
+ @db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
150
+ end
151
+
152
+ specify "should accept inline index definition for foreign keys" do
153
+ @db.create_table(:cats) do
154
+ foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => true
155
+ end
156
+ @db.sqls.should == ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
157
+ "CREATE INDEX cats_project_id_index ON cats (project_id)"]
158
+ end
159
+
160
+ specify "should accept index definitions" do
161
+ @db.create_table(:cats) do
162
+ integer :id
163
+ index :id
164
+ end
165
+ @db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
166
+ end
167
+
168
+ specify "should accept unique index definitions" do
169
+ @db.create_table(:cats) do
170
+ text :name
171
+ unique :name
172
+ end
173
+ @db.sqls.should == ["CREATE TABLE cats (name text)", "CREATE UNIQUE INDEX cats_name_index ON cats (name)"]
174
+ end
175
+
176
+ specify "should accept multiple index definitions" do
177
+ @db.create_table(:cats) do
178
+ integer :id
179
+ index :id
180
+ index :name
181
+ end
182
+ @db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)", "CREATE INDEX cats_name_index ON cats (name)"]
183
+ end
184
+
185
+ specify "should accept custom index names" do
186
+ @db.create_table(:cats) do
187
+ integer :id
188
+ index :id, :name => 'abc'
189
+ end
190
+ @db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE INDEX abc ON cats (id)"]
191
+ end
192
+
193
+ specify "should accept unique index definitions" do
194
+ @db.create_table(:cats) do
195
+ integer :id
196
+ index :id, :unique => true
197
+ end
198
+ @db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
199
+ end
200
+
201
+ specify "should accept composite index definitions" do
202
+ @db.create_table(:cats) do
203
+ integer :id
204
+ index [:id, :name], :unique => true
205
+ end
206
+ @db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_name_index ON cats (id, name)"]
207
+ end
208
+ end
209
+
210
+ context "DB#create_table!" do
211
+ setup do
212
+ @db = SchemaDummyDatabase.new
213
+ end
214
+
215
+ specify "should drop the table and then create it" do
216
+ @db.create_table!(:cats) {}
217
+ @db.sqls.should == ['DROP TABLE cats', 'CREATE TABLE cats ()']
218
+ end
219
+ end
220
+
221
+ context "DB#drop_table" do
222
+ setup do
223
+ @db = SchemaDummyDatabase.new
224
+ end
225
+
226
+ specify "should generate a DROP TABLE statement" do
227
+ @db.drop_table :cats
228
+ @db.sqls.should == ['DROP TABLE cats']
229
+ end
230
+ end