mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 12:43:58 +00:00
change column nullablility - works (without SQLite - table recreate needed)
This commit is contained in:
@@ -5,15 +5,15 @@ import { AlterPlan } from './alterPlan';
|
|||||||
type DbDiffSchemaMode = 'strict' | 'ignore' | 'ignoreImplicit';
|
type DbDiffSchemaMode = 'strict' | 'ignore' | 'ignoreImplicit';
|
||||||
|
|
||||||
export interface DbDiffOptions {
|
export interface DbDiffOptions {
|
||||||
allowRecreateTable: boolean;
|
allowRecreateTable?: boolean;
|
||||||
allowRecreateConstraint: boolean;
|
allowRecreateConstraint?: boolean;
|
||||||
allowRecreateSpecificObject: boolean;
|
allowRecreateSpecificObject?: boolean;
|
||||||
allowPairRenamedTables: boolean;
|
allowPairRenamedTables?: boolean;
|
||||||
|
|
||||||
ignoreCase: boolean;
|
ignoreCase?: boolean;
|
||||||
schemaMode: DbDiffSchemaMode;
|
schemaMode?: DbDiffSchemaMode;
|
||||||
leftImplicitSchema: string;
|
leftImplicitSchema?: string;
|
||||||
rightImplicitSchema: string;
|
rightImplicitSchema?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateTablePairingId(table: TableInfo): TableInfo {
|
export function generateTablePairingId(table: TableInfo): TableInfo {
|
||||||
@@ -69,12 +69,12 @@ function testEqualFullNames(lft: NamedObjectInfo, rgt: NamedObjectInfo, opts: Db
|
|||||||
return testEqualSchemas(lft.schemaName, rgt.schemaName, opts) && testEqualNames(lft.pureName, rgt.pureName, opts);
|
return testEqualSchemas(lft.schemaName, rgt.schemaName, opts) && testEqualNames(lft.pureName, rgt.pureName, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testEqualsColumns(
|
export function testEqualColumns(
|
||||||
a: ColumnInfo,
|
a: ColumnInfo,
|
||||||
b: ColumnInfo,
|
b: ColumnInfo,
|
||||||
checkName: boolean,
|
checkName: boolean,
|
||||||
checkDefault: boolean,
|
checkDefault: boolean,
|
||||||
opts: DbDiffOptions
|
opts: DbDiffOptions = {}
|
||||||
) {
|
) {
|
||||||
if (checkName && !testEqualNames(a.columnName, b.columnName, opts)) {
|
if (checkName && !testEqualNames(a.columnName, b.columnName, opts)) {
|
||||||
// opts.DiffLogger.Trace("Column, different name: {0}; {1}", a, b);
|
// opts.DiffLogger.Trace("Column, different name: {0}; {1}", a, b);
|
||||||
@@ -178,7 +178,7 @@ function testEqualsColumns(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts: DbDiffOptions) {
|
export function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts: DbDiffOptions = {}) {
|
||||||
if (a.dataType != b.dataType) {
|
if (a.dataType != b.dataType) {
|
||||||
// opts.DiffLogger.Trace("Column {0}, {1}: different types: {2}; {3}", a, b, a.DataType, b.DataType);
|
// opts.DiffLogger.Trace("Column {0}, {1}: different types: {2}; {3}", a, b, a.DataType, b.DataType);
|
||||||
return false;
|
return false;
|
||||||
@@ -254,8 +254,8 @@ function planAlterTable(plan: AlterPlan, oldTable: TableInfo, newTable: TableInf
|
|||||||
columnPairs
|
columnPairs
|
||||||
.filter(x => x[0] && x[1])
|
.filter(x => x[0] && x[1])
|
||||||
.forEach(x => {
|
.forEach(x => {
|
||||||
if (!testEqualsColumns(x[0], x[1], true, true, opts)) {
|
if (!testEqualColumns(x[0], x[1], true, true, opts)) {
|
||||||
if (testEqualsColumns(x[0], x[1], false, true, opts)) {
|
if (testEqualColumns(x[0], x[1], false, true, opts)) {
|
||||||
// console.log('PLAN RENAME COLUMN')
|
// console.log('PLAN RENAME COLUMN')
|
||||||
plan.renameColumn(x[0], x[1].columnName);
|
plan.renameColumn(x[0], x[1].columnName);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
2
packages/types/alter-processor.d.ts
vendored
2
packages/types/alter-processor.d.ts
vendored
@@ -4,7 +4,7 @@ export interface AlterProcessor {
|
|||||||
createTable(table: TableInfo);
|
createTable(table: TableInfo);
|
||||||
dropTable(table: TableInfo);
|
dropTable(table: TableInfo);
|
||||||
createColumn(column: ColumnInfo, constraints: ConstraintInfo[]);
|
createColumn(column: ColumnInfo, constraints: ConstraintInfo[]);
|
||||||
changeColumn(oldColumn: ColumnInfo, newColumn: ColumnInfo);
|
changeColumn(oldColumn: ColumnInfo, newColumn: ColumnInfo, constraints: ConstraintInfo[]);
|
||||||
dropColumn(column: ColumnInfo);
|
dropColumn(column: ColumnInfo);
|
||||||
createConstraint(constraint: ConstraintInfo);
|
createConstraint(constraint: ConstraintInfo);
|
||||||
changeConstraint(oldConstraint: ConstraintInfo, newConstraint: ConstraintInfo);
|
changeConstraint(oldConstraint: ConstraintInfo, newConstraint: ConstraintInfo);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const { SqlDumper } = global.DBGATE_TOOLS;
|
const { SqlDumper, testEqualColumns } = global.DBGATE_TOOLS;
|
||||||
|
|
||||||
class MsSqlDumper extends SqlDumper {
|
class MsSqlDumper extends SqlDumper {
|
||||||
autoIncrement() {
|
autoIncrement() {
|
||||||
@@ -88,6 +88,21 @@ class MsSqlDumper extends SqlDumper {
|
|||||||
this.putCmd("^execute sp_rename '%f.%i', '%s', 'COLUMN'", column, column.columnName, newcol);
|
this.putCmd("^execute sp_rename '%f.%i', '%s', 'COLUMN'", column, column.columnName, newcol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
changeColumn(oldcol, newcol, constraints) {
|
||||||
|
if (testEqualColumns(oldcol, newcol, false, false)) {
|
||||||
|
this.dropDefault(oldcol);
|
||||||
|
if (oldcol.columnName != newcol.columnName) this.renameColumn(oldcol, newcol.columnName);
|
||||||
|
this.createDefault(oldcol);
|
||||||
|
} else {
|
||||||
|
this.dropDefault(oldcol);
|
||||||
|
if (oldcol.columnName != newcol.columnName) this.renameColumn(oldcol, newcol.columnName);
|
||||||
|
this.put('^alter ^table %f ^alter ^column %i ', oldcol, oldcol.columnName, newcol.columnName);
|
||||||
|
this.columnDefinition(newcol, { includeDefault: false });
|
||||||
|
this.endCommand();
|
||||||
|
this.createDefault(oldcol);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
renameConstraint(cnt, newname) {
|
renameConstraint(cnt, newname) {
|
||||||
if (cnt.constraintType == 'index')
|
if (cnt.constraintType == 'index')
|
||||||
this.putCmd("^execute sp_rename '%f.%i', '%s', 'INDEX'", cnt, cnt.constraintName, newname);
|
this.putCmd("^execute sp_rename '%f.%i', '%s', 'INDEX'", cnt, cnt.constraintName, newname);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class Dumper extends SqlDumper {
|
|||||||
|
|
||||||
changeColumn(oldcol, newcol, constraints) {
|
changeColumn(oldcol, newcol, constraints) {
|
||||||
this.put('^alter ^table %f ^change ^column %i %i ', oldcol, oldcol.columnName, newcol.columnName);
|
this.put('^alter ^table %f ^change ^column %i %i ', oldcol, oldcol.columnName, newcol.columnName);
|
||||||
this.columnDefinition(newcol, true, true, true);
|
this.columnDefinition(newcol);
|
||||||
this.inlineConstraints(constraints);
|
this.inlineConstraints(constraints);
|
||||||
this.endCommand();
|
this.endCommand();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const { SqlDumper } = global.DBGATE_TOOLS;
|
const { SqlDumper, testEqualTypes } = global.DBGATE_TOOLS;
|
||||||
|
|
||||||
class Dumper extends SqlDumper {
|
class Dumper extends SqlDumper {
|
||||||
/** @param type {import('dbgate-types').TransformType} */
|
/** @param type {import('dbgate-types').TransformType} */
|
||||||
@@ -61,6 +61,31 @@ class Dumper extends SqlDumper {
|
|||||||
}
|
}
|
||||||
super.columnDefinition(col, options);
|
super.columnDefinition(col, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
changeColumn(oldcol, newcol, constraints) {
|
||||||
|
if (oldcol.columnName != newcol.columnName) {
|
||||||
|
this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', oldcol, oldcol.columnName, newcol.columnName);
|
||||||
|
}
|
||||||
|
if (!testEqualTypes(oldcol, newcol)) {
|
||||||
|
this.putCmd('^alter ^table %f ^alter ^column %i ^type %s', oldcol, oldcol.columnName, newcol.dataType);
|
||||||
|
}
|
||||||
|
if (oldcol.notNull != newcol.notNull) {
|
||||||
|
if (newcol.notNull) this.putCmd('^alter ^table %f ^alter ^column %i ^set ^not ^null', newcol, newcol.columnName);
|
||||||
|
else this.putCmd('^alter ^table %f ^alter ^column %i ^drop ^not ^null', newcol, newcol.columnName);
|
||||||
|
}
|
||||||
|
if (oldcol.defaultValue != newcol.defaultValue) {
|
||||||
|
if (newcol.defaultValue == null) {
|
||||||
|
this.putCmd('^alter ^table %f ^alter ^column %i ^drop ^default', newcol, newcol.columnName);
|
||||||
|
} else {
|
||||||
|
this.putCmd(
|
||||||
|
'^alter ^table %f ^alter ^column %i ^set ^default %s',
|
||||||
|
newcol,
|
||||||
|
newcol.columnName,
|
||||||
|
newcol.defaultValue
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Dumper;
|
module.exports = Dumper;
|
||||||
|
|||||||
Reference in New Issue
Block a user