mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-02 17:13:58 +00:00
deployDB shell WIP
This commit is contained in:
8
packages/api/src/shell/deployDb.js
Normal file
8
packages/api/src/shell/deployDb.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
const EnsureStreamHeaderStream = require('../utility/EnsureStreamHeaderStream');
|
||||||
|
const importDbModel = require('../utility/importDbModel');
|
||||||
|
|
||||||
|
async function deployDb(connection, modelFolder, options) {
|
||||||
|
const dbModel = await importDbModel(modelFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = deployDb;
|
||||||
65
packages/api/src/utility/importDbModel.js
Normal file
65
packages/api/src/utility/importDbModel.js
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
const fs = require('fs-extra');
|
||||||
|
const path = require('path');
|
||||||
|
const yaml = require('js-yaml');
|
||||||
|
const { tableInfoFromYaml, DatabaseAnalyser } = require('dbgate-tools');
|
||||||
|
|
||||||
|
async function importDbModel(inputDir) {
|
||||||
|
const tablesYaml = [];
|
||||||
|
|
||||||
|
const model = DatabaseAnalyser.createEmptyStructure();
|
||||||
|
|
||||||
|
for (const file of await fs.readdir(inputDir)) {
|
||||||
|
if (file.endsWith('.table.yaml') || file.endsWith('.sql')) {
|
||||||
|
const content = await fs.readFile(path.join(inputDir, file), { encoding: 'utf-8' });
|
||||||
|
|
||||||
|
if (file.endsWith('.table.yaml')) {
|
||||||
|
const json = yaml.load(content);
|
||||||
|
tablesYaml.push(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (file.endsWith('.view.sql')) {
|
||||||
|
model.views.push({
|
||||||
|
pureName: file.slice(0, -'.view.sql'.length),
|
||||||
|
createSql: content,
|
||||||
|
columns: [],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.endsWith('.matview.sql')) {
|
||||||
|
model.matviews.push({
|
||||||
|
pureName: file.slice(0, -'.matview.sql'.length),
|
||||||
|
createSql: content,
|
||||||
|
columns: [],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.endsWith('.proc.sql')) {
|
||||||
|
model.procedures.push({
|
||||||
|
pureName: file.slice(0, -'.proc.sql'.length),
|
||||||
|
createSql: content,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.endsWith('.func.sql')) {
|
||||||
|
model.functions.push({
|
||||||
|
pureName: file.slice(0, -'.func.sql'.length),
|
||||||
|
createSql: content,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.endsWith('.trigger.sql')) {
|
||||||
|
model.triggers.push({
|
||||||
|
pureName: file.slice(0, -'.trigger.sql'.length),
|
||||||
|
createSql: content,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
model.tables = tablesYaml.map(table => tableInfoFromYaml(table, tablesYaml));
|
||||||
|
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = importDbModel;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { ColumnInfo, TableInfo } from 'dbgate-types';
|
import { ColumnInfo, TableInfo, ForeignKeyInfo } from 'dbgate-types';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import _cloneDeep from 'lodash/cloneDeep';
|
import _cloneDeep from 'lodash/cloneDeep';
|
||||||
|
|
||||||
@@ -22,9 +22,7 @@ export interface ForeignKeyInfoYaml {
|
|||||||
deleteAction?: string;
|
deleteAction?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function foreignKeyInfoToYaml() {
|
// function foreignKeyInfoToYaml() {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml {
|
function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml {
|
||||||
const res: ColumnInfoYaml = {
|
const res: ColumnInfoYaml = {
|
||||||
@@ -55,6 +53,17 @@ function columnInfoToYaml(column: ColumnInfo, table: TableInfo): ColumnInfoYaml
|
|||||||
return res;
|
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 {
|
export function tableInfoToYaml(table: TableInfo): TableInfoYaml {
|
||||||
const tableCopy = _cloneDeep(table);
|
const tableCopy = _cloneDeep(table);
|
||||||
const res: TableInfoYaml = {
|
const res: TableInfoYaml = {
|
||||||
@@ -65,6 +74,40 @@ export function tableInfoToYaml(table: TableInfo): TableInfoYaml {
|
|||||||
if (tableCopy.primaryKey && !tableCopy.primaryKey['_dumped']) {
|
if (tableCopy.primaryKey && !tableCopy.primaryKey['_dumped']) {
|
||||||
res.primaryKey = tableCopy.primaryKey.columns.map(x => x.columnName);
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
22
packages/types/dbinfo.d.ts
vendored
22
packages/types/dbinfo.d.ts
vendored
@@ -23,10 +23,10 @@ export interface ColumnsConstraintInfo extends ConstraintInfo {
|
|||||||
export interface PrimaryKeyInfo extends ColumnsConstraintInfo {}
|
export interface PrimaryKeyInfo extends ColumnsConstraintInfo {}
|
||||||
|
|
||||||
export interface ForeignKeyInfo extends ColumnsConstraintInfo {
|
export interface ForeignKeyInfo extends ColumnsConstraintInfo {
|
||||||
refSchemaName: string;
|
refSchemaName?: string;
|
||||||
refTableName: string;
|
refTableName: string;
|
||||||
updateAction: string;
|
updateAction?: string;
|
||||||
deleteAction: string;
|
deleteAction?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IndexInfo extends ColumnsConstraintInfo {
|
export interface IndexInfo extends ColumnsConstraintInfo {
|
||||||
@@ -46,14 +46,14 @@ export interface ColumnInfo extends NamedObjectInfo {
|
|||||||
notNull: boolean;
|
notNull: boolean;
|
||||||
autoIncrement: boolean;
|
autoIncrement: boolean;
|
||||||
dataType: string;
|
dataType: string;
|
||||||
precision: number;
|
precision?: number;
|
||||||
scale: number;
|
scale?: number;
|
||||||
length: number;
|
length?: number;
|
||||||
computedExpression: string;
|
computedExpression?: string;
|
||||||
isPersisted: boolean;
|
isPersisted?: boolean;
|
||||||
isSparse: boolean;
|
isSparse?: boolean;
|
||||||
defaultValue: string;
|
defaultValue?: string;
|
||||||
defaultConstraint: string;
|
defaultConstraint?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DatabaseObjectInfo extends NamedObjectInfo {
|
export interface DatabaseObjectInfo extends NamedObjectInfo {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
import { currentArchive } from '../stores';
|
import { currentArchive } from '../stores';
|
||||||
|
|
||||||
import axiosInstance from '../utility/axiosInstance';
|
import axiosInstance from '../utility/axiosInstance';
|
||||||
|
import openNewTab from '../utility/openNewTab';
|
||||||
import AppObjectCore from './AppObjectCore.svelte';
|
import AppObjectCore from './AppObjectCore.svelte';
|
||||||
|
|
||||||
export let data;
|
export let data;
|
||||||
@@ -17,8 +18,22 @@
|
|||||||
axiosInstance.post('archive/delete-folder', { folder: data.name });
|
axiosInstance.post('archive/delete-folder', { folder: data.name });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleGenerateDeployScript = () => {
|
||||||
|
openNewTab(
|
||||||
|
{
|
||||||
|
title: 'Shell #',
|
||||||
|
icon: 'img shell',
|
||||||
|
tabComponent: 'ShellTab',
|
||||||
|
},
|
||||||
|
{ editor: `await dbgateApi.deployDb()` }
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
function createMenu() {
|
function createMenu() {
|
||||||
return [data.name != 'default' && { text: 'Delete', onClick: handleDelete }];
|
return [
|
||||||
|
data.name != 'default' && { text: 'Delete', onClick: handleDelete },
|
||||||
|
data.name != 'default' && { text: 'Generate deploy script', onClick: handleGenerateDeployScript },
|
||||||
|
];
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user