From fc0db925c5a185d60ddbbb250277976874398227 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Wed, 18 Sep 2024 16:01:02 +0200 Subject: [PATCH] schema analyser test --- .../__tests__/alter-database.spec.js | 1 + .../__tests__/schema-tests.spec.js | 53 +++++++++++++++++++ integration-tests/docker-compose.yaml | 28 +++++----- integration-tests/engines.js | 6 ++- 4 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 integration-tests/__tests__/schema-tests.spec.js diff --git a/integration-tests/__tests__/alter-database.spec.js b/integration-tests/__tests__/alter-database.spec.js index b06dcf58..90a80f15 100644 --- a/integration-tests/__tests__/alter-database.spec.js +++ b/integration-tests/__tests__/alter-database.spec.js @@ -67,3 +67,4 @@ describe('Alter database', () => { }) ); }); + diff --git a/integration-tests/__tests__/schema-tests.spec.js b/integration-tests/__tests__/schema-tests.spec.js new file mode 100644 index 00000000..96d070f4 --- /dev/null +++ b/integration-tests/__tests__/schema-tests.spec.js @@ -0,0 +1,53 @@ +const stableStringify = require('json-stable-stringify'); +const _ = require('lodash'); +const fp = require('lodash/fp'); +const { testWrapper } = require('../tools'); +const engines = require('../engines'); +const { runCommandOnDriver } = require('dbgate-tools'); + +describe('Schema tests', () => { + test.each(engines.filter(x => x.supportSchemas).map(engine => [engine.label, engine]))( + 'Create schema - %s', + testWrapper(async (conn, driver, engine) => { + const structure1 = await driver.analyseFull(conn); + expect(structure1.schemas.find(x => x.schemaName == 'myschema')).toBeFalsy(); + await runCommandOnDriver(conn, driver, dmp => dmp.createSchema('myschema')); + const structure2 = await driver.analyseIncremental(conn, structure1); + expect(structure2.schemas.find(x => x.schemaName == 'myschema')).toBeTruthy(); + }) + ); + + test.each(engines.filter(x => x.supportSchemas).map(engine => [engine.label, engine]))( + 'Drop schema - %s', + testWrapper(async (conn, driver, engine) => { + await runCommandOnDriver(conn, driver, dmp => dmp.createSchema('myschema')); + + const structure1 = await driver.analyseFull(conn); + expect(structure1.schemas.find(x => x.schemaName == 'myschema')).toBeTruthy(); + await runCommandOnDriver(conn, driver, dmp => dmp.dropSchema('myschema')); + const structure2 = await driver.analyseIncremental(conn, structure1); + expect(structure2.schemas.find(x => x.schemaName == 'myschema')).toBeFalsy(); + }) + ); +}); + +describe('Base analyser test', () => { + test.each(engines.map(engine => [engine.label, engine]))( + 'Structure without change - %s', + testWrapper(async (conn, driver, engine) => { + await driver.query(conn, `create table t1 (id int not null primary key)`); + + await driver.query( + conn, + `create table t2 ( + id int not null primary key, + t1_id int null references t1(id) + )` + ); + + const structure1 = await driver.analyseFull(conn); + const structure2 = await driver.analyseIncremental(conn, structure1); + expect(structure2).toBeNull(); + }) + ); +}); diff --git a/integration-tests/docker-compose.yaml b/integration-tests/docker-compose.yaml index 7f0bc431..4e347bf9 100644 --- a/integration-tests/docker-compose.yaml +++ b/integration-tests/docker-compose.yaml @@ -1,12 +1,12 @@ version: '3' services: - # postgres: - # image: postgres - # restart: always - # environment: - # POSTGRES_PASSWORD: Pwd2020Db - # ports: - # - 15000:5432 + postgres: + image: postgres + restart: always + environment: + POSTGRES_PASSWORD: Pwd2020Db + ports: + - 15000:5432 # mariadb: # image: mariadb @@ -26,13 +26,13 @@ services: # environment: # - MYSQL_ROOT_PASSWORD=Pwd2020Db - clickhouse: - image: bitnami/clickhouse:24.8.4 - restart: always - ports: - - 15005:8123 - environment: - - CLICKHOUSE_ADMIN_PASSWORD=Pwd2020Db + # clickhouse: + # image: bitnami/clickhouse:24.8.4 + # restart: always + # ports: + # - 15005:8123 + # environment: + # - CLICKHOUSE_ADMIN_PASSWORD=Pwd2020Db # mssql: # image: mcr.microsoft.com/mssql/server diff --git a/integration-tests/engines.js b/integration-tests/engines.js index 564a3577..380e8c3f 100644 --- a/integration-tests/engines.js +++ b/integration-tests/engines.js @@ -81,6 +81,7 @@ const engines = [ drop2: 'DROP FUNCTION obj2', }, ], + supportSchemas: true, }, { label: 'SQL Server', @@ -105,6 +106,7 @@ const engines = [ drop2: 'DROP PROCEDURE obj2', }, ], + supportSchemas: true, }, { label: 'SQLite', @@ -159,11 +161,11 @@ const filterLocal = [ // filter local testing '-MySQL', '-MariaDB', - '-PostgreSQL', + 'PostgreSQL', '-SQL Server', '-SQLite', '-CockroachDB', - 'ClickHouse', + '-ClickHouse', ]; const enginesPostgre = engines.filter(x => x.label == 'PostgreSQL');