2020-05-07 18:40:35 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-06-28 14:51:37 +02:00
|
|
|
import { Persistent } from '@sapphirecode/modelling';
|
2020-04-19 18:49:07 +02:00
|
|
|
import { PatchAction } from '../classes/PatchAction';
|
|
|
|
import { Database } from '../classes/Database';
|
2020-04-18 20:43:02 +02:00
|
|
|
|
2020-06-28 14:51:37 +02:00
|
|
|
export class RenameColumn extends Persistent implements PatchAction {
|
|
|
|
public get table (): string {
|
2020-06-28 16:55:44 +02:00
|
|
|
return this.get ('table') as string;
|
2020-06-28 14:51:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get column (): string {
|
2020-06-28 16:55:44 +02:00
|
|
|
return this.get ('column') as string;
|
2020-06-28 14:51:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get new_name (): string {
|
2020-06-28 16:55:44 +02:00
|
|
|
return this.get ('new_name') as string;
|
2020-06-28 14:51:37 +02:00
|
|
|
}
|
2020-04-18 20:43:02 +02:00
|
|
|
|
2020-04-19 18:49:07 +02:00
|
|
|
public constructor (
|
|
|
|
column: string,
|
2020-06-28 16:55:44 +02:00
|
|
|
new_name: string,
|
|
|
|
table: string
|
2020-04-19 18:49:07 +02:00
|
|
|
) {
|
2020-06-28 16:55:44 +02:00
|
|
|
super ();
|
|
|
|
this.properties.column = 'string';
|
|
|
|
this.properties.table = 'string';
|
|
|
|
this.properties.new_name = 'string';
|
2020-04-18 20:43:02 +02:00
|
|
|
|
2020-06-28 14:51:37 +02:00
|
|
|
this.set ('column', column);
|
|
|
|
this.set ('new_name', new_name);
|
|
|
|
this.set ('table', table);
|
2020-04-19 18:49:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public apply (db: Database): void {
|
|
|
|
const table = db.get_table (this.table);
|
2020-04-23 17:51:19 +02:00
|
|
|
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;
|
2020-04-18 20:43:02 +02:00
|
|
|
}
|
|
|
|
}
|