diff --git a/packages/engines/mysql/MySqlAnalyser.js b/packages/engines/mysql/MySqlAnalyser.js index 12e3a97ac..c28dbdb08 100644 --- a/packages/engines/mysql/MySqlAnalyser.js +++ b/packages/engines/mysql/MySqlAnalyser.js @@ -83,12 +83,15 @@ class MySqlAnalyser extends DatabaseAnalayser { } async _runAnalysis() { - const tables = await this.driver.query(this.pool, this.createQuery('tables')); - const columns = await this.driver.query(this.pool, this.createQuery('columns')); - const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys')); - const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys')); - const views = await this.driver.query(this.pool, this.createQuery('views')); - const programmables = await this.driver.query(this.pool, this.createQuery('programmables')); + const tables = await this.driver.query(this.pool, this.createQuery('tables', ['tables'])); + const columns = await this.driver.query(this.pool, this.createQuery('columns', ['tables', 'views'])); + const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables'])); + const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables'])); + const views = await this.driver.query(this.pool, this.createQuery('views', ['views'])); + const programmables = await this.driver.query( + this.pool, + this.createQuery('programmables', ['procedures', 'functions']) + ); const viewTexts = await this.getViewTexts(views.rows.map((x) => x.pureName)); diff --git a/packages/engines/postgres/PostgreAnalyser.js b/packages/engines/postgres/PostgreAnalyser.js index 353b0dced..b55fb268a 100644 --- a/packages/engines/postgres/PostgreAnalyser.js +++ b/packages/engines/postgres/PostgreAnalyser.js @@ -29,7 +29,7 @@ function getColumnInfo({ return { columnName, dataType: fullDataType, - notNull: !isNullable, + notNull: !isNullable || isNullable == 'NO' || isNullable == 'no', autoIncrement: !!isIdentity, defaultValue, }; @@ -40,16 +40,17 @@ class PostgreAnalyser extends DatabaseAnalayser { super(pool, driver); } - createQuery(resFileName, tables = false, views = false, procedures = false, functions = false, triggers = false) { + createQuery(resFileName, typeFields) { let res = sql[resFileName]; res = res.replace('=[OBJECT_ID_CONDITION]', ' is not null'); return res; } async _runAnalysis() { - const tables = await this.driver.query(this.pool, this.createQuery('tableModifications')); - const columns = await this.driver.query(this.pool, this.createQuery('columns')); - const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys')); - const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys')); + const tables = await this.driver.query(this.pool, this.createQuery('tableModifications', ['tables'])); + const columns = await this.driver.query(this.pool, this.createQuery('columns', ['tables'])); + const pkColumns = await this.driver.query(this.pool, this.createQuery('primaryKeys', ['tables'])); + const fkColumns = await this.driver.query(this.pool, this.createQuery('foreignKeys', ['tables'])); + const views = await this.driver.query(this.pool, this.createQuery('views', ['views'])); // console.log('PG fkColumns', fkColumns.rows); return this.mergeAnalyseResult({ @@ -61,6 +62,12 @@ class PostgreAnalyser extends DatabaseAnalayser { primaryKey: DatabaseAnalayser.extractPrimaryKeys(table, pkColumns.rows), foreignKeys: DatabaseAnalayser.extractForeignKeys(table, fkColumns.rows), })), + views: views.rows.map((view) => ({ + ...view, + columns: columns.rows + .filter((col) => col.pureName == view.pureName && col.schemaName == view.schemaName) + .map(getColumnInfo), + })), }); } } diff --git a/packages/engines/postgres/sql/index.js b/packages/engines/postgres/sql/index.js index 5fc018094..a06334731 100644 --- a/packages/engines/postgres/sql/index.js +++ b/packages/engines/postgres/sql/index.js @@ -2,10 +2,12 @@ const columns = require('./columns'); const tableModifications = require('./tableModifications'); const primaryKeys = require('./primaryKeys'); const foreignKeys = require('./foreignKeys'); +const views = require('./views'); module.exports = { columns, tableModifications, primaryKeys, foreignKeys, + views, }; diff --git a/packages/engines/postgres/sql/tableModifications.js b/packages/engines/postgres/sql/tableModifications.js index ee6cab595..bb403579c 100644 --- a/packages/engines/postgres/sql/tableModifications.js +++ b/packages/engines/postgres/sql/tableModifications.js @@ -20,7 +20,7 @@ SELECT oid as "objectId", nspname as "schemaName", relname as "pureName", ' ' || column_name || ' ' || type || ' '|| not_null ) , E',\\n' - ) || E'\\n);\\n' || (select pkey from pkey where pkey.conrelid = oid)) as "hash" + ) || E'\\n);\\n' || (select pkey from pkey where pkey.conrelid = oid)) as "hashCode" from ( SELECT diff --git a/packages/engines/postgres/sql/views.js b/packages/engines/postgres/sql/views.js new file mode 100644 index 000000000..bc72e1aea --- /dev/null +++ b/packages/engines/postgres/sql/views.js @@ -0,0 +1,9 @@ +module.exports = ` +select + table_name as "pureName", + table_schema as "schemaName", + view_definition as "createSql", + md5(view_definition) as "hashCode" +from + information_schema.views where table_schema != 'information_schema' and table_schema != 'pg_catalog' +`; diff --git a/packages/types/dbinfo.d.ts b/packages/types/dbinfo.d.ts index 55082defe..cbf43fba2 100644 --- a/packages/types/dbinfo.d.ts +++ b/packages/types/dbinfo.d.ts @@ -45,6 +45,7 @@ export interface DatabaseObjectInfo extends NamedObjectInfo { objectId?: string; createDate?: string; modifyDate?: string; + hashCode?: string; } export interface SqlObjectInfo extends DatabaseObjectInfo {