-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(casts): support create and drop cast (#942)
Co-authored-by: Shinigami92 <[email protected]>
- Loading branch information
1 parent
569d173
commit ee76beb
Showing
12 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Cast Operations | ||
|
||
### `pgm.createCast( source_type, target_type, opttions )` | ||
|
||
> Create a new cast - [postgres docs](https://www.postgresql.org/docs/current/sql-createcast.html) | ||
**Arguments** | ||
|
||
- `source_type` _[string]_ - The name of the source data type of the cast | ||
- `target_type` _[string]_ - The name of the target data type of the cast | ||
- `options` _[object]_ - options: | ||
|
||
- `functionName` _[string]_ - Name of function to use to do the cast | ||
- `argumentTypes` _[array]_ - Array of types of arguments for the function | ||
|
||
Array of strings listing the types of arguments for the conversion | ||
function. If this is not present, it defaults to just the `source_type`. | ||
|
||
- `inout` _[boolean]_ - Use standard I/O routines for conversion | ||
|
||
Setting this to `true` indicates that conversion should be used by using | ||
the standard text output conversion for `source_type` and passing the | ||
result to the input conversion process for `target_type`. | ||
|
||
- `as` _[string]_ - Indicate when this may cast may be done implicitly. | ||
|
||
This may be either `assignment` or `implicit`. If `implicit` is used, the | ||
cast may be used implicitly in any context; this is not recommended. If | ||
`assignment` is used the cast will only be done implicitly in | ||
assignments. | ||
|
||
**Reverse Operation:** `dropCast` | ||
|
||
--- | ||
|
||
### `pgm.dropCast( source_type, target_type )` | ||
|
||
> Drop a cast - [postgres docs](https://www.postgresql.org/docs/current/sql-dropcast.html) | ||
**Arguments** | ||
|
||
- `source_type` _[string]_ - The name of the source data type of the cast | ||
- `target_type` _[string]_ - The name of the target data type of the cast | ||
- `options` _[object]_ - options: | ||
- `ifExists` _[boolean]_ - Do not throw an error if the cast does not exist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { MigrationOptions } from '../../types'; | ||
import type { Name, Reversible } from '../generalTypes'; | ||
import type { DropCastOptions } from './dropCast'; | ||
import { dropCast } from './dropCast'; | ||
|
||
export interface CreateCastWithFunctionOptions { | ||
functionName: Name; | ||
argumentTypes?: string[]; | ||
inout?: undefined; | ||
} | ||
|
||
export interface CreateCastWithoutFunctionOptions { | ||
functionName?: undefined; | ||
argumentTypes?: undefined; | ||
inout?: undefined; | ||
} | ||
|
||
export interface CreateCastWithInoutOptions { | ||
functionName?: undefined; | ||
argumentTypes?: undefined; | ||
inout: boolean; | ||
} | ||
|
||
export type CreateCastOptions = ( | ||
| CreateCastWithFunctionOptions | ||
| CreateCastWithoutFunctionOptions | ||
| CreateCastWithInoutOptions | ||
) & { | ||
as?: 'ASSIGNMENT' | 'IMPLICIT'; | ||
}; | ||
|
||
export type CreateCastFn = ( | ||
fromType: string, | ||
toType: string, | ||
options: CreateCastOptions & DropCastOptions | ||
) => string; | ||
|
||
export type CreateCast = Reversible<CreateCastFn>; | ||
|
||
export function createCast(mOptions: MigrationOptions): CreateCast { | ||
const _create: CreateCast = (sourceType, targetType, options = {}) => { | ||
const { functionName, argumentTypes, inout, as } = options; | ||
|
||
let conversion = ''; | ||
if (functionName) { | ||
const args = argumentTypes || [sourceType]; | ||
conversion = ` WITH FUNCTION ${mOptions.literal(functionName)}(${args.join(', ')})`; | ||
} else if (inout) { | ||
conversion = ' WITH INOUT'; | ||
} else { | ||
conversion = ' WITHOUT FUNCTION'; | ||
} | ||
|
||
const implicit = as ? ` AS ${as}` : ''; | ||
|
||
return `CREATE CAST (${sourceType} AS ${targetType})${conversion}${implicit};`; | ||
}; | ||
|
||
_create.reverse = dropCast(mOptions); | ||
|
||
return _create; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { MigrationOptions } from '../../types'; | ||
import type { DropOptions } from '../generalTypes'; | ||
|
||
export type DropCastOptions = Omit<DropOptions, 'cascade'>; | ||
|
||
export type DropCast = ( | ||
fromType: string, | ||
toType: string, | ||
dropOptions: DropCastOptions | ||
) => string; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export function dropCast(mOptions: MigrationOptions): DropCast { | ||
const _drop: DropCast = (sourceType, targetType, options = {}) => { | ||
const { ifExists } = options; | ||
const ifExistsStr = ifExists ? ' IF EXISTS' : ''; | ||
|
||
return `DROP CAST${ifExistsStr} (${sourceType} AS ${targetType});`; | ||
}; | ||
|
||
return _drop; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './createCast'; | ||
export * from './dropCast'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
exports.up = (pgm) => { | ||
pgm.createCast('varchar', 'integer', { | ||
inout: true, | ||
as: 'IMPLICIT', | ||
}); | ||
|
||
pgm.createFunction( | ||
'text_to_integer', | ||
['text'], | ||
{ | ||
returns: 'integer', | ||
language: 'plpgsql', | ||
}, | ||
` | ||
BEGIN | ||
RETURN CAST($1 AS integer); | ||
END; | ||
` | ||
); | ||
pgm.createCast('text', 'integer', { | ||
functionName: 'text_to_integer', | ||
argumentTypes: ['text'], | ||
as: 'ASSIGNMENT', | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { PgType } from '../../../src'; | ||
import { createCast } from '../../../src/operations/casts'; | ||
import { options1 } from '../../presetMigrationOptions'; | ||
|
||
describe('operations', () => { | ||
describe('casts', () => { | ||
describe('createCast', () => { | ||
const createCastFn = createCast(options1); | ||
|
||
it('should return a function', () => { | ||
expect(createCastFn).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement with string', () => { | ||
const statement = createCastFn('bigint', 'int4', {}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
'CREATE CAST (bigint AS int4) WITHOUT FUNCTION;' | ||
); | ||
}); | ||
|
||
it('should return sql statement with PgType', () => { | ||
const statement = createCastFn(PgType.BIGINT, PgType.INT4, {}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
'CREATE CAST (bigint AS int4) WITHOUT FUNCTION;' | ||
); | ||
}); | ||
|
||
it('should return sql statement with schema', () => { | ||
const statement = createCastFn('bigint', 'int4', { | ||
functionName: { name: 'add', schema: 'myschema' }, | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
'CREATE CAST (bigint AS int4) WITH FUNCTION "myschema"."add"(bigint);' | ||
); | ||
}); | ||
|
||
it('should return sql statement with castOptions with function', () => { | ||
const statement = createCastFn('bigint', 'int4', { | ||
functionName: 'add', | ||
argumentTypes: ['integer', 'integer'], | ||
as: 'ASSIGNMENT', | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
'CREATE CAST (bigint AS int4) WITH FUNCTION "add"(integer, integer) AS ASSIGNMENT;' | ||
); | ||
}); | ||
|
||
it('should return sql statement with castOptions without function', () => { | ||
const statement = createCastFn('bigint', 'int4', { | ||
as: 'IMPLICIT', | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
'CREATE CAST (bigint AS int4) WITHOUT FUNCTION AS IMPLICIT;' | ||
); | ||
}); | ||
|
||
it('should return sql statement with castOptions with inout', () => { | ||
const statement = createCastFn('bigint', 'int4', { | ||
inout: true, | ||
as: 'ASSIGNMENT', | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe( | ||
'CREATE CAST (bigint AS int4) WITH INOUT AS ASSIGNMENT;' | ||
); | ||
}); | ||
|
||
describe('reverse', () => { | ||
it('should contain a reverse function', () => { | ||
expect(createCastFn.reverse).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = createCastFn.reverse('bigint', 'int4', {}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe('DROP CAST (bigint AS int4);'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { dropCast } from '../../../src/operations/casts'; | ||
import { options1 } from '../../presetMigrationOptions'; | ||
|
||
describe('operations', () => { | ||
describe('casts', () => { | ||
describe('dropCast', () => { | ||
const dropCastFn = dropCast(options1); | ||
|
||
it('should return a function', () => { | ||
expect(dropCastFn).toBeTypeOf('function'); | ||
}); | ||
|
||
it('should return sql statement', () => { | ||
const statement = dropCastFn('bigint', 'int4', {}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe('DROP CAST (bigint AS int4);'); | ||
}); | ||
|
||
it('should return sql statement with dropOptions', () => { | ||
const statement = dropCastFn('bigint', 'int4', { | ||
ifExists: true, | ||
}); | ||
|
||
expect(statement).toBeTypeOf('string'); | ||
expect(statement).toBe('DROP CAST IF EXISTS (bigint AS int4);'); | ||
}); | ||
}); | ||
}); | ||
}); |