set NOT NULL for column with default value

This commit is contained in:
Jan Prochazka
2024-11-18 13:41:29 +01:00
parent 85c4821606
commit 1ce8f6bd1f
4 changed files with 80 additions and 26 deletions

View File

@@ -82,30 +82,34 @@ async function testDatabaseDeploy(engine, conn, driver, dbModelsYaml, options) {
dbdiffOptionsExtra.schemaMode = 'ignore';
for (const loadedDbModel of dbModelsYaml) {
const { sql, isEmpty } = await generateDeploySql({
systemConnection: conn.isPreparedOnly ? undefined : conn,
connection: conn.isPreparedOnly ? conn : undefined,
driver,
loadedDbModel,
dbdiffOptionsExtra,
});
console.debug('Generated deploy script:', sql);
if (!allowDropStatements) {
expect(sql.toUpperCase().includes('DROP ')).toBeFalsy();
}
if (_.isString(loadedDbModel)) {
await driver.script(conn, loadedDbModel);
} else {
const { sql, isEmpty } = await generateDeploySql({
systemConnection: conn.isPreparedOnly ? undefined : conn,
connection: conn.isPreparedOnly ? conn : undefined,
driver,
loadedDbModel,
dbdiffOptionsExtra,
});
console.debug('Generated deploy script:', sql);
if (!allowDropStatements) {
expect(sql.toUpperCase().includes('DROP ')).toBeFalsy();
}
console.log('dbModelsYaml.length', dbModelsYaml.length, index);
if (testEmptyLastScript && index == dbModelsYaml.length - 1) {
expect(isEmpty).toBeTruthy();
}
console.log('dbModelsYaml.length', dbModelsYaml.length, index);
if (testEmptyLastScript && index == dbModelsYaml.length - 1) {
expect(isEmpty).toBeTruthy();
}
await deployDb({
systemConnection: conn.isPreparedOnly ? undefined : conn,
connection: conn.isPreparedOnly ? conn : undefined,
driver,
loadedDbModel,
dbdiffOptionsExtra,
});
await deployDb({
systemConnection: conn.isPreparedOnly ? undefined : conn,
connection: conn.isPreparedOnly ? conn : undefined,
driver,
loadedDbModel,
dbdiffOptionsExtra,
});
}
index++;
}
@@ -416,6 +420,48 @@ describe('Deploy database', () => {
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Change column to NOT NULL column with default - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(engine, conn, driver, [
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int', notNull: true },
{ name: 'val', type: 'int' },
],
primaryKey: ['id'],
},
},
],
'insert into t1 (id, val) values (1, 1); insert into t1 (id) values (2)',
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int', notNull: true },
{ name: 'val', type: 'int', notNull: true, default: '20' },
],
primaryKey: ['id'],
},
},
],
]);
const res1 = await driver.query(conn, `select val from t1 where id = 1`);
expect(res1.rows[0].val).toEqual(1);
const res2 = await driver.query(conn, `select val from t1 where id = 2`);
expect(res2.rows[0].val).toEqual(20);
})
);
const T1 = {
name: 't1.table.yaml',
json: {