default schema refactor

This commit is contained in:
Jan Prochazka
2024-09-19 13:41:49 +02:00
parent 9eb27f5e92
commit 8c3c32aeba
9 changed files with 38 additions and 11 deletions

View File

@@ -32,6 +32,7 @@ describe('Schema tests', () => {
const schemas2 = await driver.listSchemas(conn); const schemas2 = await driver.listSchemas(conn);
expect(schemas2.find(x => x.schemaName == 'myschema')).toBeTruthy(); expect(schemas2.find(x => x.schemaName == 'myschema')).toBeTruthy();
expect(schemas2.length).toEqual(count + 1); expect(schemas2.length).toEqual(count + 1);
expect(schemas2.find(x => x.isDefault).schemaName).toEqual(engine.defaultSchemaName);
expect(structure2).toBeNull(); expect(structure2).toBeNull();
}) })
); );

View File

@@ -82,6 +82,7 @@ const engines = [
}, },
], ],
supportSchemas: true, supportSchemas: true,
defaultSchemaName: 'public',
}, },
{ {
label: 'SQL Server', label: 'SQL Server',
@@ -107,6 +108,7 @@ const engines = [
}, },
], ],
supportSchemas: true, supportSchemas: true,
defaultSchemaName: 'dbo',
}, },
{ {
label: 'SQLite', label: 'SQLite',

View File

@@ -23,3 +23,4 @@ export * from './getLogger';
export * from './getConnectionLabel'; export * from './getConnectionLabel';
export * from './detectSqlFilterBehaviour'; export * from './detectSqlFilterBehaviour';
export * from './filterBehaviours'; export * from './filterBehaviours';
export * from './schemaInfoTools';

View File

@@ -0,0 +1,15 @@
import { SchemaInfo, SqlDialect } from 'dbgate-types';
export function findDefaultSchema(schemaList: SchemaInfo[], dialect: SqlDialect) {
if (!schemaList) {
return null;
}
const dynamicDefaultSchema = schemaList.find(x => x.isDefault);
if (dynamicDefaultSchema) {
return dynamicDefaultSchema.schemaName;
}
if (dialect.defaultSchemaName && schemaList.find(x => x.schemaName == dialect.defaultSchemaName)) {
return dialect.defaultSchemaName;
}
return schemaList[0]?.schemaName;
}

View File

@@ -126,6 +126,7 @@ export interface TriggerInfo extends SqlObjectInfo {}
export interface SchemaInfo { export interface SchemaInfo {
objectId?: string; objectId?: string;
schemaName: string; schemaName: string;
isDefault?: boolean;
} }
export interface DatabaseInfoObjects { export interface DatabaseInfoObjects {
@@ -140,5 +141,4 @@ export interface DatabaseInfoObjects {
export interface DatabaseInfo extends DatabaseInfoObjects { export interface DatabaseInfo extends DatabaseInfoObjects {
engine?: string; engine?: string;
defaultSchema?: string;
} }

View File

@@ -1,8 +1,10 @@
import _ from 'lodash'; import _ from 'lodash';
import { addCompleter, setCompleters } from 'ace-builds/src-noconflict/ext-language_tools'; import { addCompleter, setCompleters } from 'ace-builds/src-noconflict/ext-language_tools';
import { getDatabaseInfo, getSchemaList } from '../utility/metadataLoaders'; import { getConnectionInfo, getDatabaseInfo, getSchemaList } from '../utility/metadataLoaders';
import analyseQuerySources from './analyseQuerySources'; import analyseQuerySources from './analyseQuerySources';
import { getStringSettingsValue } from '../settings/settingsTools'; import { getStringSettingsValue } from '../settings/settingsTools';
import { findEngineDriver, findDefaultSchema } from 'dbgate-tools';
import { getExtensions } from '../stores';
const COMMON_KEYWORDS = [ const COMMON_KEYWORDS = [
'select', 'select',
@@ -79,6 +81,9 @@ export function mountCodeCompletion({ conid, database, editor, getText }) {
const line = session.getLine(cursor.row).slice(0, cursor.column); const line = session.getLine(cursor.row).slice(0, cursor.column);
const dbinfo = await getDatabaseInfo({ conid, database }); const dbinfo = await getDatabaseInfo({ conid, database });
const schemaList = await getSchemaList({ conid, database }); const schemaList = await getSchemaList({ conid, database });
const connection = await getConnectionInfo(conid);
const driver = findEngineDriver(connection, getExtensions());
const defaultSchema = findDefaultSchema(schemaList, driver.dialect);
const convertUpper = getStringSettingsValue('sqlEditor.sqlCommandsCase', 'upperCase') == 'upperCase'; const convertUpper = getStringSettingsValue('sqlEditor.sqlCommandsCase', 'upperCase') == 'upperCase';
@@ -168,11 +173,7 @@ export function mountCodeCompletion({ conid, database, editor, getText }) {
} else { } else {
list = [ list = [
...(onlyTables ? [] : list), ...(onlyTables ? [] : list),
...createTableLikeList( ...createTableLikeList(schemaList, dbinfo, x => !defaultSchema || defaultSchema == x.schemaName),
schemaList,
dbinfo,
x => !dbinfo.defaultSchema || dbinfo.defaultSchema == x.schemaName
),
...(onlyTables ...(onlyTables
? [] ? []

View File

@@ -92,8 +92,6 @@ class MsSqlAnalyser extends DatabaseAnalyser {
const indexesRows = await this.analyserQuery('indexes', ['tables']); const indexesRows = await this.analyserQuery('indexes', ['tables']);
this.feedback({ analysingMessage: 'Loading index columns' }); this.feedback({ analysingMessage: 'Loading index columns' });
const indexcolsRows = await this.analyserQuery('indexcols', ['tables']); const indexcolsRows = await this.analyserQuery('indexcols', ['tables']);
this.feedback({ analysingMessage: 'Loading default schema' });
const defaultSchemaRows = await this.driver.query(this.pool, 'SELECT SCHEMA_NAME() as name');
this.feedback({ analysingMessage: 'Loading table sizes' }); this.feedback({ analysingMessage: 'Loading table sizes' });
const tableSizes = await this.analyserQuery('tableSizes'); const tableSizes = await this.analyserQuery('tableSizes');
@@ -173,7 +171,6 @@ class MsSqlAnalyser extends DatabaseAnalyser {
views, views,
procedures, procedures,
functions, functions,
defaultSchema: defaultSchemaRows.rows[0] ? defaultSchemaRows.rows[0].name : undefined,
}; };
} }

View File

@@ -152,7 +152,14 @@ const driver = {
}, },
async listSchemas(pool) { async listSchemas(pool) {
const { rows } = await this.query(pool, 'select schema_id as objectId, name as schemaName from sys.schemas'); const { rows } = await this.query(pool, 'select schema_id as objectId, name as schemaName from sys.schemas');
return rows;
const defaultSchemaRows = await this.query(pool, 'SELECT SCHEMA_NAME() as name');
const defaultSchema = defaultSchemaRows.rows[0]?.name;
return rows.map(x => ({
...x,
isDefault: x.schemaName == defaultSchema,
}));
}, },
}; };

View File

@@ -273,10 +273,13 @@ const drivers = driverBases.map(driverBase => ({
pool, pool,
'select oid as "object_id", nspname as "schema_name" from pg_catalog.pg_namespace' 'select oid as "object_id", nspname as "schema_name" from pg_catalog.pg_namespace'
); );
const defaultSchemaRows = await this.query(pool, 'SHOW SEARCH_PATH;');
const searchPath = defaultSchemaRows.rows[0]?.search_path?.replace('"$user",', '')?.trim();
const schemas = schemaRows.rows.map(x => ({ const schemas = schemaRows.rows.map(x => ({
schemaName: x.schema_name, schemaName: x.schema_name,
objectId: x.object_id, objectId: x.object_id,
isDefault: x.schema_name == searchPath,
})); }));
return schemas; return schemas;