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