Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PostgreSQL] Improve parsing support for postgresql data types #73

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions lib/dialects/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type RawColumn = {
export function rawColumnToColumn(rawColumn: RawColumn): Column {
return {
...rawColumn,
default_value_raw: rawColumn.default_value,
default_value: parseDefaultValue(rawColumn.default_value),
generation_expression: rawColumn.generation_expression || null,
is_generated: !!rawColumn.is_generated,
Expand Down
1 change: 1 addition & 0 deletions lib/dialects/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function rawColumnToColumn(rawColumn: RawColumn): Column {
name: rawColumn.COLUMN_NAME,
table: rawColumn.TABLE_NAME,
data_type: dataType,
default_value_raw: rawColumn.COLUMN_DEFAULT,
default_value: parseDefaultValue(rawColumn.COLUMN_DEFAULT),
generation_expression: rawColumn.GENERATION_EXPRESSION || null,
max_length: rawColumn.CHARACTER_MAXIMUM_LENGTH,
Expand Down
1 change: 1 addition & 0 deletions lib/dialects/oracledb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function rawColumnToColumn(rawColumn: RawColumn): Column {
name: rawColumn.COLUMN_NAME,
table: rawColumn.TABLE_NAME,
data_type: rawColumn.DATA_TYPE,
default_value_raw: !is_generated ? default_value : null,
default_value: !is_generated ? default_value : null,
generation_expression: is_generated ? default_value : null,
max_length: rawColumn.DATA_LENGTH,
Expand Down
95 changes: 86 additions & 9 deletions lib/dialects/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@ import { Column } from '../types/column';
import { ForeignKey } from '../types/foreign-key';
import { stripQuotes } from '../utils/strip-quotes';
import isNil from 'lodash.isnil';
import { builtins, getTypeParser } from 'pg-types';

const pgTypes = builtins as any;

pgTypes['SMALLINT'] = pgTypes['INT2'];
pgTypes['INTEGER'] = pgTypes['INT4'];
pgTypes['BIGINT'] = pgTypes['INT8'];
pgTypes['REAL'] = pgTypes['FLOAT4'];
pgTypes['DOUBLE_PRECISION'] = pgTypes['FLOAT8'];
pgTypes['BOOLEAN'] = pgTypes['BOOL'];
pgTypes['CHARACTER'] = pgTypes['VARCHAR'];
pgTypes['CHARACTER_VARYING'] = pgTypes['VARCHAR'];
pgTypes['TIMESTAMP_WITHOUT_TIME_ZONE'] = pgTypes['TIMESTAMP'];
pgTypes['TIMESTAMP_WITH_TIME_ZONE'] = pgTypes['TIMESTAMPTZ'];
pgTypes['_SMALLINT'] = 1005;
pgTypes['_INTEGER'] = 1007;
pgTypes['_BIGINT'] = 1016;
pgTypes['_REAL'] = 1021;
pgTypes['_DOUBLE_PRECISION'] = 1022;
pgTypes['_BOOLEAN'] = 1000;
pgTypes['_BPCHAR'] = 1014;
pgTypes['_CHARACTER_VARYING'] = 1015;
pgTypes['_TIMESTAMP_WITHOUT_TIME_ZONE'] = 1115;
pgTypes['_TIMESTAMP_WITH_TIME_ZONE'] = 1185;
pgTypes['_DATE'] = 1182;

type RawTable = {
table_name: string;
Expand Down Expand Up @@ -39,7 +64,11 @@ export function rawColumnToColumn(rawColumn: RawColumn): Column {
name: rawColumn.column_name,
table: rawColumn.table_name,
data_type: rawColumn.data_type,
default_value: parseDefaultValue(rawColumn.column_default),
default_value_raw: rawColumn.column_default,
default_value: parseDefaultValue(
rawColumn.column_default,
rawColumn.data_type
),
generation_expression: rawColumn.generation_expression || null,
max_length: rawColumn.character_maximum_length,
numeric_precision: rawColumn.numeric_precision,
Expand All @@ -58,22 +87,70 @@ export function rawColumnToColumn(rawColumn: RawColumn): Column {
};
}

/**
* Converts Postgres default array to JS
* Eg `array['{"text":"Lorem Ipsum"}']::json[],` => [{"text":"Lorem Ipsum"}]
*/
function parseDefaultArray(column_default: string): any {
// case when '{el1,el2,el3}'::cast[]
if (column_default.startsWith("'{")) {
let [value, cast] = column_default.split('::');

cast = cast.substring(0, cast.length - 2).replace(/ /g, '_');
value = value.replace(/^\'([\s\S]*)\'$/, '$1');

const code = pgTypes['_' + cast.toUpperCase()];

const parser = getTypeParser(code);

return parser(value);
}

// case when ARRAY[el1::cast,el2::cast]
if (column_default.startsWith('ARRAY[]')) return [];

// For any other case, information_schema.data_type
// is not enough to identify the appropriate parser
// for each array element
return null;
}

/**
* Converts Postgres default value to JS
* Eg `'example'::character varying` => `example`
*/
export function parseDefaultValue(type: string | null) {
if (isNil(type)) return null;
if (type.startsWith('nextval(')) return type;
export function parseDefaultValue(
column_default: string | null,
column_type: string
) {
if (isNil(column_default)) return null;
if (column_default.startsWith('nextval(')) return column_default;

try {
if (column_type === 'ARRAY') return parseDefaultArray(column_default);

let [value, cast] = type.split('::');
let [value, cast] = column_default.split('::');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the value contains "::", i think you must search the last "::"


value = value.replace(/^\'([\s\S]*)\'$/, '$1');
value = value.replace(/^\'([\s\S]*)\'$/, '$1');

if (/.*json.*/.test(cast)) return JSON.parse(value);
if (/.*(char|text).*/.test(cast)) return String(value);
if (!cast) {
cast = column_type;
}

return isNaN(value as any) ? value : Number(value);
cast = cast.toUpperCase().replace(/ /g, '_');

if (!pgTypes.hasOwnProperty(cast)) {
throw `type ${cast} not supported`;
}

const code = pgTypes[cast];

const parser = getTypeParser(code);

return parser(value);
} catch (e: any) {
return null;
}
}

export default class Postgres implements SchemaInspector {
Expand Down
1 change: 1 addition & 0 deletions lib/dialects/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export default class SQLite implements SchemaInspector {
name: raw.name,
table: table,
data_type: extractType(raw.type),
default_value_raw: raw.dflt_value,
default_value: stripQuotes(raw.dflt_value),
max_length: extractMaxLength(raw.type),
/** @NOTE SQLite3 doesn't support precision/scale */
Expand Down
1 change: 1 addition & 0 deletions lib/types/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface Column {
name: string;
table: string;
data_type: string;
default_value_raw: any | null;
default_value: any | null;
max_length: number | null;
numeric_precision: number | null;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
},
"dependencies": {
"lodash.flatten": "^4.4.0",
"lodash.isnil": "^4.0.0"
"lodash.isnil": "^4.0.0",
"pg-types": "^3.0.1"
}
}
25 changes: 25 additions & 0 deletions test/mssql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe('mssql', () => {
name: 'id',
table: 'teams',
data_type: 'int',
default_value_raw: null,
default_value: null,
max_length: 4,
numeric_precision: 10,
Expand All @@ -141,6 +142,7 @@ describe('mssql', () => {
name: 'uuid',
table: 'teams',
data_type: 'char',
default_value_raw: null,
default_value: null,
max_length: 36,
numeric_precision: null,
Expand All @@ -158,6 +160,7 @@ describe('mssql', () => {
name: 'name',
table: 'teams',
data_type: 'nvarchar',
default_value_raw: null,
default_value: null,
max_length: 100,
numeric_precision: null,
Expand All @@ -175,6 +178,7 @@ describe('mssql', () => {
name: 'name_upper',
table: 'teams',
data_type: 'nvarchar',
default_value_raw: null,
default_value: null,
max_length: 100,
numeric_precision: null,
Expand All @@ -192,6 +196,7 @@ describe('mssql', () => {
name: 'description',
table: 'teams',
data_type: 'varchar',
default_value_raw: null,
default_value: null,
max_length: -1,
numeric_precision: null,
Expand All @@ -209,6 +214,7 @@ describe('mssql', () => {
name: 'credits',
table: 'teams',
data_type: 'int',
default_value_raw: null,
default_value: null,
max_length: 4,
numeric_precision: 10,
Expand All @@ -226,6 +232,7 @@ describe('mssql', () => {
name: 'created_at',
table: 'teams',
data_type: 'datetime2',
default_value_raw: null,
default_value: null,
max_length: 6,
numeric_precision: 19,
Expand All @@ -243,6 +250,7 @@ describe('mssql', () => {
name: 'activated_at',
table: 'teams',
data_type: 'date',
default_value_raw: null,
default_value: null,
max_length: 3,
numeric_precision: 10,
Expand All @@ -260,6 +268,7 @@ describe('mssql', () => {
name: 'id',
table: 'users',
data_type: 'int',
default_value_raw: null,
default_value: null,
max_length: 4,
numeric_precision: 10,
Expand All @@ -277,6 +286,7 @@ describe('mssql', () => {
name: 'team_id',
table: 'users',
data_type: 'int',
default_value_raw: null,
default_value: null,
max_length: 4,
numeric_precision: 10,
Expand All @@ -294,6 +304,7 @@ describe('mssql', () => {
name: 'email',
table: 'users',
data_type: 'varchar',
default_value_raw: null,
default_value: null,
max_length: 100,
numeric_precision: null,
Expand All @@ -311,6 +322,7 @@ describe('mssql', () => {
name: 'password',
table: 'users',
data_type: 'varchar',
default_value_raw: null,
default_value: null,
max_length: 60,
numeric_precision: null,
Expand All @@ -328,6 +340,7 @@ describe('mssql', () => {
name: 'status',
table: 'users',
data_type: 'varchar',
default_value_raw: "('active')",
default_value: 'active',
max_length: 60,
numeric_precision: null,
Expand All @@ -345,6 +358,7 @@ describe('mssql', () => {
name: 'request_path',
table: 'page_visits',
data_type: 'varchar',
default_value_raw: null,
default_value: null,
max_length: 100,
numeric_precision: null,
Expand All @@ -362,6 +376,7 @@ describe('mssql', () => {
name: 'user_agent',
table: 'page_visits',
data_type: 'varchar',
default_value_raw: null,
default_value: null,
max_length: 200,
numeric_precision: null,
Expand All @@ -379,6 +394,7 @@ describe('mssql', () => {
name: 'created_at',
table: 'page_visits',
data_type: 'datetime2',
default_value_raw: null,
default_value: null,
max_length: 6,
numeric_precision: 19,
Expand All @@ -400,6 +416,7 @@ describe('mssql', () => {
name: 'id',
table: 'teams',
data_type: 'int',
default_value_raw: null,
default_value: null,
max_length: 4,
numeric_precision: 10,
Expand All @@ -417,6 +434,7 @@ describe('mssql', () => {
name: 'uuid',
table: 'teams',
data_type: 'char',
default_value_raw: null,
default_value: null,
max_length: 36,
numeric_precision: null,
Expand All @@ -434,6 +452,7 @@ describe('mssql', () => {
name: 'name',
table: 'teams',
data_type: 'nvarchar',
default_value_raw: null,
default_value: null,
max_length: 100,
numeric_precision: null,
Expand All @@ -451,6 +470,7 @@ describe('mssql', () => {
name: 'name_upper',
table: 'teams',
data_type: 'nvarchar',
default_value_raw: null,
default_value: null,
max_length: 100,
numeric_precision: null,
Expand All @@ -468,6 +488,7 @@ describe('mssql', () => {
name: 'description',
table: 'teams',
data_type: 'varchar',
default_value_raw: null,
default_value: null,
max_length: -1,
numeric_precision: null,
Expand All @@ -485,6 +506,7 @@ describe('mssql', () => {
name: 'credits',
table: 'teams',
data_type: 'int',
default_value_raw: null,
default_value: null,
max_length: 4,
numeric_precision: 10,
Expand All @@ -502,6 +524,7 @@ describe('mssql', () => {
name: 'created_at',
table: 'teams',
data_type: 'datetime2',
default_value_raw: null,
default_value: null,
max_length: 6,
numeric_precision: 19,
Expand All @@ -519,6 +542,7 @@ describe('mssql', () => {
name: 'activated_at',
table: 'teams',
data_type: 'date',
default_value_raw: null,
default_value: null,
max_length: 3,
numeric_precision: 10,
Expand All @@ -539,6 +563,7 @@ describe('mssql', () => {
name: 'uuid',
table: 'teams',
data_type: 'char',
default_value_raw: null,
default_value: null,
max_length: 36,
numeric_precision: null,
Expand Down
Loading