clickhouse + mysql: modify table option

This commit is contained in:
Jan Prochazka 2024-09-11 15:09:16 +02:00
parent 7ad1950777
commit fb39cd1302
12 changed files with 88 additions and 21 deletions

View File

@ -304,7 +304,7 @@ export class SqlDumper implements AlterProcessor {
} }
tableOptions(table: TableInfo) { tableOptions(table: TableInfo) {
const options = this.driver.getTableFormOptions('sqlCreateTable'); const options = this.driver?.dialect?.getTableFormOptions?.('sqlCreateTable') || [];
for (const option of options) { for (const option of options) {
if (table[option.name]) { if (table[option.name]) {
this.put('&n'); this.put('&n');
@ -686,6 +686,23 @@ export class SqlDumper implements AlterProcessor {
this.putCmd('^drop %s %f', this.getSqlObjectSqlName(obj.objectTypeField), obj); this.putCmd('^drop %s %f', this.getSqlObjectSqlName(obj.objectTypeField), obj);
} }
setTableOption(table: TableInfo, optionName: string, optionValue: string) {
const options = this?.dialect?.getTableFormOptions?.('sqlAlterTable');
const option = options?.find(x => x.name == optionName && !x.disabled);
if (!option) {
return;
}
this.setTableOptionCore(table, optionName, optionValue, option.sqlFormatString);
this.endCommand();
}
setTableOptionCore(table: TableInfo, optionName: string, optionValue: string, formatString: string) {
this.put('^alter ^table %f ', table);
this.put(formatString, optionValue);
}
fillPreloadedRows( fillPreloadedRows(
table: NamedObjectInfo, table: NamedObjectInfo,
oldRows: any[], oldRows: any[],

View File

@ -97,6 +97,13 @@ interface AlterOperation_FillPreloadedRows {
autoIncrementColumn: string; autoIncrementColumn: string;
} }
interface AlterOperation_SetTableOption {
operationType: 'setTableOption';
table: TableInfo;
optionName: string;
optionValue: string;
}
type AlterOperation = type AlterOperation =
| AlterOperation_CreateColumn | AlterOperation_CreateColumn
| AlterOperation_ChangeColumn | AlterOperation_ChangeColumn
@ -112,7 +119,8 @@ type AlterOperation =
| AlterOperation_CreateSqlObject | AlterOperation_CreateSqlObject
| AlterOperation_DropSqlObject | AlterOperation_DropSqlObject
| AlterOperation_RecreateTable | AlterOperation_RecreateTable
| AlterOperation_FillPreloadedRows; | AlterOperation_FillPreloadedRows
| AlterOperation_SetTableOption;
export class AlterPlan { export class AlterPlan {
recreates = { recreates = {
@ -253,6 +261,15 @@ export class AlterPlan {
}); });
} }
setTableOption(table: TableInfo, optionName: string, optionValue: string) {
this.operations.push({
operationType: 'setTableOption',
table,
optionName,
optionValue,
});
}
run(processor: AlterProcessor) { run(processor: AlterProcessor) {
for (const op of this.operations) { for (const op of this.operations) {
runAlterOperation(op, processor); runAlterOperation(op, processor);
@ -575,6 +592,9 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor)
case 'dropSqlObject': case 'dropSqlObject':
processor.dropSqlObject(op.oldObject); processor.dropSqlObject(op.oldObject);
break; break;
case 'setTableOption':
processor.setTableOption(op.table, op.optionName, op.optionValue);
break;
case 'fillPreloadedRows': case 'fillPreloadedRows':
processor.fillPreloadedRows(op.table, op.oldRows, op.newRows, op.key, op.insertOnly, op.autoIncrementColumn); processor.fillPreloadedRows(op.table, op.oldRows, op.newRows, op.key, op.insertOnly, op.autoIncrementColumn);
break; break;

View File

@ -129,4 +129,9 @@ export class DatabaseInfoAlterProcessor {
tableInfo.preloadedRowsKey = key; tableInfo.preloadedRowsKey = key;
tableInfo.preloadedRowsInsertOnly = insertOnly; tableInfo.preloadedRowsInsertOnly = insertOnly;
} }
setTableOption(table: TableInfo, optionName: string, optionValue: string) {
const tableInfo = this.db.tables.find(x => x.pureName == table.pureName && x.schemaName == table.schemaName);
tableInfo[optionName] = optionValue;
}
} }

View File

@ -425,6 +425,20 @@ function planAlterTable(plan: AlterPlan, oldTable: TableInfo, newTable: TableInf
constraintPairs.filter(x => x[0] == null).forEach(x => plan.createConstraint(x[1])); constraintPairs.filter(x => x[0] == null).forEach(x => plan.createConstraint(x[1]));
planTablePreload(plan, oldTable, newTable); planTablePreload(plan, oldTable, newTable);
planChangeTableOptions(plan, oldTable, newTable, opts);
}
function planChangeTableOptions(plan: AlterPlan, oldTable: TableInfo, newTable: TableInfo, opts: DbDiffOptions) {
for(const option of plan.dialect?.getTableFormOptions?.('sqlAlterTable') || []) {
if (option.disabled) {
continue;
}
const name = option.name;
if (oldTable[name] != newTable[name] && (oldTable[name]||newTable[name])) {
plan.setTableOption(newTable, name, newTable[name]);
}
}
} }
export function testEqualTables( export function testEqualTables(

View File

@ -15,6 +15,7 @@ export interface AlterProcessor {
recreateTable(oldTable: TableInfo, newTable: TableInfo); recreateTable(oldTable: TableInfo, newTable: TableInfo);
createSqlObject(obj: SqlObjectInfo); createSqlObject(obj: SqlObjectInfo);
dropSqlObject(obj: SqlObjectInfo); dropSqlObject(obj: SqlObjectInfo);
setTableOption(table: TableInfo, optionName: string, optionValue: string);
fillPreloadedRows( fillPreloadedRows(
table: NamedObjectInfo, table: NamedObjectInfo,
oldRows: any[], oldRows: any[],

View File

@ -48,5 +48,6 @@ export interface SqlDialect {
getTableFormOptions(intent: 'newTableForm' | 'editTableForm' | 'sqlCreateTable' | 'sqlAlterTable'): { getTableFormOptions(intent: 'newTableForm' | 'editTableForm' | 'sqlCreateTable' | 'sqlAlterTable'): {
name: string; name: string;
sqlFormatString: string; sqlFormatString: string;
disabled?: boolean;
}[]; }[];
} }

View File

@ -4,6 +4,7 @@
import FormArgumentList from '../forms/FormArgumentList.svelte'; import FormArgumentList from '../forms/FormArgumentList.svelte';
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import FormProviderCore from '../forms/FormProviderCore.svelte'; import FormProviderCore from '../forms/FormProviderCore.svelte';
import createRef from '../utility/createRef';
export let title; export let title;
export let fieldDefinitions; export let fieldDefinitions;

View File

@ -89,6 +89,7 @@
export let setTableInfo; export let setTableInfo;
export let dbInfo; export let dbInfo;
export let driver; export let driver;
export let resetCounter;
$: isWritable = !!setTableInfo; $: isWritable = !!setTableInfo;
@ -161,6 +162,7 @@
<div class="wrapper"> <div class="wrapper">
{#if tableFormOptions} {#if tableFormOptions}
{#key resetCounter}
<ObjectFieldsEditor <ObjectFieldsEditor
title="Table properties" title="Table properties"
fieldDefinitions={tableFormOptions} fieldDefinitions={tableFormOptions}
@ -174,6 +176,7 @@
} }
}} }}
/> />
{/key}
{/if} {/if}
<ObjectListControl <ObjectListControl

View File

@ -72,6 +72,7 @@
let domEditor; let domEditor;
let savedName; let savedName;
let resetCounter = 0;
export const activator = createActivator('TableStructureTab', true); export const activator = createActivator('TableStructureTab', true);
@ -157,7 +158,8 @@
export async function reset() { export async function reset() {
await apiCall('database-connections/sync-model', { conid, database }); await apiCall('database-connections/sync-model', { conid, database });
clearEditorData(); await clearEditorData();
resetCounter++;
} }
// $: { // $: {
@ -172,6 +174,7 @@
tableInfo={showTable} tableInfo={showTable}
dbInfo={$dbInfo} dbInfo={$dbInfo}
{driver} {driver}
{resetCounter}
setTableInfo={objectTypeField == 'tables' && !$connection?.isReadOnly && hasPermission(`dbops/model/edit`) setTableInfo={objectTypeField == 'tables' && !$connection?.isReadOnly && hasPermission(`dbops/model/edit`)
? tableInfoUpdater => ? tableInfoUpdater =>
setEditorData(tbl => setEditorData(tbl =>

View File

@ -1,6 +1,10 @@
const { SqlDumper } = require('dbgate-tools'); const { SqlDumper } = require('dbgate-tools');
class Dumper extends SqlDumper { class Dumper extends SqlDumper {
setTableOptionCore(table, optionName, optionValue, formatString) {
this.put('^alter ^table %f ^modify ', table);
this.put(formatString, optionValue);
}
} }
module.exports = Dumper; module.exports = Dumper;

View File

@ -74,7 +74,7 @@ const dialect = {
}, },
getTableFormOptions(intent) { getTableFormOptions(intent) {
const isNewTable = intent == 'newTableForm'; const isNewTable = intent == 'newTableForm' || intent == 'sqlCreateTable';
return [ return [
{ {
type: isNewTable ? 'dropdowntext' : 'text', type: isNewTable ? 'dropdowntext' : 'text',

View File

@ -105,21 +105,19 @@ const dialect = {
}, },
getTableFormOptions(intent) { getTableFormOptions(intent) {
const isNewTable = intent == 'newTableForm';
return [ return [
{ {
type: isNewTable ? 'dropdowntext' : 'text', type: 'dropdowntext',
options: this.getSupportedEngines(), options: this.getSupportedEngines(),
label: 'Engine', label: 'Engine',
name: 'tableEngine', name: 'tableEngine',
sqlFormatString: '^engine = %s', sqlFormatString: '^engine = %s',
disabled: !isNewTable,
}, },
{ {
type: 'text', type: 'text',
label: 'Comment', label: 'Comment',
name: 'objectComment', name: 'objectComment',
sqlFormatString: '^comment %v', sqlFormatString: '^comment = %v',
}, },
]; ];
}, },