Skip to content

Commit

Permalink
'column.type' is now a string
Browse files Browse the repository at this point in the history
  • Loading branch information
multum committed Mar 20, 2020
1 parent 222a4ec commit 3b19912
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 160 deletions.
40 changes: 8 additions & 32 deletions docs/metadata/column.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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`
Expand Down
24 changes: 4 additions & 20 deletions lib/dialects/mysql/tables/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions lib/dialects/mysql/tables/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
20 changes: 1 addition & 19 deletions lib/dialects/postgres/tables/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
'use strict';

const helpers = require('../helpers');
const utils = require('../../../utils');

const FOREIGN_KEY_DEFAULTS = {
onUpdate: 'NO ACTION',
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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;
});
};
Expand Down
21 changes: 6 additions & 15 deletions lib/dialects/postgres/tables/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,36 +22,30 @@ 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,
ic.identity_increment,
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)};
Expand All @@ -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}'
Expand Down
34 changes: 5 additions & 29 deletions test/mysql/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
34 changes: 5 additions & 29 deletions test/postgres/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,31 @@ Object {
"identity": null,
"name": "id",
"nullable": false,
"type": Object {
"name": "bigint",
"precision": 64,
"raw": "bigint",
"scale": 0,
},
"type": "bigint",
},
Object {
"collate": null,
"default": null,
"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,
"default": null,
"identity": null,
"name": "child",
"nullable": true,
"type": Object {
"name": "bigint",
"precision": 64,
"raw": "bigint",
"scale": 0,
},
"type": "bigint",
},
Object {
"collate": null,
"default": "'noname'::character varying",
"identity": null,
"name": "name",
"nullable": true,
"type": Object {
"length": 255,
"name": "character varying",
"raw": "character varying(255)",
},
"type": "character varying(255)",
},
Object {
"collate": null,
Expand All @@ -91,12 +72,7 @@ Object {
},
"name": "age",
"nullable": false,
"type": Object {
"name": "smallint",
"precision": 16,
"raw": "smallint",
"scale": 0,
},
"type": "smallint",
},
],
"foreignKeys": Array [
Expand Down
Loading

0 comments on commit 3b19912

Please sign in to comment.