diff --git a/docs/metadata/column.md b/docs/metadata/column.md index 8154956..bc3bfcc 100644 --- a/docs/metadata/column.md +++ b/docs/metadata/column.md @@ -6,39 +6,13 @@ Column name -## type.name +## type - Type: `String` -> Examples: `'numeric', 'character varying'` +> Examples: `'bigint', 'numeric(16, 0)', 'character varying(255)'` -Type name(without modifier type) - -## type.raw - -- Type: `String` - -> Examples: `'numeric(16, 2)', 'character varying(255)'` -> -SQL name of a data type(possibly with a modifier type) - -## type.precision - -***(date | numeric) types*** - -- Type: `Number` - -## type.scale - -***numeric types*** - -- Type: `Number` - -## type.length - -***character types*** - -- Type: `Number` +SQL data type ## default @@ -53,11 +27,13 @@ Default value The column nullability ## identity + !> `postgres` dialect requires version 10 or later + - Types: - - PostgreSQL: `Object` - - MySQL: `Boolean` - + - PostgreSQL: `Object` + - MySQL: `Boolean` + ## identity.generation - Type: `String` diff --git a/lib/dialects/mysql/tables/parser.js b/lib/dialects/mysql/tables/parser.js index cbe2d77..f21cdac 100644 --- a/lib/dialects/mysql/tables/parser.js +++ b/lib/dialects/mysql/tables/parser.js @@ -13,45 +13,29 @@ exports.columns = columns => { const { column_name, column_type, - data_type, is_nullable, column_default, - numeric_precision, - numeric_scale, - datetime_precision, - character_maximum_length, auto_increment, } = column; column = { name: column_name, - type: { name: data_type }, + type: column_type, default: column_default, nullable: is_nullable === 'YES', identity: auto_increment === 'YES', }; if ( - /^smallint|mediumint|int|bigint/i.test(column_type) && - column_type.indexOf('zerofill') === -1 + /^smallint|mediumint|int|bigint/i.test(column.type) && + column.type.indexOf('zerofill') === -1 ) { /** * Removing unnecessary parameter for integers in older versions of `mysql` * @example `bigint(20)` => `bigint` * @example `bigint(20) zerofill` => `bigint(20) zerofill` */ - column.type.raw = column_type.replace(/\(.*\)/, ''); - } else { - column.type.raw = column_type; - } - - if (utils.exists(datetime_precision)) { - column.type.precision = datetime_precision; - } else if (utils.exists(character_maximum_length)) { - column.type.length = character_maximum_length; - } else if (utils.exists(numeric_precision) || utils.exists(numeric_scale)) { - column.type.precision = numeric_precision; - column.type.scale = numeric_scale; + column.type = column.type.replace(/\(.*\)/, ''); } return column; diff --git a/lib/dialects/mysql/tables/query-generator.js b/lib/dialects/mysql/tables/query-generator.js index 1e86055..f58e233 100644 --- a/lib/dialects/mysql/tables/query-generator.js +++ b/lib/dialects/mysql/tables/query-generator.js @@ -16,12 +16,7 @@ select column_name, column_default, is_nullable, - data_type, column_type, - numeric_precision, - numeric_scale, - datetime_precision, - character_maximum_length, case when extra = "auto_increment" then "YES" else "NO" end as auto_increment from information_schema.columns where concat(table_schema, '.', table_name) ${inTables} diff --git a/lib/dialects/postgres/tables/parser.js b/lib/dialects/postgres/tables/parser.js index 752aa16..135891b 100644 --- a/lib/dialects/postgres/tables/parser.js +++ b/lib/dialects/postgres/tables/parser.js @@ -7,7 +7,6 @@ 'use strict'; const helpers = require('../helpers'); -const utils = require('../../../utils'); const FOREIGN_KEY_DEFAULTS = { onUpdate: 'NO ACTION', @@ -20,15 +19,9 @@ exports.columns = columns => { const { column_name, is_nullable, - type_string, data_type, column_default, collation_name, - interval_precision, - numeric_precision, - numeric_scale, - datetime_precision, - character_maximum_length, is_identity, identity_generation, identity_start, @@ -39,7 +32,7 @@ exports.columns = columns => { } = column; column = { - type: { name: data_type, raw: type_string }, + type: data_type, name: column_name, default: column_default, nullable: is_nullable === 'YES', @@ -59,17 +52,6 @@ exports.columns = columns => { column.identity = null; } - if (utils.exists(datetime_precision)) { - column.type.precision = datetime_precision; - } else if (utils.exists(character_maximum_length)) { - column.type.length = character_maximum_length; - } else if (utils.exists(numeric_precision) || utils.exists(numeric_scale)) { - column.type.precision = numeric_precision; - column.type.scale = numeric_scale; - } else if (utils.exists(interval_precision)) { - column.type.precision = interval_precision; - } - return column; }); }; diff --git a/lib/dialects/postgres/tables/query-generator.js b/lib/dialects/postgres/tables/query-generator.js index 7bfdd1e..6105808 100644 --- a/lib/dialects/postgres/tables/query-generator.js +++ b/lib/dialects/postgres/tables/query-generator.js @@ -11,7 +11,7 @@ const helpers = require('../helpers'); exports.getConstraints = tables => { tables = tables.map(n => `'${helpers.quoteObjectName(n)}'`); return ` -${exports.publicSearchPath()}; +set search_path to public; select conname as name, c.contype as type, @@ -22,24 +22,18 @@ from pg_catalog.pg_constraint as c join pg_catalog.pg_namespace n on n.oid = cl.relnamespace where c.conrelid = any(array[${tables.join(', ')}]::regclass[]) order by 1; -${exports.resetSearchPath()}; +set search_path to default; `; }; exports.getColumns = tables => ` select n.nspname || '.' || t.relname as table, - pg_catalog.format_type(c.atttypid, c.atttypmod) as type_string, - ic.data_type, + pg_catalog.format_type(a.atttypid, a.atttypmod) as data_type, ic.collation_name, ic.column_default, ic.is_nullable, ic.column_name, - ic.numeric_precision, - ic.numeric_scale, - ic.interval_precision, - ic.datetime_precision, - ic.character_maximum_length, ic.is_identity, ic.identity_generation, ic.identity_start, @@ -47,11 +41,11 @@ select ic.identity_minimum, ic.identity_maximum, ic.identity_cycle -from pg_attribute c -join pg_class t on c.attrelid = t.oid +from pg_attribute a +join pg_class t on a.attrelid = t.oid join pg_namespace n on t.relnamespace = n.oid join information_schema.columns ic - on c.attname = ic.column_name + on a.attname = ic.column_name and t.relname = ic.table_name and n.nspname = ic.table_schema where n.nspname || '.' || t.relname = ${helpers.anyNamesSQL(tables)}; @@ -67,9 +61,6 @@ where schemaname || '.' || tablename = ${helpers.anyNamesSQL(tables)} and indexname not in (select conname from pg_catalog.pg_constraint); `; -exports.publicSearchPath = () => 'set search_path to public'; -exports.resetSearchPath = () => 'set search_path to default'; - exports.tableExist = table => ` select exists ( select 1 from pg_tables where schemaname || '.' || tablename = '${table}' diff --git a/test/mysql/__snapshots__/index.test.js.snap b/test/mysql/__snapshots__/index.test.js.snap index 3629651..a4d0a82 100644 --- a/test/mysql/__snapshots__/index.test.js.snap +++ b/test/mysql/__snapshots__/index.test.js.snap @@ -11,59 +11,35 @@ Object { "identity": false, "name": "id", "nullable": false, - "type": Object { - "name": "bigint", - "precision": 19, - "raw": "bigint", - "scale": 0, - }, + "type": "bigint", }, Object { "default": "noname", "identity": false, "name": "name", "nullable": true, - "type": Object { - "length": 255, - "name": "varchar", - "raw": "varchar(255)", - }, + "type": "varchar(255)", }, Object { "default": null, "identity": false, "name": "budget", "nullable": true, - "type": Object { - "name": "decimal", - "precision": 16, - "raw": "decimal(16,3)", - "scale": 3, - }, + "type": "decimal(16,3)", }, Object { "default": null, "identity": true, "name": "age", "nullable": false, - "type": Object { - "name": "bigint", - "precision": 19, - "raw": "bigint", - "scale": 0, - }, + "type": "bigint", }, Object { "default": null, "identity": false, "name": "child", "nullable": true, - "type": Object { - "name": "bigint", - "precision": 19, - "raw": "bigint", - "scale": 0, - }, + "type": "bigint", }, ], "foreignKeys": Array [ diff --git a/test/postgres/__snapshots__/index.test.js.snap b/test/postgres/__snapshots__/index.test.js.snap index b681579..53e172a 100644 --- a/test/postgres/__snapshots__/index.test.js.snap +++ b/test/postgres/__snapshots__/index.test.js.snap @@ -33,12 +33,7 @@ Object { "identity": null, "name": "id", "nullable": false, - "type": Object { - "name": "bigint", - "precision": 64, - "raw": "bigint", - "scale": 0, - }, + "type": "bigint", }, Object { "collate": null, @@ -46,12 +41,7 @@ Object { "identity": null, "name": "budget", "nullable": true, - "type": Object { - "name": "numeric", - "precision": 16, - "raw": "numeric(16,3)", - "scale": 3, - }, + "type": "numeric(16,3)", }, Object { "collate": null, @@ -59,12 +49,7 @@ Object { "identity": null, "name": "child", "nullable": true, - "type": Object { - "name": "bigint", - "precision": 64, - "raw": "bigint", - "scale": 0, - }, + "type": "bigint", }, Object { "collate": null, @@ -72,11 +57,7 @@ Object { "identity": null, "name": "name", "nullable": true, - "type": Object { - "length": 255, - "name": "character varying", - "raw": "character varying(255)", - }, + "type": "character varying(255)", }, Object { "collate": null, @@ -91,12 +72,7 @@ Object { }, "name": "age", "nullable": false, - "type": Object { - "name": "smallint", - "precision": 16, - "raw": "smallint", - "scale": 0, - }, + "type": "smallint", }, ], "foreignKeys": Array [ diff --git a/types/index.d.ts b/types/index.d.ts index 12418ec..35ff64d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -33,17 +33,9 @@ interface IdentityMetadata extends SequenceMetadata { generation: 'ALWAYS' | 'BY DEFAULT' } -interface ColumnType { - name: string, - raw: string, - length?: number, - precision?: number, - scale?: number, -} - interface Column { name: string, - type: ColumnType, + type: string, nullable: boolean, default: string, identity?: IdentityMetadata, @@ -82,8 +74,10 @@ interface ReadOptions { } interface ReadResult { - tables: Record, - sequences: Record, + // @ts-ignore + tables: Map, + // @ts-ignore + sequences: Map, } declare class Metalize {