60 lines
1.6 KiB
TypeScript

/*
* Copyright (C) SapphireCode - All Rights Reserved
* This file is part of Snippeteer which is released under BSD-3-Clause.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import { Persistent } from '@sapphirecode/modelling';
import { PatchAction } from '../classes/PatchAction';
import { Database } from '../classes/Database';
export class RenameColumn extends Persistent implements PatchAction {
public get table (): string {
return this.get ('table');
}
public get column (): string {
return this.get ('column');
}
public get new_name (): string {
return this.get ('new_name');
}
public constructor (
column: string,
new_name: string|null = null,
table: string|null = null
) {
this.properties = { column: 'string', table: 'string', new_name: 'string' };
if (new_name === null || table === null) {
const parsed = JSON.parse (column);
this.assign_object (parsed);
return;
}
this.set ('column', column);
this.set ('new_name', new_name);
this.set ('table', table);
}
public apply (db: Database): void {
const table = db.get_table (this.table);
if (typeof table === 'undefined' || table === null) {
throw new Error (
`table ${this.table} not found`
);
}
const column = table.get_column (this.column);
if (typeof column === 'undefined' || column === null) {
throw new Error (
`column ${this.column} not found in table ${this.table}`
);
}
column.name = this.new_name;
}
}