diff --git a/integration-tests/__tests__/table-analyse.spec.js b/integration-tests/__tests__/table-analyse.spec.js index 16c7fb8d1..ea3ca82eb 100644 --- a/integration-tests/__tests__/table-analyse.spec.js +++ b/integration-tests/__tests__/table-analyse.spec.js @@ -6,9 +6,10 @@ const ix1Sql = 'CREATE index ix1 ON t1(val1, id)'; const t2Sql = engine => `CREATE TABLE t2 (id int not null primary key, val2 varchar(50) ${engine.skipUnique ? '' : 'unique'})`; const t3Sql = 'CREATE TABLE t3 (id int not null primary key, valfk int, foreign key (valfk) references t2(id))'; +const t4Sql = 'CREATE TABLE t4 (id int not null primary key, valdef int not null default 12)'; // const fkSql = 'ALTER TABLE t3 ADD FOREIGN KEY (valfk) REFERENCES t2(id)' -const txMatch = (engine, tname, vcolname, nextcol) => +const txMatch = (engine, tname, vcolname, nextcol, defaultValue) => expect.objectContaining({ pureName: tname, columns: [ @@ -19,10 +20,14 @@ const txMatch = (engine, tname, vcolname, nextcol) => }), expect.objectContaining({ columnName: vcolname, - ...(engine.skipNullability ? {} : { notNull: false }), - dataType: engine.skipStringLength - ? expect.stringMatching(/.*string|char.*/i) - : expect.stringMatching(/.*char.*\(50\)/i), + ...(engine.skipNullability ? {} : { notNull: !!defaultValue }), + ...(defaultValue + ? { defaultValue } + : { + dataType: engine.skipStringLength + ? expect.stringMatching(/.*string|char.*/i) + : expect.stringMatching(/.*char.*\(50\)/i), + }), }), ...(nextcol ? [ @@ -48,6 +53,7 @@ const txMatch = (engine, tname, vcolname, nextcol) => const t1Match = engine => txMatch(engine, 't1', 'val1'); const t2Match = engine => txMatch(engine, 't2', 'val2'); const t2NextColMatch = engine => txMatch(engine, 't2', 'val2', true); +const t4Match = engine => txMatch(engine, 't4', 'valdef', null, '12'); describe('Table analyse', () => { test.each(engines.map(engine => [engine.label, engine]))( @@ -169,4 +175,16 @@ describe('Table analyse', () => { ); }) ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Table structure - default value - %s', + testWrapper(async (conn, driver, engine) => { + await driver.query(conn, t4Sql); + + const structure = await driver.analyseFull(conn); + + expect(structure.tables.length).toEqual(1); + expect(structure.tables[0]).toEqual(t4Match(engine)); + }) + ); }); diff --git a/plugins/dbgate-plugin-clickhouse/src/backend/sql/columns.js b/plugins/dbgate-plugin-clickhouse/src/backend/sql/columns.js index 7d9f3770a..b15919321 100644 --- a/plugins/dbgate-plugin-clickhouse/src/backend/sql/columns.js +++ b/plugins/dbgate-plugin-clickhouse/src/backend/sql/columns.js @@ -4,7 +4,8 @@ select tables.uuid as "objectId", columns.name as "columnName", columns.type as "dataType", - columns.comment as "columnComment" + columns.comment as "columnComment", + columns.default_expression as "defaultValue" from system.columns inner join system.tables on columns.table = tables.name and columns.database = tables.database where columns.database='#DATABASE#' and tables.uuid =OBJECT_ID_CONDITION