mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-27 16:06:23 +00:00
oracle alter table passes
This commit is contained in:
@@ -4,7 +4,12 @@ const fp = require('lodash/fp');
|
|||||||
const { testWrapper } = require('../tools');
|
const { testWrapper } = require('../tools');
|
||||||
const engines = require('../engines');
|
const engines = require('../engines');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const { getAlterTableScript, extendDatabaseInfo, generateDbPairingId } = require('dbgate-tools');
|
const {
|
||||||
|
getAlterTableScript,
|
||||||
|
extendDatabaseInfo,
|
||||||
|
generateDbPairingId,
|
||||||
|
formatQueryWithoutParams,
|
||||||
|
} = require('dbgate-tools');
|
||||||
|
|
||||||
function pickImportantTableInfo(engine, table) {
|
function pickImportantTableInfo(engine, table) {
|
||||||
const props = ['columnName', 'defaultValue'];
|
const props = ['columnName', 'defaultValue'];
|
||||||
@@ -15,7 +20,10 @@ function pickImportantTableInfo(engine, table) {
|
|||||||
columns: table.columns
|
columns: table.columns
|
||||||
.filter(x => x.columnName != 'rowid')
|
.filter(x => x.columnName != 'rowid')
|
||||||
.map(fp.pick(props))
|
.map(fp.pick(props))
|
||||||
.map(props => _.omitBy(props, x => x == null)),
|
.map(props => _.omitBy(props, x => x == null))
|
||||||
|
.map(props =>
|
||||||
|
_.omitBy(props, (v, k) => k == 'defaultValue' && v == 'NULL' && engine.setNullDefaultInsteadOfDrop)
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,27 +33,36 @@ function checkTableStructure(engine, t1, t2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function testTableDiff(engine, conn, driver, mangle) {
|
async function testTableDiff(engine, conn, driver, mangle) {
|
||||||
await driver.query(conn, `create table t0 (id int not null primary key)`);
|
await driver.query(conn, formatQueryWithoutParams(driver, `create table ~t0 (~id int not null primary key)`));
|
||||||
|
|
||||||
await driver.query(
|
await driver.query(
|
||||||
conn,
|
conn,
|
||||||
`create table t1 (
|
formatQueryWithoutParams(
|
||||||
col_pk int not null primary key,
|
driver,
|
||||||
col_std int,
|
`create table ~t1 (
|
||||||
col_def int default 12,
|
~col_pk int not null primary key,
|
||||||
${engine.skipReferences ? '' : 'col_fk int references t0(id),'}
|
~col_std int,
|
||||||
col_idx int,
|
~col_def int default 12,
|
||||||
col_uq int ${engine.skipUnique ? '' : 'unique'} ,
|
${engine.skipReferences ? '' : '~col_fk int references ~t0(~id),'}
|
||||||
col_ref int ${engine.skipUnique ? '' : 'unique'}
|
~col_idx int,
|
||||||
|
~col_uq int ${engine.skipUnique ? '' : 'unique'} ,
|
||||||
|
~col_ref int ${engine.skipUnique ? '' : 'unique'}
|
||||||
)`
|
)`
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!engine.skipIndexes) {
|
if (!engine.skipIndexes) {
|
||||||
await driver.query(conn, `create index idx1 on t1(col_idx)`);
|
await driver.query(conn, formatQueryWithoutParams(driver, `create index ~idx1 on ~t1(~col_idx)`));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!engine.skipReferences) {
|
if (!engine.skipReferences) {
|
||||||
await driver.query(conn, `create table t2 (id int not null primary key, fkval int null references t1(col_ref))`);
|
await driver.query(
|
||||||
|
conn,
|
||||||
|
formatQueryWithoutParams(
|
||||||
|
driver,
|
||||||
|
`create table ~t2 (~id int not null primary key, ~fkval int null references ~t1(~col_ref))`
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tget = x => x.tables.find(y => y.pureName == 't1');
|
const tget = x => x.tables.find(y => y.pureName == 't1');
|
||||||
@@ -175,5 +192,4 @@ describe('Alter table', () => {
|
|||||||
// });
|
// });
|
||||||
// })
|
// })
|
||||||
// );
|
// );
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -416,6 +416,7 @@ end;$$`,
|
|||||||
},
|
},
|
||||||
skipOnCI: true,
|
skipOnCI: true,
|
||||||
dbSnapshotBySeconds: true,
|
dbSnapshotBySeconds: true,
|
||||||
|
setNullDefaultInsteadOfDrop: true,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -60,9 +60,9 @@ class Dumper extends SqlDumper {
|
|||||||
// this.putCmd('^alter ^table %f ^rename ^to %i', obj, newname);
|
// this.putCmd('^alter ^table %f ^rename ^to %i', obj, newname);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// renameColumn(column, newcol) {
|
renameColumn(column, newcol) {
|
||||||
// this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', column, column.columnName, newcol);
|
this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', column, column.columnName, newcol);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// dropTable(obj, options = {}) {
|
// dropTable(obj, options = {}) {
|
||||||
// this.put('^drop ^table');
|
// this.put('^drop ^table');
|
||||||
@@ -87,13 +87,29 @@ class Dumper extends SqlDumper {
|
|||||||
// super.columnDefinition(col, options);
|
// super.columnDefinition(col, options);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// changeColumn(oldcol, newcol, constraints) {
|
changeColumn(oldcol, newcol, constraints) {
|
||||||
// if (oldcol.columnName != newcol.columnName) {
|
if (oldcol.columnName != newcol.columnName) {
|
||||||
// this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', oldcol, 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, newcol.columnName, newcol.dataType);
|
if (!testEqualTypes(oldcol, newcol) || oldcol.notNull != newcol.notNull) {
|
||||||
// }
|
this.putCmd(
|
||||||
|
'^alter ^table %f ^modify (%i %s %k)',
|
||||||
|
newcol,
|
||||||
|
newcol.columnName,
|
||||||
|
newcol.dataType,
|
||||||
|
newcol.notNull ? 'not null' : 'null'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (oldcol.defaultValue != newcol.defaultValue) {
|
||||||
|
if (newcol.defaultValue?.trim()) {
|
||||||
|
this.putCmd('^alter ^table %f ^modify (%i ^default %s)', newcol, newcol.columnName, newcol.defaultValue);
|
||||||
|
} else {
|
||||||
|
this.putCmd('^alter ^table %f ^modify (%i ^default ^null)', newcol, newcol.columnName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if (oldcol.notNull != newcol.notNull) {
|
// if (oldcol.notNull != newcol.notNull) {
|
||||||
// if (newcol.notNull) this.putCmd('^alter ^table %f ^alter ^column %i ^set ^not ^null', newcol, newcol.columnName);
|
// 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);
|
// else this.putCmd('^alter ^table %f ^alter ^column %i ^drop ^not ^null', newcol, newcol.columnName);
|
||||||
@@ -110,7 +126,7 @@ class Dumper extends SqlDumper {
|
|||||||
// );
|
// );
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
}
|
||||||
|
|
||||||
// putValue(value) {
|
// putValue(value) {
|
||||||
// if (value === true) this.putRaw('true');
|
// if (value === true) this.putRaw('true');
|
||||||
|
|||||||
Reference in New Issue
Block a user