Skip to content

Commit

Permalink
Drop most database deps and code in JavaScript since they're now impl…
Browse files Browse the repository at this point in the history
…emented in Go (#125)

* Drop most deps

* Try upgrading go-ora

* override default clickhouse database

* Fix go mod

* Bring jest required coverage back down
  • Loading branch information
eatonphil authored Dec 21, 2021
1 parent e200d3a commit 9fd01ba
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 1,465 deletions.
92 changes: 42 additions & 50 deletions desktop/panel/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const DATABASES = [
type: 'postgres',
query: `SELECT 1 AS "1", 2.2 AS "2", true AS "true", 'string' AS "string", CAST('2021-01-01' AS DATE) AS "date"`,
},
{
type: 'clickhouse',
query: `SELECT 1 AS "1", 2.2 AS "2", true AS "true", 'string' AS "string", parseDateTimeBestEffortOrNull('2021-01-01') AS "date"`,
},
{
type: 'sqlserver',
// SQL Server doesn't have true/false literals
Expand Down Expand Up @@ -57,6 +61,9 @@ const vendorOverride = {
postgres: {
address: 'localhost?sslmode=disable',
},
clickhouse: {
database: 'default',
},
oracle: {
database: 'XEPDB1',
},
Expand All @@ -69,12 +76,12 @@ const vendorOverride = {
};

for (const subprocess of RUNNERS) {
for (const t of DATABASES) {
// Only test Oracle with the Go runner for now
if (t.type === 'oracle' && !subprocess?.go) {
continue;
}
// Most databases now only work with the Go runner.
if (!subprocess?.go) {
continue;
}

for (const t of DATABASES) {
describe(
t.type +
' running via ' +
Expand Down Expand Up @@ -108,54 +115,39 @@ for (const subprocess of RUNNERS) {

let finished = false;
const panels = [lp, dp];
try {
await withSavedPanels(
panels,
async (project) => {
const panelValueBuffer = fs.readFileSync(
getProjectResultsFile(project.projectName) + dp.id
);
await withSavedPanels(
panels,
async (project) => {
const panelValueBuffer = fs.readFileSync(
getProjectResultsFile(project.projectName) + dp.id
);

const v = JSON.parse(panelValueBuffer.toString());
if (t.query.startsWith('SELECT 1')) {
expect(v.length).toBe(1);
// These database drivers are all over the place between Node and Go.
// Close enough is fine I guess.
expect(v[0]['1']).toBe(1);
// TODO: fix the Oracle driver reading floats as zero
// https://github.com/sijms/go-ora/issues/135
if (t.type !== 'oracle') {
expect(String(v[0]['2'])).toBe('2.2');
}
expect(v[0]['true'] == '1').toBe(true);
expect(v[0].string).toBe('string');
expect(new Date(v[0].date)).toStrictEqual(
new Date('2021-01-01')
);
} else {
expect(v).toStrictEqual([
{ name: 'Kate', age: 9 },
{ name: 'Bake', age: 10 },
]);
}
const v = JSON.parse(panelValueBuffer.toString());
if (t.query.startsWith('SELECT 1')) {
expect(v.length).toBe(1);
// These database drivers are all over the place between Node and Go.
// Close enough is fine I guess.
expect(v[0]['1']).toBe(1);
expect(String(v[0]['2'])).toBe('2.2');
expect(v[0]['true'] == '1').toBe(true);
expect(v[0].string).toBe('string');
expect(new Date(v[0].date)).toStrictEqual(
new Date('2021-01-01')
);
} else {
expect(v).toStrictEqual([
{ name: 'Kate', age: 9 },
{ name: 'Bake', age: 10 },
]);
}

finished = true;
},
{ evalPanels: true, connectors, subprocessName: subprocess }
);
finished = true;
},
{ evalPanels: true, connectors, subprocessName: subprocess }
);

if (!finished) {
throw new Error('Callback did not finish');
}
} finally {
// Delete sqlite file
if (t.type === 'sqlite') {
try {
fs.unlinkSync('test');
} catch (e) {
console.error(e);
}
}
if (!finished) {
throw new Error('Callback did not finish');
}
// sqlserver at least can take longer than 5s to fail
}, 30_000);
Expand Down
94 changes: 6 additions & 88 deletions desktop/panel/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import log from '../../shared/log';
import { ANSI_SQL_QUOTE, MYSQL_QUOTE } from '../../shared/sql';
import {
DatabaseConnectorInfo,
DatabasePanelInfo,
Expand All @@ -9,18 +8,11 @@ import {
import { fullHttpURL } from '../../shared/url';
import { Dispatch } from '../rpc';
import { decryptFields } from '../secret';
import { evalClickHouse } from './databases/clickhouse';
import { evalElasticsearch } from './databases/elasticsearch';
import { evalInflux } from './databases/influx';
import { evalMySQL } from './databases/mysql';
import { evalOracle } from './databases/oracle';
import { evalPostgres } from './databases/postgres';
import { evalPrometheus } from './databases/prometheus';
import { evalSnowflake } from './databases/snowflake';
import { evalSplunk } from './databases/splunk';
import { evalSQLite } from './databases/sqlite';
import { evalSQLServer } from './databases/sqlserver';
import { transformDM_getPanelCalls } from './databases/sqlutil';
import { tunnel } from './tunnel';
import { EvalHandlerExtra, EvalHandlerResponse, guardPanel } from './types';

Expand All @@ -43,18 +35,18 @@ export function getAndDecryptConnector(

const DEFAULT_PORT = {
postgres: 5432,
mysql: 3306,
sqlite: 0,
sqlserver: 1433,
oracle: 1521,
clickhouse: 8123,
cassandra: 9160,
snowflake: 443,
presto: 8080,
elasticsearch: 9200,
influx: 8086,
splunk: 443,
prometheus: 9090,
mysql: 3306,
sqlserver: 1433,
oracle: 1521,
clickhouse: 8123,
};

export function portHostFromAddress(
Expand Down Expand Up @@ -153,83 +145,9 @@ export async function evalDatabase(
);
}

const { query, panelsToImport } = transformDM_getPanelCalls(
content,
extra.idShapeMap,
extra.idMap,
['mysql', 'postgres', 'sqlite'].includes(connector.database.type),
connector.database.type === 'mysql' ? MYSQL_QUOTE : ANSI_SQL_QUOTE
);

// SQLite is file, not network based so handle separately.
if (connector.database.type === 'sqlite') {
return await evalSQLite(
dispatch,
query,
info,
connector,
project,
panelsToImport
);
}

if (connector.database.type === 'snowflake') {
return await evalSnowflake(query, connector);
}

const { host, port } = portHostFromAddress(info, connector);

// The way hosts are formatted is unique so have sqlserver manage its own call to tunnel()
if (connector.database.type === 'sqlserver') {
return await tunnel(
project,
serverId,
host.split('\\')[0],
port,
(host: string, port: number): any =>
evalSQLServer(query, host, port, connector)
);
return await evalSnowflake(content, connector);
}

return await tunnel(
project,
serverId,
host,
port,
(host: string, port: number): any => {
if (connector.database.type === 'postgres') {
return evalPostgres(
dispatch,
query,
host,
port,
connector,
project.projectName,
panelsToImport
);
}

if (connector.database.type === 'mysql') {
return evalMySQL(
dispatch,
query,
host,
port,
connector,
project.projectName,
panelsToImport
);
}

if (connector.database.type === 'oracle') {
return evalOracle(query, host, port, connector);
}

if (connector.database.type === 'clickhouse') {
return evalClickHouse(query, host, port, connector);
}

throw new Error(`Unknown SQL type: ${connector.database.type}`);
}
);
throw new Error(`Unknown SQL type: ${connector.database.type}`);
}
55 changes: 0 additions & 55 deletions desktop/panel/databases/clickhouse.test.js

This file was deleted.

31 changes: 0 additions & 31 deletions desktop/panel/databases/clickhouse.ts

This file was deleted.

42 changes: 0 additions & 42 deletions desktop/panel/databases/mysql.ts

This file was deleted.

Loading

0 comments on commit 9fd01ba

Please sign in to comment.