default value support in table yaml files #296

This commit is contained in:
Jan Prochazka 2022-06-12 08:15:35 +02:00
parent 6794b79d0e
commit c104122a50
3 changed files with 37 additions and 0 deletions

View File

@ -297,4 +297,33 @@ describe('Deploy database', () => {
expect(res.rows[0].val.toString()).toEqual('5');
})
);
test.each(engines.enginesPostgre.map(engine => [engine.label, engine]))(
'Current timestamp default value - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(conn, driver, [
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int' },
{
name: 'val',
type: 'timestamp',
default: 'current_timestamp',
},
],
primaryKey: ['id'],
},
},
],
]);
await driver.query(conn, `insert into t1 (id) values (1)`);
const res = await driver.query(conn, ` select val from t1 where id = 1`);
expect(res.rows[0].val.toString().substring(0, 2)).toEqual('20');
})
);
});

View File

@ -141,6 +141,10 @@ const filterLocal = [
'-CockroachDB',
];
const enginesPostgre = engines.filter(x => x.label == 'PostgreSQL');
module.exports = process.env.CITEST
? engines.filter(x => !x.skipOnCI)
: engines.filter(x => filterLocal.find(y => x.label == y));
module.exports.enginesPostgre = enginesPostgre;

View File

@ -1,4 +1,5 @@
import { ColumnInfo, TableInfo, ForeignKeyInfo, DatabaseInfo } from 'dbgate-types';
import { StringNullableChain } from 'lodash';
import _cloneDeep from 'lodash/cloneDeep';
import _compact from 'lodash/compact';
import { DatabaseAnalyser } from './DatabaseAnalyser';
@ -11,6 +12,7 @@ export interface ColumnInfoYaml {
autoIncrement?: boolean;
references?: string;
primaryKey?: boolean;
default?: string;
}
export interface DatabaseModelFile {
@ -39,6 +41,7 @@ function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml
const res: ColumnInfoYaml = {
name: column.columnName,
type: column.dataType,
default: column.defaultValue,
};
if (column.autoIncrement) res.autoIncrement = true;
if (column.notNull) res.notNull = true;
@ -71,6 +74,7 @@ function columnInfoFromYaml(column: ColumnInfoYaml, table: TableInfoYaml): Colum
dataType: column.length ? `${column.type}(${column.length})` : column.type,
autoIncrement: column.autoIncrement,
notNull: column.notNull || (table.primaryKey && table.primaryKey.includes(column.name)),
defaultValue: column.default,
};
return res;
}