Class Sequel::Schema::AlterTableGenerator
In: lib/sequel/database/schema_generator.rb
Parent: Object

Schema::AlterTableGenerator is an internal class that the user is not expected to instantiate directly. Instances are created by Database#alter_table. It is used to specify table alteration parameters. It takes a Database object and a block of operations to perform on the table, and gives the Database a table an array of operations, which the database uses to alter a table‘s description.

Methods

Attributes

operations  [R]  An array of DDL operations to perform

Public Class methods

Set the Database object to which to apply the DDL, and evaluate the block in the context of this object.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 210
210:       def initialize(db, &block)
211:         @db = db
212:         @operations = []
213:         instance_eval(&block) if block
214:       end

Public Instance methods

Add a column with the given name, type, and opts to the DDL for the table. See Generator#column for the available options.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 218
218:       def add_column(name, type, opts = {})
219:         @operations << {:op => :add_column, :name => name, :type => type}.merge(opts)
220:       end

Add a constraint with the given name and args to the DDL for the table. See Generator#constraint.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 224
224:       def add_constraint(name, *args, &block)
225:         @operations << {:op => :add_constraint, :name => name, :type => :check, :check => block || args}
226:       end

Add a foreign key with the given name and referencing the given table to the DDL for the table. See Generator#column for the available options.

You can also pass an array of column names for creating composite foreign keys. In this case, it will assume the columns exists and will only add the constraint.

NOTE: If you need to add a foreign key constraint to an existing column use the composite key syntax even if it is only one column.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 242
242:       def add_foreign_key(name, table, opts = {})
243:         return add_composite_foreign_key(name, table, opts) if name.is_a?(Array)
244:         add_column(name, Integer, {:table=>table}.merge(opts))
245:       end

Add a full text index on the given columns to the DDL for the table. See Generator#index for available options.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 249
249:       def add_full_text_index(columns, opts = {})
250:         add_index(columns, {:type=>:full_text}.merge(opts))
251:       end

Add an index on the given columns to the DDL for the table. See Generator#index for available options.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 255
255:       def add_index(columns, opts = {})
256:         @operations << {:op => :add_index, :columns => Array(columns)}.merge(opts)
257:       end

Add a primary key to the DDL for the table. See Generator#column for the available options.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 261
261:       def add_primary_key(name, opts = {})
262:         return add_composite_primary_key(name, opts) if name.is_a?(Array)
263:         opts = @db.serial_primary_key_options.merge(opts)
264:         add_column(name, opts.delete(:type), opts)
265:       end

Add a spatial index on the given columns to the DDL for the table. See Generator#index for available options.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 269
269:       def add_spatial_index(columns, opts = {})
270:         add_index(columns, {:type=>:spatial}.merge(opts))
271:       end

Add a unique constraint to the given column(s)

[Source]

     # File lib/sequel/database/schema_generator.rb, line 229
229:       def add_unique_constraint(columns, opts = {})
230:         @operations << {:op => :add_constraint, :type => :unique, :columns => Array(columns)}.merge(opts)
231:       end

Remove a column from the DDL for the table.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 274
274:       def drop_column(name)
275:         @operations << {:op => :drop_column, :name => name}
276:       end

Remove a constraint from the DDL for the table.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 279
279:       def drop_constraint(name)
280:         @operations << {:op => :drop_constraint, :name => name}
281:       end

Remove an index from the DDL for the table.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 284
284:       def drop_index(columns, options={})
285:         @operations << {:op => :drop_index, :columns => Array(columns)}.merge(options)
286:       end

Modify a column‘s name in the DDL for the table.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 289
289:       def rename_column(name, new_name, opts = {})
290:         @operations << {:op => :rename_column, :name => name, :new_name => new_name}.merge(opts)
291:       end

Modify a column‘s NOT NULL constraint.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 304
304:       def set_column_allow_null(name, allow_null)
305:         @operations << {:op => :set_column_null, :name => name, :null => allow_null}
306:       end

Modify a column‘s default value in the DDL for the table.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 294
294:       def set_column_default(name, default)
295:         @operations << {:op => :set_column_default, :name => name, :default => default}
296:       end

Modify a column‘s type in the DDL for the table.

[Source]

     # File lib/sequel/database/schema_generator.rb, line 299
299:       def set_column_type(name, type, opts={})
300:         @operations << {:op => :set_column_type, :name => name, :type => type}.merge(opts)
301:       end

[Validate]