mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
create, drop sql object
This commit is contained in:
parent
dcaf8351b5
commit
39748bdd6c
@ -16,6 +16,7 @@ import {
|
|||||||
UniqueInfo,
|
UniqueInfo,
|
||||||
CheckInfo,
|
CheckInfo,
|
||||||
AlterProcessor,
|
AlterProcessor,
|
||||||
|
SqlObjectInfo,
|
||||||
} from 'dbgate-types';
|
} from 'dbgate-types';
|
||||||
import _isString from 'lodash/isString';
|
import _isString from 'lodash/isString';
|
||||||
import _isNumber from 'lodash/isNumber';
|
import _isNumber from 'lodash/isNumber';
|
||||||
@ -577,4 +578,27 @@ export class SqlDumper implements AlterProcessor {
|
|||||||
|
|
||||||
this.dropTable({ ...oldTable, pureName: tmpTable });
|
this.dropTable({ ...oldTable, pureName: tmpTable });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createSqlObject(obj: SqlObjectInfo) {
|
||||||
|
this.putCmd(obj.createSql);
|
||||||
|
}
|
||||||
|
|
||||||
|
getSqlObjectSqlName(ojectTypeField: string) {
|
||||||
|
switch (ojectTypeField) {
|
||||||
|
case 'procedures':
|
||||||
|
return 'PROCEDURE';
|
||||||
|
case 'views':
|
||||||
|
return 'VIEW';
|
||||||
|
case 'functions':
|
||||||
|
return 'FUNCTION';
|
||||||
|
case 'triggers':
|
||||||
|
return 'TRIGGER';
|
||||||
|
case 'matviews':
|
||||||
|
return 'MATERIALIZED VIEW';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dropSqlObject(obj: SqlObjectInfo) {
|
||||||
|
this.putCmd('^drop %s %f', this.getSqlObjectSqlName(obj.objectTypeField), obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import {
|
|||||||
ColumnInfo,
|
ColumnInfo,
|
||||||
ConstraintInfo,
|
ConstraintInfo,
|
||||||
DatabaseInfo,
|
DatabaseInfo,
|
||||||
NamedObjectInfo,
|
SqlObjectInfo,
|
||||||
SqlDialect,
|
SqlDialect,
|
||||||
TableInfo,
|
TableInfo,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
@ -21,6 +21,16 @@ interface AlterOperation_DropTable {
|
|||||||
oldObject: TableInfo;
|
oldObject: TableInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface AlterOperation_CreateSqlObject {
|
||||||
|
operationType: 'createSqlObject';
|
||||||
|
newObject: SqlObjectInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AlterOperation_DropSqlObject {
|
||||||
|
operationType: 'dropSqlObject';
|
||||||
|
oldObject: SqlObjectInfo;
|
||||||
|
}
|
||||||
|
|
||||||
interface AlterOperation_RenameTable {
|
interface AlterOperation_RenameTable {
|
||||||
operationType: 'renameTable';
|
operationType: 'renameTable';
|
||||||
object: TableInfo;
|
object: TableInfo;
|
||||||
@ -88,6 +98,8 @@ type AlterOperation =
|
|||||||
| AlterOperation_RenameTable
|
| AlterOperation_RenameTable
|
||||||
| AlterOperation_RenameColumn
|
| AlterOperation_RenameColumn
|
||||||
| AlterOperation_RenameConstraint
|
| AlterOperation_RenameConstraint
|
||||||
|
| AlterOperation_CreateSqlObject
|
||||||
|
| AlterOperation_DropSqlObject
|
||||||
| AlterOperation_RecreateTable;
|
| AlterOperation_RecreateTable;
|
||||||
|
|
||||||
export class AlterPlan {
|
export class AlterPlan {
|
||||||
@ -108,6 +120,20 @@ export class AlterPlan {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createSqlObject(obj: SqlObjectInfo) {
|
||||||
|
this.operations.push({
|
||||||
|
operationType: 'createSqlObject',
|
||||||
|
newObject: obj,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
dropSqlObject(obj: SqlObjectInfo) {
|
||||||
|
this.operations.push({
|
||||||
|
operationType: 'dropSqlObject',
|
||||||
|
oldObject: obj,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
createColumn(column: ColumnInfo) {
|
createColumn(column: ColumnInfo) {
|
||||||
this.operations.push({
|
this.operations.push({
|
||||||
operationType: 'createColumn',
|
operationType: 'createColumn',
|
||||||
@ -419,6 +445,12 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor)
|
|||||||
case 'renameConstraint':
|
case 'renameConstraint':
|
||||||
processor.renameConstraint(op.object, op.newName);
|
processor.renameConstraint(op.object, op.newName);
|
||||||
break;
|
break;
|
||||||
|
case 'createSqlObject':
|
||||||
|
processor.createSqlObject(op.newObject);
|
||||||
|
break;
|
||||||
|
case 'dropSqlObject':
|
||||||
|
processor.dropSqlObject(op.oldObject);
|
||||||
|
break;
|
||||||
case 'recreateTable':
|
case 'recreateTable':
|
||||||
{
|
{
|
||||||
const newTable = _.cloneDeep(op.table);
|
const newTable = _.cloneDeep(op.table);
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import _ from 'lodash';
|
||||||
import {
|
import {
|
||||||
ColumnInfo,
|
ColumnInfo,
|
||||||
ConstraintInfo,
|
ConstraintInfo,
|
||||||
@ -8,6 +9,7 @@ import {
|
|||||||
IndexInfo,
|
IndexInfo,
|
||||||
CheckInfo,
|
CheckInfo,
|
||||||
UniqueInfo,
|
UniqueInfo,
|
||||||
|
SqlObjectInfo,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
|
|
||||||
export class DatabaseInfoAlterProcessor {
|
export class DatabaseInfoAlterProcessor {
|
||||||
@ -18,7 +20,18 @@ export class DatabaseInfoAlterProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dropTable(table: TableInfo) {
|
dropTable(table: TableInfo) {
|
||||||
this.db.tables = this.db.tables.filter(x => x.pureName != table.pureName && x.schemaName != table.schemaName);
|
_.remove(this.db.tables, x => x.pureName != table.pureName && x.schemaName != table.schemaName);
|
||||||
|
}
|
||||||
|
|
||||||
|
createSqlObject(obj: SqlObjectInfo) {
|
||||||
|
this.db[obj.objectTypeField].push(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
dropSqlObject(obj: SqlObjectInfo) {
|
||||||
|
_.remove(
|
||||||
|
this.db[obj.objectTypeField] as SqlObjectInfo[],
|
||||||
|
x => x.pureName != obj.pureName && x.schemaName != obj.schemaName
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
createColumn(column: ColumnInfo) {
|
createColumn(column: ColumnInfo) {
|
||||||
@ -33,7 +46,7 @@ export class DatabaseInfoAlterProcessor {
|
|||||||
|
|
||||||
dropColumn(column: ColumnInfo) {
|
dropColumn(column: ColumnInfo) {
|
||||||
const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName);
|
const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName);
|
||||||
table.columns = table.columns.filter(x => x.columnName != column.columnName);
|
_.remove(table.columns, x => x.columnName != column.columnName);
|
||||||
}
|
}
|
||||||
|
|
||||||
createConstraint(constraint: ConstraintInfo) {
|
createConstraint(constraint: ConstraintInfo) {
|
||||||
|
@ -309,18 +309,26 @@ export function createAlterDatabasePlan(
|
|||||||
): AlterPlan {
|
): AlterPlan {
|
||||||
const plan = new AlterPlan(db, driver.dialect);
|
const plan = new AlterPlan(db, driver.dialect);
|
||||||
|
|
||||||
for (const objectTypeField of ['tables']) {
|
for (const objectTypeField of ['tables', 'views', 'procedures', 'matviews', 'functions']) {
|
||||||
for (const oldobj of oldDb[objectTypeField]) {
|
for (const oldobj of oldDb[objectTypeField] || []) {
|
||||||
const newobj = newDb[objectTypeField].find(x => x.pairingId == oldobj.pairingId);
|
const newobj = (newDb[objectTypeField] || []).find(x => x.pairingId == oldobj.pairingId);
|
||||||
if (objectTypeField == 'tables') {
|
if (objectTypeField == 'tables') {
|
||||||
if (newobj == null) plan.dropTable(oldobj);
|
if (newobj == null) plan.dropTable(oldobj);
|
||||||
else planAlterTable(plan, oldobj, newobj, opts);
|
else planAlterTable(plan, oldobj, newobj, opts);
|
||||||
|
} else {
|
||||||
|
if (newobj == null) plan.dropSqlObject(oldobj);
|
||||||
|
else if (newobj.createSql != oldobj.createSql) {
|
||||||
|
plan.dropSqlObject(oldobj);
|
||||||
|
plan.createSqlObject(newobj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const newobj of newDb[objectTypeField]) {
|
for (const newobj of newDb[objectTypeField] || []) {
|
||||||
const oldobj = oldDb[objectTypeField].find(x => x.pairingId == newobj.pairingId);
|
const oldobj = (oldDb[objectTypeField] || []).find(x => x.pairingId == newobj.pairingId);
|
||||||
if (objectTypeField == 'tables') {
|
if (objectTypeField == 'tables') {
|
||||||
if (newobj == null) plan.createTable(newobj);
|
if (newobj == null) plan.createTable(newobj);
|
||||||
|
} else {
|
||||||
|
plan.createSqlObject(newobj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
packages/types/alter-processor.d.ts
vendored
4
packages/types/alter-processor.d.ts
vendored
@ -1,4 +1,4 @@
|
|||||||
import { ColumnInfo, ConstraintInfo, TableInfo } from './dbinfo';
|
import { ColumnInfo, ConstraintInfo, TableInfo, SqlObjectInfo } from './dbinfo';
|
||||||
|
|
||||||
export interface AlterProcessor {
|
export interface AlterProcessor {
|
||||||
createTable(table: TableInfo);
|
createTable(table: TableInfo);
|
||||||
@ -13,4 +13,6 @@ export interface AlterProcessor {
|
|||||||
renameColumn(column: ColumnInfo, newName: string);
|
renameColumn(column: ColumnInfo, newName: string);
|
||||||
renameConstraint(constraint: ConstraintInfo, newName: string);
|
renameConstraint(constraint: ConstraintInfo, newName: string);
|
||||||
recreateTable(oldTable: TableInfo, newTable: TableInfo);
|
recreateTable(oldTable: TableInfo, newTable: TableInfo);
|
||||||
|
createSqlObject(obj: SqlObjectInfo);
|
||||||
|
dropSqlObject(obj: SqlObjectInfo);
|
||||||
}
|
}
|
||||||
|
1
packages/types/dbinfo.d.ts
vendored
1
packages/types/dbinfo.d.ts
vendored
@ -67,6 +67,7 @@ export interface DatabaseObjectInfo extends NamedObjectInfo {
|
|||||||
export interface SqlObjectInfo extends DatabaseObjectInfo {
|
export interface SqlObjectInfo extends DatabaseObjectInfo {
|
||||||
createSql?: string;
|
createSql?: string;
|
||||||
requiresFormat?: boolean; // SQL is human unreadable, requires formatting (eg. MySQL views)
|
requiresFormat?: boolean; // SQL is human unreadable, requires formatting (eg. MySQL views)
|
||||||
|
objectTypeField?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TableInfo extends DatabaseObjectInfo {
|
export interface TableInfo extends DatabaseObjectInfo {
|
||||||
|
Loading…
Reference in New Issue
Block a user