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
  1. DB.alterTable("xyz", function() {
  2. this.addColumn("aaa", "text", {null : false, unique : true});
  3. this.dropColumn("bbb");
  4. this.renameColumn("ccc", "ddd");
  5. this.setColumnType("eee", "integer");
  6. this.setColumnDefault("hhh", 'abcd');
  7. this.addIndex("fff", {unique : true});
  8. this.dropIndex("ggg");
  9. });
  10.  
  11. //or using the passed in generator
  12. DB.alterTable("xyz", function(table) {
  13. table.addColumn("aaa", "text", {null : false, unique : true});
  14. table.dropColumn("bbb");
  15. table.renameColumn("ccc", "ddd");
  16. table.setColumnType("eee", "integer");
  17. table.setColumnDefault("hhh", 'abcd');
  18. table.addIndex("fff", {unique : true});
  19. table.dropIndex("ggg");
  20. });

Constructor

Defined database/schemaGenerators.js Source
  1. function (db,block){
  2. this.db = db;
  3. this.operations = [];
  4. block.apply(this, [this]);
  5. }

__addCompositeForeignKey Function Private


Defined database/schemaGenerators.js

Arguments Source
  1. function (columns,table,opts){
  2. this.operations.push(merge({op:"addConstraint", type:"foreignKey", columns:columns, table:table}, opts));
  3. }

__addCompositePrimaryKey Function Private


Defined database/schemaGenerators.js

Arguments Source
  1. function (columns,opts){
  2. this.operations.push(merge({op:"addConstraint", type:"primaryKey", columns:columns}, opts));
  3. }

addColumn Function Public


Defined database/schemaGenerators.js

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
  1. DB.alterTable("test", function(){
  2. this.addColumn("name", String);
  3. //=> ADD COLUMN name varchar(255)
  4. });
Arguments Source
  1. function (name,type,opts){
  2. opts = opts || {};
  3. this.operations.push(merge({op:"addColumn", name:name, type:type}, opts));
  4. }

addConstraint Function Public


Defined database/schemaGenerators.js

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

Example
  1. var sql = patio.sql;
  2. DB.alterTable("test", function(){
  3. this.addConstraint("valid_name", sql.name.like('A%'));
  4. //=>ADD CONSTRAINT valid_name CHECK (name LIKE 'A%')
  5. });
Arguments Source
  1. function (name){
  2. var args = argsToArray(arguments).slice(1);
  3. var block = isFunction(args[args.length - 1]) ? args[args.length - 1]() : null;
  4. this.operations.push({op:"addConstraint", name:name, type:"check", check:block || args});
  5. }

addForeignKey Function Public


Defined database/schemaGenerators.js

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
  1. DB.alterTable("albums", function(){
  2. this.addForeignKey("artist_id", "table");
  3. //=>ADD COLUMN artist_id integer REFERENCES table
  4. this.addForeignKey(["name"], "table")
  5. //=>ADD FOREIGN KEY (name) REFERENCES table
  6. });
Arguments Source
  1. function (name,table,opts){
  2. opts = opts;
  3. if (isArray(name)) {
  4. return this.__addCompositeForeignKey(name, table, opts);
  5. } else {
  6. return this.addColumn(name, this.db.defaultPrimaryKeyType, merge({table:table}, opts));
  7. }
  8. }

addFullTextIndex Function Public


Defined database/schemaGenerators.js

Add a full text index on the given columns to the DDL for the table. See @{link patio.SchemaGenerator#index} for available options.

Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. return this.addIndex(columns, merge({type:"fullText"}, opts));
  4. }

addIndex Function Public


Defined database/schemaGenerators.js

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

Example
  1. DB.alterTable("table", function(){
  2. this.addIndex("artist_id");
  3. //=> CREATE INDEX table_artist_id_index ON table (artist_id)
  4. });
Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. this.operations.push(merge({op:"addIndex", columns:toArray(columns)}, opts));
  4. }

addPrimaryKey Function Public


Defined database/schemaGenerators.js

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
  1. DB.alterTable("albums", function(){
  2. this.addPrimaryKey("id");
  3. //=> ADD COLUMN id serial PRIMARY KEY
  4. this.addPrimaryKey(["artist_id", "name"])
  5. //=>ADD PRIMARY KEY (artist_id, name)
  6. });
Arguments Source
  1. function (name,opts){
  2. opts = opts || {};
  3. if (isArray(name)) {
  4. return this.__addCompositePrimaryKey(name, opts);
  5. } else {
  6. opts = merge({}, this.db.serialPrimaryKeyOptions, opts);
  7. delete opts.type;
  8. return this.addColumn(name, "integer", opts);
  9. }
  10. }

addSpatialIndex Function Public


Defined database/schemaGenerators.js

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

Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. this.addIndex(columns, merge({}, {type:"spatial"}, opts));
  4. }

addUniqueConstraint Function Public


Defined database/schemaGenerators.js

Add a unique constraint to the given column(s). See patio.SchemaGenerator#constraint.

Example
  1. DB.alterTable("test", function(){
  2. this.addUniqueConstraint("name");
  3. //=> ADD UNIQUE (name)
  4. this.addUniqueConstraint("name", {name : "uniqueName});
  5. //=> ADD CONSTRAINT uniqueName UNIQUE (name)
  6. });
Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. this.operations.push(merge({op:"addConstraint", type:"unique", columns:toArray(columns)}, opts));
  4. }

dropColumn Function Public


Defined database/schemaGenerators.js

Remove a column from the DDL for the table.

Example
  1. DB.alterTable("albums", function(){
  2. this.dropColumn("artist_id");
  3. //=>DROP COLUMN artist_id
  4. });
Arguments Source
  1. function (name){
  2. this.operations.push({op:"dropColumn", name:name});
  3. }

dropConstraint Function Public


Defined database/schemaGenerators.js

Remove a constraint from the DDL for the table.

Example
  1. DB.alterTable("test", function(){
  2. this.dropConstraint("constraint_name");
  3. //=>DROP CONSTRAINT constraint_name
  4. });
Arguments Source
  1. function (name){
  2. this.operations.push({op:"dropConstraint", name:name});
  3. }

dropIndex Function Public


Defined database/schemaGenerators.js

Remove an index from the DDL for the table.

Example
  1. DB.alterTable("albums", function(){
  2. this.dropIndex("artist_id")
  3. //=>DROP INDEX table_artist_id_index
  4. this.dropIndex(["a", "b"])
  5. //=>DROP INDEX table_a_b_index
  6. this.dropIndex(["a", "b"], {name : "foo"})
  7. //=>DROP INDEX foo
  8. });
Arguments Source
  1. function (columns,opts){
  2. opts = opts || {};
  3. this.operations.push(merge({op:"dropIndex", columns:toArray(columns)}, opts));
  4. }

noInherit Function Public


Defined database/schemaGenerators.js

Remove a child table's inheritance from a parent table.

Example
  1. DB.alterTable("test", function () {
  2. this.noInherit("parentTable");
  3. //=>ALTER TABLE test NO INHERIT parent_table
  4. });
Arguments Source
  1. function (name){
  2. this.operations.push({op:"noInherit", name: name});
  3. }

renameColumn Function Public


Defined database/schemaGenerators.js

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

Example
  1. DB.alterTable("artist", function(){
  2. this.renameColumn("name", "artistName");
  3. //=> RENAME COLUMN name TO artist_name
  4. });
Arguments Source
  1. function (name,newName,opts){
  2. opts = opts || {};
  3. this.operations.push(merge({op:"renameColumn", name:name, newName:newName}, opts));
  4. }

setAllowNull Function Public


Defined database/schemaGenerators.js

Modify a column's NOT NULL constraint.

Example
  1. DB.alterTable("artist", function(){
  2. this.setColumnAllowNull("artist_name", false);
  3. //=> ALTER COLUMN artist_name SET NOT NULL
  4. });
Arguments Source
  1. function (name,allowNull){
  2. this.operations.push({op:"setColumnNull", name:name, "null":allowNull});
  3. }

setColumnDefault Function Public


Defined database/schemaGenerators.js

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

Example
  1. DB.alterTable("artist", function(){
  2. //=>this.setColumnDefault("artist_name", "a");
  3. //=> ALTER COLUMN artist_name SET DEFAULT 'a'
  4. });
Arguments Source
  1. function (name,def){
  2. this.operations.push({op:"setColumnDefault", name:name, "default":def});
  3. }

setColumnType Function Public


Defined database/schemaGenerators.js

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

Example
  1. DB.alterTable("artist", function(){
  2. this.setColumnType("artist_name", 'char(10)');
  3. //=> ALTER COLUMN artist_name TYPE char(10)
  4. });
Arguments Source
  1. function (name,type,opts){
  2. opts = opts || {};
  3. this.operations.push(merge({op:"setColumnType", name:name, type:type}, opts));
  4. }

Documentation generated using coddoc.