@directus/schema
Advanced tools
Comparing version 9.0.0-rc.94 to 9.0.0-rc.95
@@ -80,25 +80,33 @@ "use strict"; | ||
Postgres.prototype.overview = function () { | ||
var _a; | ||
var _a, _b, _c, _d; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _b, columnsResult, primaryKeysResult, geometryColumnsResult, columns, primaryKeys, geometryColumns, overview, _i, columns_1, column, _c, primaryKeys_1, _d, table_name, column_name, _e, geometryColumns_1, _f, table_name, column_name, data_type; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
case 0: return [4 /*yield*/, Promise.all([ | ||
// Only select columns from BASE TABLEs to exclude views (Postgres views | ||
// cannot have primary keys so they cannot be used) | ||
this.knex.raw("\n SELECT c.table_name\n , c.column_name\n , c.column_default as default_value\n , c.data_type\n\t\t\t \t\t, c.character_maximum_length as max_length\n , CASE WHEN c.is_identity = 'YES' THEN true ELSE false END is_identity\n , CASE WHEN c.is_nullable = 'YES' THEN true ELSE false END is_nullable\n FROM\n information_schema.columns c\n LEFT JOIN information_schema.tables t\n ON c.table_name = t.table_name\n WHERE\n t.table_type = 'BASE TABLE'\n AND c.table_schema IN (?);\n ", [this.explodedSchema.join(',')]), | ||
this.knex.raw("\n SELECT relname as table_name\n , pg_attribute.attname as column_name\n FROM pg_index\n , pg_class\n , pg_attribute\n , pg_namespace\n WHERE\n indrelid = pg_class.oid\n AND nspname IN (?)\n AND pg_class.relnamespace = pg_namespace.oid\n AND pg_attribute.attrelid = pg_class.oid\n AND pg_attribute.attnum = ANY (pg_index.indkey)\n AND indisprimary\n ", [this.explodedSchema.join(',')]), | ||
this.knex | ||
.raw("\n\t\tWITH geometries as (\n\t\t\tselect * from geometry_columns\n\t\t\tunion\n\t\t\tselect * from geography_columns\n\t\t)\n SELECT f_table_name as table_name\n\t\t\t, f_geometry_column as column_name\n\t\t\t, type as data_type\n FROM geometries g\n JOIN information_schema.tables t\n\t ON g.f_table_name = t.table_name\n\t AND t.table_type = 'BASE TABLE'\n WHERE f_table_schema in (?)\n ", [this.explodedSchema.join(',')]) | ||
.catch(function () { return undefined; }), | ||
])]; | ||
var _e, columnsResult, primaryKeysResult, columns, primaryKeys, geometryColumns, hasPostGIS, result, overview, _i, columns_1, column, _f, primaryKeys_1, _g, table_name, column_name, _h, geometryColumns_1, _j, table_name, column_name, data_type; | ||
return __generator(this, function (_k) { | ||
switch (_k.label) { | ||
case 0: | ||
return [4 /*yield*/, Promise.all([ | ||
// Only select columns from BASE TABLEs to exclude views (Postgres views | ||
// cannot have primary keys so they cannot be used) | ||
this.knex.raw("\n SELECT c.table_name\n , c.column_name\n , c.column_default as default_value\n , c.data_type\n\t\t\t \t\t, c.character_maximum_length as max_length\n , CASE WHEN c.is_identity = 'YES' THEN true ELSE false END is_identity\n , CASE WHEN c.is_nullable = 'YES' THEN true ELSE false END is_nullable\n FROM\n information_schema.columns c\n LEFT JOIN information_schema.tables t\n ON c.table_name = t.table_name\n WHERE\n t.table_type = 'BASE TABLE'\n AND c.table_schema IN (?);\n ", [this.explodedSchema.join(',')]), | ||
this.knex.raw("\n SELECT relname as table_name\n , pg_attribute.attname as column_name\n FROM pg_index\n , pg_class\n , pg_attribute\n , pg_namespace\n WHERE\n indrelid = pg_class.oid\n AND nspname IN (?)\n AND pg_class.relnamespace = pg_namespace.oid\n AND pg_attribute.attrelid = pg_class.oid\n AND pg_attribute.attnum = ANY (pg_index.indkey)\n AND indisprimary\n ", [this.explodedSchema.join(',')]), | ||
])]; | ||
case 1: | ||
_b = _g.sent(), columnsResult = _b[0], primaryKeysResult = _b[1], geometryColumnsResult = _b[2]; | ||
_e = _k.sent(), columnsResult = _e[0], primaryKeysResult = _e[1]; | ||
columns = columnsResult.rows; | ||
primaryKeys = primaryKeysResult.rows; | ||
geometryColumns = (geometryColumnsResult === null || geometryColumnsResult === void 0 ? void 0 : geometryColumnsResult.rows) || []; | ||
geometryColumns = []; | ||
return [4 /*yield*/, this.knex.raw("SELECT EXISTS (\n\t\t\t\tSELECT\n\t\t\t\tFROM\n\t\t\t\t\tpg_proc p\n\t\t\t\t\tJOIN pg_namespace n ON p.pronamespace = n.oid\n\t\t\t\tWHERE\n\t\t\t\t\tn.nspname IN(?)\n\t\t\t\t\tAND p.oid::regprocedure::varchar = 'postgis_version()'\n\t\t\t);", [this.explodedSchema.join(',')])]; | ||
case 2: | ||
hasPostGIS = (_c = (_b = (_a = (_k.sent())) === null || _a === void 0 ? void 0 : _a.rows) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.exists; | ||
if (!hasPostGIS) return [3 /*break*/, 4]; | ||
return [4 /*yield*/, this.knex.raw("WITH geometries as (\n\t\t\t\t\tselect * from geometry_columns\n\t\t\t\t\tunion\n\t\t\t\t\tselect * from geography_columns\n\t\t\t\t)\n\t\t\t\tSELECT f_table_name as table_name\n\t\t\t\t\t, f_geometry_column as column_name\n\t\t\t\t\t, type as data_type\n\t\t\t\tFROM geometries g\n\t\t\t\tJOIN information_schema.tables t\n\t\t\t\t\tON g.f_table_name = t.table_name\n\t\t\t\t\tAND t.table_type = 'BASE TABLE'\n\t\t\t\tWHERE f_table_schema in (?)\n\t\t\t\t", [this.explodedSchema.join(',')])]; | ||
case 3: | ||
result = _k.sent(); | ||
geometryColumns = result.rows; | ||
_k.label = 4; | ||
case 4: | ||
overview = {}; | ||
for (_i = 0, columns_1 = columns; _i < columns_1.length; _i++) { | ||
column = columns_1[_i]; | ||
if (column.is_identity || ((_a = column.default_value) === null || _a === void 0 ? void 0 : _a.startsWith('nextval('))) { | ||
if (column.is_identity || ((_d = column.default_value) === null || _d === void 0 ? void 0 : _d.startsWith('nextval('))) { | ||
column.default_value = 'AUTO_INCREMENT'; | ||
@@ -114,8 +122,8 @@ } | ||
} | ||
for (_c = 0, primaryKeys_1 = primaryKeys; _c < primaryKeys_1.length; _c++) { | ||
_d = primaryKeys_1[_c], table_name = _d.table_name, column_name = _d.column_name; | ||
for (_f = 0, primaryKeys_1 = primaryKeys; _f < primaryKeys_1.length; _f++) { | ||
_g = primaryKeys_1[_f], table_name = _g.table_name, column_name = _g.column_name; | ||
overview[table_name].primary = column_name; | ||
} | ||
for (_e = 0, geometryColumns_1 = geometryColumns; _e < geometryColumns_1.length; _e++) { | ||
_f = geometryColumns_1[_e], table_name = _f.table_name, column_name = _f.column_name, data_type = _f.data_type; | ||
for (_h = 0, geometryColumns_1 = geometryColumns; _h < geometryColumns_1.length; _h++) { | ||
_j = geometryColumns_1[_h], table_name = _j.table_name, column_name = _j.column_name, data_type = _j.data_type; | ||
overview[table_name].columns[column_name].data_type = data_type; | ||
@@ -122,0 +130,0 @@ } |
@@ -11,4 +11,4 @@ export declare type SchemaOverview = { | ||
data_type: string; | ||
numeric_precision: number | null; | ||
numeric_scale: number | null; | ||
numeric_precision?: number | null; | ||
numeric_scale?: number | null; | ||
max_length: number | null; | ||
@@ -15,0 +15,0 @@ }; |
{ | ||
"name": "@directus/schema", | ||
"version": "9.0.0-rc.94", | ||
"version": "9.0.0-rc.95", | ||
"description": "Utility for extracting information about existing DB schema", | ||
@@ -45,6 +45,6 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"knex-schema-inspector": "1.6.1", | ||
"knex-schema-inspector": "1.6.2", | ||
"lodash": "^4.17.21" | ||
}, | ||
"gitHead": "7e41860cbf543c7fb6e104c76e3b01817e1aecf4" | ||
"gitHead": "afe7b15990e62fb8109469349071b79f2c69cde6" | ||
} |
@@ -8,3 +8,19 @@ import KnexPostgres, { parseDefaultValue } from 'knex-schema-inspector/dist/dialects/postgres'; | ||
async overview(): Promise<SchemaOverview> { | ||
const [columnsResult, primaryKeysResult, geometryColumnsResult] = await Promise.all([ | ||
type RawColumn = { | ||
table_name: string; | ||
column_name: string; | ||
default_value: string; | ||
data_type: string; | ||
max_length: number | null; | ||
is_identity: boolean; | ||
is_nullable: boolean; | ||
}; | ||
type RawGeometryColumn = { | ||
table_name: string; | ||
column_name: string; | ||
data_type: string; | ||
}; | ||
const [columnsResult, primaryKeysResult] = await Promise.all([ | ||
// Only select columns from BASE TABLEs to exclude views (Postgres views | ||
@@ -50,28 +66,48 @@ // cannot have primary keys so they cannot be used) | ||
), | ||
this.knex | ||
.raw( | ||
` | ||
WITH geometries as ( | ||
select * from geometry_columns | ||
union | ||
select * from geography_columns | ||
) | ||
SELECT f_table_name as table_name | ||
, f_geometry_column as column_name | ||
, type as data_type | ||
FROM geometries g | ||
JOIN information_schema.tables t | ||
ON g.f_table_name = t.table_name | ||
AND t.table_type = 'BASE TABLE' | ||
WHERE f_table_schema in (?) | ||
`, | ||
[this.explodedSchema.join(',')] | ||
) | ||
.catch(() => undefined), | ||
]); | ||
const columns = columnsResult.rows; | ||
const columns: RawColumn[] = columnsResult.rows; | ||
const primaryKeys = primaryKeysResult.rows; | ||
const geometryColumns = geometryColumnsResult?.rows || []; | ||
let geometryColumns: RawGeometryColumn[] = []; | ||
// Before we fetch the available geometry types, we'll have to ensure PostGIS exists | ||
// in the first place. If we don't, the transaction would error out due to the exception in | ||
// SQL, which we can't catch in JS. | ||
const hasPostGIS = ( | ||
await this.knex.raw( | ||
`SELECT EXISTS ( | ||
SELECT | ||
FROM | ||
pg_proc p | ||
JOIN pg_namespace n ON p.pronamespace = n.oid | ||
WHERE | ||
n.nspname IN(?) | ||
AND p.oid::regprocedure::varchar = 'postgis_version()' | ||
);`, | ||
[this.explodedSchema.join(',')] | ||
) | ||
)?.rows?.[0]?.exists; | ||
if (hasPostGIS) { | ||
const result = await this.knex.raw<{ rows: RawGeometryColumn[] }>( | ||
`WITH geometries as ( | ||
select * from geometry_columns | ||
union | ||
select * from geography_columns | ||
) | ||
SELECT f_table_name as table_name | ||
, f_geometry_column as column_name | ||
, type as data_type | ||
FROM geometries g | ||
JOIN information_schema.tables t | ||
ON g.f_table_name = t.table_name | ||
AND t.table_type = 'BASE TABLE' | ||
WHERE f_table_schema in (?) | ||
`, | ||
[this.explodedSchema.join(',')] | ||
); | ||
geometryColumns = result.rows; | ||
} | ||
const overview: SchemaOverview = {}; | ||
@@ -78,0 +114,0 @@ |
@@ -11,4 +11,4 @@ export type SchemaOverview = { | ||
data_type: string; | ||
numeric_precision: number | null; | ||
numeric_scale: number | null; | ||
numeric_precision?: number | null; | ||
numeric_scale?: number | null; | ||
max_length: number | null; | ||
@@ -15,0 +15,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107054
1352
+ Addedknex-schema-inspector@1.6.2(transitive)
- Removedknex-schema-inspector@1.6.1(transitive)
Updatedknex-schema-inspector@1.6.2