An internal class that the user is not expected to instantiate directly. Instances are created by patio.Database#alterTable. It is used to specify table alteration parameters. It takes a Database object and a function which is called in the scope of the patio.AlterTableGenerator to perform on the table, and gives the Database an array of table altering operations, which the database uses to alter a table's description.
Example
DB.alterTable("xyz", function() {
this.addColumn("aaa", "text", {null : false, unique : true});
this.dropColumn("bbb");
this.renameColumn("ccc", "ddd");
this.setColumnType("eee", "integer");
this.setColumnDefault("hhh", 'abcd');
this.addIndex("fff", {unique : true});
this.dropIndex("ggg");
});
//or using the passed in generator
DB.alterTable("xyz", function(table) {
table.addColumn("aaa", "text", {null : false, unique : true});
table.dropColumn("bbb");
table.renameColumn("ccc", "ddd");
table.setColumnType("eee", "integer");
table.setColumnDefault("hhh", 'abcd');
table.addIndex("fff", {unique : true});
table.dropIndex("ggg");
});
function (db,block){
this.db = db;
this.operations = [];
block.apply(this, [this]);
}
function (columns,table,opts){
this.operations.push(merge({op:"addConstraint", type:"foreignKey", columns:columns, table:table}, opts));
}
function (columns,opts){
this.operations.push(merge({op:"addConstraint", type:"primaryKey", columns:columns}, opts));
}
Add a column with the given name, type, and opts to the DDL for the table. See patio.SchemaGenerator#column for the available options.
Example
DB.alterTable("test", function(){
this.addColumn("name", String);
//=> ADD COLUMN name varchar(255)
});
Arguments
function (name,type,opts){
opts = opts || {};
this.operations.push(merge({op:"addColumn", name:name, type:type}, opts));
}
Add a constraint with the given name and args to the DDL for the table. See patio.SchemaGenerator#constraint.
Example
var sql = patio.sql;
DB.alterTable("test", function(){
this.addConstraint("valid_name", sql.name.like('A%'));
//=>ADD CONSTRAINT valid_name CHECK (name LIKE 'A%')
});
Arguments
function (name){
var args = argsToArray(arguments).slice(1);
var block = isFunction(args[args.length - 1]) ? args[args.length - 1]() : null;
this.operations.push({op:"addConstraint", name:name, type:"check", check:block || args});
}
Add a foreign key with the given name and referencing the given table to the DDL for the table. See patio.SchemaGenerator#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 a single existing column use the composite key syntax even if it is only one column.
Example
DB.alterTable("albums", function(){
this.addForeignKey("artist_id", "table");
//=>ADD COLUMN artist_id integer REFERENCES table
this.addForeignKey(["name"], "table")
//=>ADD FOREIGN KEY (name) REFERENCES table
});
Arguments
function (name,table,opts){
opts = opts;
if (isArray(name)) {
return this.__addCompositeForeignKey(name, table, opts);
} else {
return this.addColumn(name, this.db.defaultPrimaryKeyType, merge({table:table}, opts));
}
}
Add a full text index on the given columns to the DDL for the table. See @{link patio.SchemaGenerator#index} for available options.
Arguments
function (columns,opts){
opts = opts || {};
return this.addIndex(columns, merge({type:"fullText"}, opts));
}
Add an index on the given columns to the DDL for the table. See patio.SchemaGenerator#index for available options.
Example
DB.alterTable("table", function(){
this.addIndex("artist_id");
//=> CREATE INDEX table_artist_id_index ON table (artist_id)
});
Arguments
function (columns,opts){
opts = opts || {};
this.operations.push(merge({op:"addIndex", columns:toArray(columns)}, opts));
}
Add a primary key to the DDL for the table. See patio.SchemaGenerator#column for the available options. Like patio.ALterTableGenerator#addForeignKey, if you specify the column name as an array, it just creates a constraint:
Example
DB.alterTable("albums", function(){
this.addPrimaryKey("id");
//=> ADD COLUMN id serial PRIMARY KEY
this.addPrimaryKey(["artist_id", "name"])
//=>ADD PRIMARY KEY (artist_id, name)
});
Arguments
function (name,opts){
opts = opts || {};
if (isArray(name)) {
return this.__addCompositePrimaryKey(name, opts);
} else {
opts = merge({}, this.db.serialPrimaryKeyOptions, opts);
delete opts.type;
return this.addColumn(name, "integer", opts);
}
}
Add a spatial index on the given columns to the DDL for the table. See patio.SchemaGenerator#index for available options.
Arguments
function (columns,opts){
opts = opts || {};
this.addIndex(columns, merge({}, {type:"spatial"}, opts));
}
Add a unique constraint to the given column(s). See patio.SchemaGenerator#constraint.
Example
DB.alterTable("test", function(){
this.addUniqueConstraint("name");
//=> ADD UNIQUE (name)
this.addUniqueConstraint("name", {name : "uniqueName});
//=> ADD CONSTRAINT uniqueName UNIQUE (name)
});
Arguments
function (columns,opts){
opts = opts || {};
this.operations.push(merge({op:"addConstraint", type:"unique", columns:toArray(columns)}, opts));
}
Remove a column from the DDL for the table.
Example
DB.alterTable("albums", function(){
this.dropColumn("artist_id");
//=>DROP COLUMN artist_id
});
Arguments
the name of the column to drop.
function (name){
this.operations.push({op:"dropColumn", name:name});
}
Remove a constraint from the DDL for the table.
Example
DB.alterTable("test", function(){
this.dropConstraint("constraint_name");
//=>DROP CONSTRAINT constraint_name
});
Arguments
the name of the constraint to drop.
function (name){
this.operations.push({op:"dropConstraint", name:name});
}
Remove an index from the DDL for the table.
Example
DB.alterTable("albums", function(){
this.dropIndex("artist_id")
//=>DROP INDEX table_artist_id_index
this.dropIndex(["a", "b"])
//=>DROP INDEX table_a_b_index
this.dropIndex(["a", "b"], {name : "foo"})
//=>DROP INDEX foo
});
Arguments
function (columns,opts){
opts = opts || {};
this.operations.push(merge({op:"dropIndex", columns:toArray(columns)}, opts));
}
Remove a child table's inheritance from a parent table.
Example
DB.alterTable("test", function () {
this.noInherit("parentTable");
//=>ALTER TABLE test NO INHERIT parent_table
});
Arguments
the name of the table to remove inheritance from.
function (name){
this.operations.push({op:"noInherit", name: name});
}
Modify a column's name in the DDL for the table.
Example
DB.alterTable("artist", function(){
this.renameColumn("name", "artistName");
//=> RENAME COLUMN name TO artist_name
});
Arguments
function (name,newName,opts){
opts = opts || {};
this.operations.push(merge({op:"renameColumn", name:name, newName:newName}, opts));
}
Modify a column's NOT NULL constraint.
Example
DB.alterTable("artist", function(){
this.setColumnAllowNull("artist_name", false);
//=> ALTER COLUMN artist_name SET NOT NULL
});
Arguments
function (name,allowNull){
this.operations.push({op:"setColumnNull", name:name, "null":allowNull});
}
Modify a column's default value in the DDL for the table.
Example
DB.alterTable("artist", function(){
//=>this.setColumnDefault("artist_name", "a");
//=> ALTER COLUMN artist_name SET DEFAULT 'a'
});
Arguments
function (name,def){
this.operations.push({op:"setColumnDefault", name:name, "default":def});
}
Modify a column's type in the DDL for the table.
Example
DB.alterTable("artist", function(){
this.setColumnType("artist_name", 'char(10)');
//=> ALTER COLUMN artist_name TYPE char(10)
});
Arguments
function (name,type,opts){
opts = opts || {};
this.operations.push(merge({op:"setColumnType", name:name, type:type}, opts));
}