diff --git a/src/operations/tables.ts b/src/operations/tables.ts index df153033..72842532 100644 --- a/src/operations/tables.ts +++ b/src/operations/tables.ts @@ -481,7 +481,7 @@ export function dropColumns(mOptions: MigrationOptions): DropColumns { const columnsStr = formatLines( columns.map(mOptions.literal), - ` DROP ${ifExists ? ' IF EXISTS' : ''}`, + ` DROP ${ifExists ? 'IF EXISTS ' : ''}`, `${cascade ? ' CASCADE' : ''},` ); diff --git a/test/operations/columns/addColumns.spec.ts b/test/operations/columns/addColumns.spec.ts new file mode 100644 index 00000000..e6025da1 --- /dev/null +++ b/test/operations/columns/addColumns.spec.ts @@ -0,0 +1,87 @@ +import { describe, expect, it } from 'vitest'; +import { PgType } from '../../../src'; +import { addColumns } from '../../../src/operations/tables'; +import { options1 } from '../../utils'; + +describe('operations', () => { + describe('columns', () => { + describe('addColumns', () => { + const addColumnsFn = addColumns(options1); + + it('should return a function', () => { + expect(addColumnsFn).toBeTypeOf('function'); + }); + + it('should return sql statement', () => { + const statement = addColumnsFn('transactions', { + status: { + type: 'varchar(30)', + default: 'old', + }, + mtime: { + type: PgType.TIMESTAMP_WITH_TIME_ZONE, + default: 'now()', + }, + }); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe(`ALTER TABLE "transactions" + ADD "status" varchar(30) DEFAULT $pga$old$pga$, + ADD "mtime" timestamp with time zone DEFAULT $pga$now()$pga$;`); + }); + + it('should return sql statement with columnOptions', () => { + const statement = addColumnsFn( + 'transactions', + { + status: { + type: 'varchar(30)', + default: 'old', + }, + }, + { + ifNotExists: true, + } + ); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe(`ALTER TABLE "transactions" + ADD IF NOT EXISTS "status" varchar(30) DEFAULT $pga$old$pga$;`); + }); + + it('should return sql statement with schema', () => { + const statement = addColumnsFn( + { schema: 'myschema', name: 'distributors' }, + { + status: PgType.VARCHAR, + } + ); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe( + `ALTER TABLE "myschema"."distributors" + ADD "status" varchar;` + ); + }); + + describe('reverse', () => { + it('should contain a reverse function', () => { + expect(addColumnsFn.reverse).toBeTypeOf('function'); + }); + + it('should return sql statement', () => { + const statement = addColumnsFn.reverse('transactions', { + status: { + type: 'varchar(30)', + default: 'old', + }, + }); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe(`ALTER TABLE "transactions" + DROP "status";`); + }); + }); + }); + }); +}); diff --git a/test/operations/columns/alterColumn.spec.ts b/test/operations/columns/alterColumn.spec.ts new file mode 100644 index 00000000..2d53f2fe --- /dev/null +++ b/test/operations/columns/alterColumn.spec.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest'; +import { alterColumn } from '../../../src/operations/tables'; +import { options1 } from '../../utils'; + +describe('operations', () => { + describe('columns', () => { + describe('alterColumn', () => { + const alterColumnFn = alterColumn(options1); + + it('should return a function', () => { + expect(alterColumnFn).toBeTypeOf('function'); + }); + + it('should return sql statement', () => { + const statement = alterColumnFn('distributors', 'address', {}); + + expect(statement).toBeTypeOf('string'); + expect(statement).toHaveLength(0); + }); + + it('should return sql statement with columnOptions', () => { + const statement = alterColumnFn('distributors', 'address', { + default: null, + type: 'varchar(30)', + collation: 'C', + using: 'address::text', + notNull: false, + sequenceGenerated: null, + comment: 'Address of the distributor', + }); + + expect(statement).toBeTypeOf('string'); + expect(statement).toStrictEqual( + `ALTER TABLE "distributors" + ALTER "address" DROP DEFAULT, + ALTER "address" SET DATA TYPE varchar(30) COLLATE C USING address::text, + ALTER "address" DROP NOT NULL, + ALTER "address" DROP IDENTITY; +COMMENT ON COLUMN "distributors"."address" IS $pga$Address of the distributor$pga$;` + ); + }); + }); + }); +}); diff --git a/test/operations/columns/dropColumns.spec.ts b/test/operations/columns/dropColumns.spec.ts new file mode 100644 index 00000000..09eb6d7e --- /dev/null +++ b/test/operations/columns/dropColumns.spec.ts @@ -0,0 +1,58 @@ +import { describe, expect, it } from 'vitest'; +import { dropColumns } from '../../../src/operations/tables'; +import { options1 } from '../../utils'; + +describe('operations', () => { + describe('columns', () => { + describe('dropColumns', () => { + const dropColumnsFn = dropColumns(options1); + + it('should return a function', () => { + expect(dropColumnsFn).toBeTypeOf('function'); + }); + + it('should return sql statement', () => { + const statement = dropColumnsFn('distributors', 'address'); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe( + `ALTER TABLE "distributors" + DROP "address";` + ); + }); + + it('should return sql statement with dropOptions', () => { + const statement = dropColumnsFn( + 'distributors', + { + address: 'varchar(30)', + }, + { + ifExists: true, + cascade: true, + } + ); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe(`ALTER TABLE "distributors" + DROP IF EXISTS "address";`); + }); + + it('should return sql statement with schema', () => { + const statement = dropColumnsFn( + { + schema: 'myschema', + name: 'distributors', + }, + { + address: 'varchar(30)', + } + ); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe(`ALTER TABLE "myschema"."distributors" + DROP "address";`); + }); + }); + }); +}); diff --git a/test/operations/columns/renameColumn.spec.ts b/test/operations/columns/renameColumn.spec.ts new file mode 100644 index 00000000..d59c1ee7 --- /dev/null +++ b/test/operations/columns/renameColumn.spec.ts @@ -0,0 +1,56 @@ +import { describe, expect, it } from 'vitest'; +import { renameColumn } from '../../../src/operations/tables'; +import { options1 } from '../../utils'; + +describe('operations', () => { + describe('columns', () => { + describe('renameColumn', () => { + const renameColumnFn = renameColumn(options1); + + it('should return a function', () => { + expect(renameColumnFn).toBeTypeOf('function'); + }); + + it('should return sql statement', () => { + const statement = renameColumnFn('distributors', 'address', 'city'); + + expect(statement).toBeTypeOf('string'); + expect(statement).toStrictEqual( + 'ALTER TABLE "distributors" RENAME "address" TO "city";' + ); + }); + + it('should return sql statement with schema', () => { + const statement = renameColumnFn( + { name: 'distributors', schema: 'myschema' }, + 'address', + 'city' + ); + + expect(statement).toBeTypeOf('string'); + expect(statement).toStrictEqual( + 'ALTER TABLE "myschema"."distributors" RENAME "address" TO "city";' + ); + }); + + describe('reverse', () => { + it('should contain a reverse function', () => { + expect(renameColumnFn.reverse).toBeTypeOf('function'); + }); + + it('should return sql statement', () => { + const statement = renameColumnFn.reverse( + 'distributors', + 'address', + 'city' + ); + + expect(statement).toBeTypeOf('string'); + expect(statement).toBe( + 'ALTER TABLE "distributors" RENAME "city" TO "address";' + ); + }); + }); + }); + }); +});