mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 23:05:59 +00:00
db alter plan improvements
This commit is contained in:
@@ -11,6 +11,7 @@ async function deployDb({
|
|||||||
modelTransforms,
|
modelTransforms,
|
||||||
dbdiffOptionsExtra,
|
dbdiffOptionsExtra,
|
||||||
ignoreNameRegex = '',
|
ignoreNameRegex = '',
|
||||||
|
targetSchema = null,
|
||||||
}) {
|
}) {
|
||||||
const { sql } = await generateDeploySql({
|
const { sql } = await generateDeploySql({
|
||||||
connection,
|
connection,
|
||||||
@@ -22,6 +23,7 @@ async function deployDb({
|
|||||||
modelTransforms,
|
modelTransforms,
|
||||||
dbdiffOptionsExtra,
|
dbdiffOptionsExtra,
|
||||||
ignoreNameRegex,
|
ignoreNameRegex,
|
||||||
|
targetSchema,
|
||||||
});
|
});
|
||||||
// console.log('RUNNING DEPLOY SCRIPT:', sql);
|
// console.log('RUNNING DEPLOY SCRIPT:', sql);
|
||||||
await executeQuery({ connection, systemConnection, driver, sql, logScriptItems: true });
|
await executeQuery({ connection, systemConnection, driver, sql, logScriptItems: true });
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ const {
|
|||||||
modelCompareDbDiffOptions,
|
modelCompareDbDiffOptions,
|
||||||
enrichWithPreloadedRows,
|
enrichWithPreloadedRows,
|
||||||
skipNamesInStructureByRegex,
|
skipNamesInStructureByRegex,
|
||||||
|
replaceSchemaInStructure,
|
||||||
|
filterStructureBySchema,
|
||||||
} = require('dbgate-tools');
|
} = require('dbgate-tools');
|
||||||
const importDbModel = require('../utility/importDbModel');
|
const importDbModel = require('../utility/importDbModel');
|
||||||
const requireEngineDriver = require('../utility/requireEngineDriver');
|
const requireEngineDriver = require('../utility/requireEngineDriver');
|
||||||
@@ -22,6 +24,7 @@ async function generateDeploySql({
|
|||||||
modelTransforms = undefined,
|
modelTransforms = undefined,
|
||||||
dbdiffOptionsExtra = {},
|
dbdiffOptionsExtra = {},
|
||||||
ignoreNameRegex = '',
|
ignoreNameRegex = '',
|
||||||
|
targetSchema = null,
|
||||||
}) {
|
}) {
|
||||||
if (!driver) driver = requireEngineDriver(connection);
|
if (!driver) driver = requireEngineDriver(connection);
|
||||||
|
|
||||||
@@ -44,6 +47,11 @@ async function generateDeploySql({
|
|||||||
deployedModelSource = transform(deployedModelSource);
|
deployedModelSource = transform(deployedModelSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (targetSchema) {
|
||||||
|
deployedModelSource = replaceSchemaInStructure(deployedModelSource, targetSchema);
|
||||||
|
analysedStructure = filterStructureBySchema(analysedStructure, targetSchema);
|
||||||
|
}
|
||||||
|
|
||||||
const deployedModel = generateDbPairingId(extendDatabaseInfo(deployedModelSource));
|
const deployedModel = generateDbPairingId(extendDatabaseInfo(deployedModelSource));
|
||||||
const currentModel = generateDbPairingId(extendDatabaseInfo(analysedStructure));
|
const currentModel = generateDbPairingId(extendDatabaseInfo(analysedStructure));
|
||||||
const opts = {
|
const opts = {
|
||||||
|
|||||||
@@ -515,6 +515,9 @@ export class SqlDumper implements AlterProcessor {
|
|||||||
this.put('%i %k', col.columnName, col.isDescending == true ? 'DESC' : 'ASC');
|
this.put('%i %k', col.columnName, col.isDescending == true ? 'DESC' : 'ASC');
|
||||||
});
|
});
|
||||||
this.put('&<&n)');
|
this.put('&<&n)');
|
||||||
|
if (ix.filterDefinition && this.dialect.filteredIndexes) {
|
||||||
|
this.put('&n^where %s', ix.filterDefinition);
|
||||||
|
}
|
||||||
this.endCommand();
|
this.endCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ interface AlterOperation_ChangeConstraint {
|
|||||||
interface AlterOperation_DropConstraint {
|
interface AlterOperation_DropConstraint {
|
||||||
operationType: 'dropConstraint';
|
operationType: 'dropConstraint';
|
||||||
oldObject: ConstraintInfo;
|
oldObject: ConstraintInfo;
|
||||||
|
isRecreate?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AlterOperation_RenameConstraint {
|
interface AlterOperation_RenameConstraint {
|
||||||
@@ -337,15 +338,16 @@ export class AlterPlan {
|
|||||||
if (op.operationType == testedOperationType) {
|
if (op.operationType == testedOperationType) {
|
||||||
const constraints = this._getDependendColumnConstraints(testedObject as ColumnInfo, testedDependencies);
|
const constraints = this._getDependendColumnConstraints(testedObject as ColumnInfo, testedDependencies);
|
||||||
|
|
||||||
if (constraints.length > 0 && this.opts.noDropConstraint) {
|
// if (constraints.length > 0 && this.opts.noDropConstraint) {
|
||||||
return [];
|
// return [];
|
||||||
}
|
// }
|
||||||
|
|
||||||
const res: AlterOperation[] = [
|
const res: AlterOperation[] = [
|
||||||
...constraints.map(oldObject => {
|
...constraints.map(oldObject => {
|
||||||
const opRes: AlterOperation = {
|
const opRes: AlterOperation = {
|
||||||
operationType: 'dropConstraint',
|
operationType: 'dropConstraint',
|
||||||
oldObject,
|
oldObject,
|
||||||
|
isRecreate: true,
|
||||||
};
|
};
|
||||||
return opRes;
|
return opRes;
|
||||||
}),
|
}),
|
||||||
@@ -382,15 +384,16 @@ export class AlterPlan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (op.operationType == 'changeConstraint') {
|
if (op.operationType == 'changeConstraint') {
|
||||||
if (this.opts.noDropConstraint) {
|
// if (this.opts.noDropConstraint) {
|
||||||
// skip constraint recreate
|
// // skip constraint recreate
|
||||||
return [];
|
// return [];
|
||||||
}
|
// }
|
||||||
|
|
||||||
this.recreates.constraints += 1;
|
this.recreates.constraints += 1;
|
||||||
const opDrop: AlterOperation = {
|
const opDrop: AlterOperation = {
|
||||||
operationType: 'dropConstraint',
|
operationType: 'dropConstraint',
|
||||||
oldObject: op.oldObject,
|
oldObject: op.oldObject,
|
||||||
|
isRecreate: true,
|
||||||
};
|
};
|
||||||
const opCreate: AlterOperation = {
|
const opCreate: AlterOperation = {
|
||||||
operationType: 'createConstraint',
|
operationType: 'createConstraint',
|
||||||
@@ -547,7 +550,7 @@ export class AlterPlan {
|
|||||||
if (this.opts.noDropColumn && op.operationType == 'dropColumn') return false;
|
if (this.opts.noDropColumn && op.operationType == 'dropColumn') return false;
|
||||||
if (this.opts.noDropTable && op.operationType == 'dropTable') return false;
|
if (this.opts.noDropTable && op.operationType == 'dropTable') return false;
|
||||||
if (this.opts.noDropTable && op.operationType == 'recreateTable') return false;
|
if (this.opts.noDropTable && op.operationType == 'recreateTable') return false;
|
||||||
if (this.opts.noDropConstraint && op.operationType == 'dropConstraint') return false;
|
if (this.opts.noDropConstraint && op.operationType == 'dropConstraint' && !op.isRecreate) return false;
|
||||||
// if (
|
// if (
|
||||||
// this.opts.noDropSqlObject &&
|
// this.opts.noDropSqlObject &&
|
||||||
// op.operationType == 'dropSqlObject' &&
|
// op.operationType == 'dropSqlObject' &&
|
||||||
|
|||||||
@@ -127,6 +127,13 @@ export function removeTablePairingId(table: TableInfo): TableInfo {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function simplifySqlExpression(sql: string) {
|
||||||
|
return (sql || '')
|
||||||
|
.replace(/[\s\(\)\[\]\"]/g, '')
|
||||||
|
.toLowerCase()
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
|
||||||
function generateObjectPairingId(obj) {
|
function generateObjectPairingId(obj) {
|
||||||
if (obj.objectTypeField)
|
if (obj.objectTypeField)
|
||||||
return {
|
return {
|
||||||
@@ -375,6 +382,7 @@ function testEqualForeignKeys(a: ForeignKeyInfo, b: ForeignKeyInfo, opts: DbDiff
|
|||||||
function testEqualIndex(a: IndexInfo, b: IndexInfo, opts: DbDiffOptions) {
|
function testEqualIndex(a: IndexInfo, b: IndexInfo, opts: DbDiffOptions) {
|
||||||
if (!testEqualColumnRefs(a.columns, b.columns, opts)) return false;
|
if (!testEqualColumnRefs(a.columns, b.columns, opts)) return false;
|
||||||
if (!!a.isUnique != !!b.isUnique) return false;
|
if (!!a.isUnique != !!b.isUnique) return false;
|
||||||
|
if (simplifySqlExpression(a.filterDefinition) != simplifySqlExpression(b.filterDefinition)) return false;
|
||||||
|
|
||||||
if (!opts.ignoreConstraintNames && !testEqualNames(a.constraintName, b.constraintName, opts)) return false;
|
if (!opts.ignoreConstraintNames && !testEqualNames(a.constraintName, b.constraintName, opts)) return false;
|
||||||
return true;
|
return true;
|
||||||
@@ -456,7 +464,7 @@ export function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts: DbDiffOptions
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((a.dataType || '').toLowerCase() != (b.dataType || '').toLowerCase()) {
|
if (simplifySqlExpression(a.dataType) != simplifySqlExpression(b.dataType)) {
|
||||||
console.debug(
|
console.debug(
|
||||||
`Column ${a.pureName}.${a.columnName}, ${b.pureName}.${b.columnName}: different data type: ${a.dataType}, ${b.dataType}`
|
`Column ${a.pureName}.${a.columnName}, ${b.pureName}.${b.columnName}: different data type: ${a.dataType}, ${b.dataType}`
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -159,6 +159,7 @@ export function tableInfoFromYaml(table: TableInfoYaml, allTables: TableInfoYaml
|
|||||||
pureName: table.name,
|
pureName: table.name,
|
||||||
isUnique: index.unique,
|
isUnique: index.unique,
|
||||||
constraintType: 'index',
|
constraintType: 'index',
|
||||||
|
filterDefinition: index.filter,
|
||||||
columns: [
|
columns: [
|
||||||
...index.columns.map(columnName => ({ columnName })),
|
...index.columns.map(columnName => ({ columnName })),
|
||||||
...(index.included || []).map(columnName => ({ columnName, isIncludedColumn: true })),
|
...(index.included || []).map(columnName => ({ columnName, isIncludedColumn: true })),
|
||||||
|
|||||||
1
packages/types/dialect.d.ts
vendored
1
packages/types/dialect.d.ts
vendored
@@ -36,6 +36,7 @@ export interface SqlDialect {
|
|||||||
dropCheck?: boolean;
|
dropCheck?: boolean;
|
||||||
renameSqlObject?: boolean;
|
renameSqlObject?: boolean;
|
||||||
multipleSchema?: boolean;
|
multipleSchema?: boolean;
|
||||||
|
filteredIndexes?: boolean;
|
||||||
|
|
||||||
specificNullabilityImplementation?: boolean;
|
specificNullabilityImplementation?: boolean;
|
||||||
omitForeignKeys?: boolean;
|
omitForeignKeys?: boolean;
|
||||||
|
|||||||
@@ -2,21 +2,26 @@
|
|||||||
import CheckboxField from '../forms/CheckboxField.svelte';
|
import CheckboxField from '../forms/CheckboxField.svelte';
|
||||||
import FormCheckboxField from '../forms/FormCheckboxField.svelte';
|
import FormCheckboxField from '../forms/FormCheckboxField.svelte';
|
||||||
import SelectField from '../forms/SelectField.svelte';
|
import SelectField from '../forms/SelectField.svelte';
|
||||||
|
import TextField from '../forms/TextField.svelte';
|
||||||
|
|
||||||
import ColumnsConstraintEditorModal from './ColumnsConstraintEditorModal.svelte';
|
import ColumnsConstraintEditorModal from './ColumnsConstraintEditorModal.svelte';
|
||||||
|
|
||||||
export let constraintInfo;
|
export let constraintInfo;
|
||||||
export let setTableInfo;
|
export let setTableInfo;
|
||||||
export let tableInfo;
|
export let tableInfo;
|
||||||
|
export let driver;
|
||||||
|
|
||||||
let isUnique = constraintInfo?.isUnique;
|
let isUnique = constraintInfo?.isUnique;
|
||||||
|
|
||||||
function getExtractConstraintProps() {
|
function getExtractConstraintProps() {
|
||||||
return {
|
return {
|
||||||
isUnique,
|
isUnique,
|
||||||
|
filterDefinition,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let filterDefinition = constraintInfo?.filterDefinition;
|
||||||
|
|
||||||
$: isReadOnly = !setTableInfo;
|
$: isReadOnly = !setTableInfo;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -60,6 +65,22 @@
|
|||||||
index
|
index
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="largeFormMarker">
|
||||||
|
{#if driver?.dialect?.filteredIndexes}
|
||||||
|
<div class="row">
|
||||||
|
<div class="label col-3">Filtered index condition</div>
|
||||||
|
<div class="col-9">
|
||||||
|
<TextField
|
||||||
|
value={filterDefinition}
|
||||||
|
on:input={e => (filterDefinition = e.target['value'])}
|
||||||
|
focused
|
||||||
|
disabled={isReadOnly}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
</svelte:fragment>
|
</svelte:fragment>
|
||||||
</ColumnsConstraintEditorModal>
|
</ColumnsConstraintEditorModal>
|
||||||
|
|
||||||
|
|||||||
@@ -141,6 +141,7 @@
|
|||||||
setTableInfo,
|
setTableInfo,
|
||||||
tableInfo,
|
tableInfo,
|
||||||
dbInfo,
|
dbInfo,
|
||||||
|
driver,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +287,7 @@
|
|||||||
title={`Indexes (${indexes?.length || 0})`}
|
title={`Indexes (${indexes?.length || 0})`}
|
||||||
emptyMessage={isWritable ? 'No index defined' : null}
|
emptyMessage={isWritable ? 'No index defined' : null}
|
||||||
clickable
|
clickable
|
||||||
on:clickrow={e => showModal(IndexEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })}
|
on:clickrow={e => showModal(IndexEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo, driver })}
|
||||||
columns={[
|
columns={[
|
||||||
{
|
{
|
||||||
fieldName: 'columns',
|
fieldName: 'columns',
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ class MsSqlAnalyser extends DatabaseAnalyser {
|
|||||||
indexes: indexesRows.rows
|
indexes: indexesRows.rows
|
||||||
.filter(idx => idx.object_id == row.objectId && !idx.is_unique_constraint)
|
.filter(idx => idx.object_id == row.objectId && !idx.is_unique_constraint)
|
||||||
.map(idx => ({
|
.map(idx => ({
|
||||||
..._.pick(idx, ['constraintName', 'indexType', 'isUnique']),
|
..._.pick(idx, ['constraintName', 'indexType', 'isUnique', 'filterDefinition']),
|
||||||
columns: indexcolsRows.rows
|
columns: indexcolsRows.rows
|
||||||
.filter(col => col.object_id == idx.object_id && col.index_id == idx.index_id)
|
.filter(col => col.object_id == idx.object_id && col.index_id == idx.index_id)
|
||||||
.map(col => ({
|
.map(col => ({
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
module.exports = `
|
module.exports = `
|
||||||
select i.object_id, i.name as constraintName, i.type_desc as indexType, i.is_unique as isUnique,i.index_id, i.is_unique_constraint from sys.indexes i
|
select i.object_id, i.name as constraintName, i.type_desc as indexType, i.is_unique as isUnique,i.index_id, i.is_unique_constraint, i.filter_definition AS filterDefinition
|
||||||
|
from sys.indexes i
|
||||||
inner join sys.objects o on i.object_id = o.object_id
|
inner join sys.objects o on i.object_id = o.object_id
|
||||||
INNER JOIN sys.schemas u ON u.schema_id=o.schema_id
|
INNER JOIN sys.schemas u ON u.schema_id=o.schema_id
|
||||||
where i.is_primary_key=0
|
where i.is_primary_key=0
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ const dialect = {
|
|||||||
createCheck: true,
|
createCheck: true,
|
||||||
dropCheck: true,
|
dropCheck: true,
|
||||||
renameSqlObject: true,
|
renameSqlObject: true,
|
||||||
|
filteredIndexes: true,
|
||||||
|
|
||||||
dropReferencesWhenDropTable: true,
|
dropReferencesWhenDropTable: true,
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ const dialect = {
|
|||||||
dropCheck: true,
|
dropCheck: true,
|
||||||
allowMultipleValuesInsert: true,
|
allowMultipleValuesInsert: true,
|
||||||
renameSqlObject: true,
|
renameSqlObject: true,
|
||||||
|
filteredIndexes: true,
|
||||||
|
|
||||||
dropReferencesWhenDropTable: true,
|
dropReferencesWhenDropTable: true,
|
||||||
requireStandaloneSelectForScopeIdentity: true,
|
requireStandaloneSelectForScopeIdentity: true,
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ const dialect = {
|
|||||||
createPrimaryKey: false,
|
createPrimaryKey: false,
|
||||||
dropPrimaryKey: false,
|
dropPrimaryKey: false,
|
||||||
dropReferencesWhenDropTable: false,
|
dropReferencesWhenDropTable: false,
|
||||||
|
filteredIndexes: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @type {import('dbgate-types').EngineDriver} */
|
/** @type {import('dbgate-types').EngineDriver} */
|
||||||
|
|||||||
Reference in New Issue
Block a user