2013-12-27 14:44:21 -05:00
|
|
|
module.exports = function(client) {
|
|
|
|
|
|
|
|
client.initSchema();
|
|
|
|
|
|
|
|
var tableSql;
|
|
|
|
var SchemaBuilder = client.SchemaBuilder;
|
|
|
|
var _ = require('lodash');
|
|
|
|
var equal = require('assert').equal;
|
|
|
|
var deepEqual = require('assert').deepEqual;
|
|
|
|
|
|
|
|
describe("PostgreSQL SchemaBuilder", function() {
|
|
|
|
|
|
|
|
it("basic create table", function() {
|
|
|
|
tableSql = new SchemaBuilder().createTable('users', function(table) {
|
|
|
|
table.increments('id');
|
|
|
|
table.string('email');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('create table "users" ("id" serial primary key, "email" varchar(255))');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("basic alter table", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.increments('id');
|
|
|
|
table.string('email');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "id" serial primary key, add column "email" varchar(255)');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop table", function() {
|
2014-04-15 11:43:47 -04:00
|
|
|
tableSql = new SchemaBuilder().dropTable('users').toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('drop table "users"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop table if exists", function() {
|
2014-04-15 11:43:47 -04:00
|
|
|
tableSql = new SchemaBuilder().dropTableIfExists('users').toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('drop table if exists "users"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop column", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropColumn('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" drop column "foo"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop multiple columns", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropColumn(['foo', 'bar']);
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" drop column "foo", drop column "bar"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop multiple columns with arguments", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropColumn('foo', 'bar');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" drop column "foo", drop column "bar"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop primary", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropPrimary();
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" drop constraint users_pkey');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop unique", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropUnique('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" drop constraint foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop index", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropIndex('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('drop index foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop foreign", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropForeign('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" drop constraint foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("drop timestamps", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dropTimestamps();
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" drop column "created_at", drop column "updated_at"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("rename table", function() {
|
2014-04-15 11:43:47 -04:00
|
|
|
tableSql = new SchemaBuilder().renameTable('users', 'foo').toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" rename to "foo"');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding primary key", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.primary('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add primary key ("foo")');
|
|
|
|
});
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
it("adding primary key fluently", function() {
|
|
|
|
tableSql = new SchemaBuilder().createTable('users', function(table) {
|
|
|
|
table.string('name').primary();
|
|
|
|
}).toSQL();
|
|
|
|
equal(2, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('create table "users" ("name" varchar(255))');
|
|
|
|
expect(tableSql[1].sql).to.equal('alter table "users" add primary key ("name")');
|
|
|
|
});
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
it("adding foreign key");
|
|
|
|
|
|
|
|
it("adding unique key", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.unique('foo', 'bar');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add constraint bar unique ("foo")');
|
|
|
|
});
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
it("adding unique key fluently", function() {
|
|
|
|
tableSql = new SchemaBuilder().createTable('users', function(table) {
|
|
|
|
table.string('email').unique();
|
|
|
|
}).toSQL();
|
|
|
|
equal(2, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('create table "users" ("email" varchar(255))');
|
|
|
|
expect(tableSql[1].sql).to.equal('alter table "users" add constraint users_email_unique unique ("email")');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding index without value", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.index(['foo', 'bar']);
|
|
|
|
}).toSQL();
|
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('create index users_foo_bar_index on "users" ("foo", "bar")');
|
|
|
|
});
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
it("adding index", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.index(['foo', 'bar'], 'baz');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('create index baz on "users" ("foo", "bar")');
|
|
|
|
});
|
|
|
|
|
2014-04-15 11:43:47 -04:00
|
|
|
it("adding index fluently", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.string('name').index();
|
|
|
|
}).toSQL();
|
|
|
|
equal(2, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "name" varchar(255)');
|
|
|
|
expect(tableSql[1].sql).to.equal('create index users_name_index on "users" ("name")');
|
|
|
|
});
|
|
|
|
|
2013-12-27 14:44:21 -05:00
|
|
|
it("adding incrementing id", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.increments('id');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "id" serial primary key');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding big incrementing id", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.bigIncrements('id');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "id" bigserial primary key');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding string", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.string('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" varchar(255)');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding varchar with length", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.string('foo', 100);
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" varchar(100)');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding a string with a default", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.string('foo', 100).defaultTo('bar');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" varchar(100) default \'bar\'');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding text", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.text('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" text');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding big integer", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.bigInteger('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" bigint');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("tests a big integer as the primary autoincrement key", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.bigIncrements('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" bigserial primary key');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding integer", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.integer('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" integer');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding autoincrement integer", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.increments('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" serial primary key');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding medium integer", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.mediumint('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" integer');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding tiny integer", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.tinyint('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" smallint');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding small integer", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.smallint('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" smallint');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding float", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.float('foo', 5, 2);
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" real');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding double", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.double('foo', 15, 8);
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" double precision');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding decimal", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.decimal('foo', 5, 2);
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" decimal(5, 2)');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding boolean", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.boolean('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" boolean');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding enum", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.enum('foo', ['bar', 'baz']);
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" text check (foo in (\'bar\', \'baz\'))');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding date", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.date('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" date');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding date time", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.dateTime('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" timestamp');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding time", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.time('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" time');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding timestamp", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.timestamp('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" timestamp');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding timestamps", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.timestamps();
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "created_at" timestamp, add column "updated_at" timestamp');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("adding binary", function() {
|
|
|
|
tableSql = new SchemaBuilder().table('users', function(table) {
|
|
|
|
table.binary('foo');
|
2014-04-15 11:43:47 -04:00
|
|
|
}).toSQL();
|
2013-12-27 14:44:21 -05:00
|
|
|
equal(1, tableSql.length);
|
|
|
|
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" bytea');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|