deployDB shell WIP

This commit is contained in:
Jan Prochazka
2021-09-30 13:03:52 +02:00
parent 50f8f84967
commit 425bed050b
5 changed files with 149 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
import { ColumnInfo, TableInfo } from 'dbgate-types';
import { ColumnInfo, TableInfo, ForeignKeyInfo } from 'dbgate-types';
import _ from 'lodash';
import _cloneDeep from 'lodash/cloneDeep';
@@ -19,12 +19,10 @@ export interface TableInfoYaml {
}
export interface ForeignKeyInfoYaml {
deleteAction?: string;
deleteAction?: string;
}
function foreignKeyInfoToYaml() {
}
// function foreignKeyInfoToYaml() {}
function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml {
const res: ColumnInfoYaml = {
@@ -55,6 +53,17 @@ function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml
return res;
}
function columnInfoFromYaml(column: ColumnInfoYaml, table: TableInfoYaml): ColumnInfo {
const res: ColumnInfo = {
pureName: table.name,
columnName: column.name,
dataType: column.type,
autoIncrement: column.autoIncrement,
notNull: column.notNull,
};
return res;
}
export function tableInfoToYaml(table: TableInfo): TableInfoYaml {
const tableCopy = _cloneDeep(table);
const res: TableInfoYaml = {
@@ -65,6 +74,40 @@ export function tableInfoToYaml(table: TableInfo): TableInfoYaml {
if (tableCopy.primaryKey && !tableCopy.primaryKey['_dumped']) {
res.primaryKey = tableCopy.primaryKey.columns.map(x => x.columnName);
}
const foreignKeys = (tableCopy.foreignKeys || []).filter(x => !x['_dumped']).map(foreignKeyInfoToYaml);
// const foreignKeys = (tableCopy.foreignKeys || []).filter(x => !x['_dumped']).map(foreignKeyInfoToYaml);
return res;
}
function convertForeignKeyFromYaml(col: ColumnInfoYaml, table: TableInfoYaml, allTables: TableInfoYaml[]): ForeignKeyInfo {
const refTable = allTables.find(x => x.name == col.references);
if (!refTable || !refTable.primaryKey) return null;
return {
constraintType: 'foreignKey',
pureName: table.name,
refTableName: col.references,
columns: [
{
columnName: col.name,
refColumnName: refTable.primaryKey[0],
},
],
};
}
export function tableInfoFromYaml(table: TableInfoYaml, allTables: TableInfoYaml[]): TableInfo {
const res: TableInfo = {
pureName: table.name,
columns: table.columns.map(c => columnInfoFromYaml(c, table)),
foreignKeys: _.compact(
table.columns.filter(x => x.references).map(col => convertForeignKeyFromYaml(col, table, allTables))
),
};
if (table.primaryKey) {
res.primaryKey = {
pureName: table.name,
constraintType: 'primaryKey',
columns: table.primaryKey.map(columnName => ({ columnName })),
};
}
return res;
}