43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { Serializable } from '../classes/Serializable';
|
|
import { PatchAction } from '../classes/PatchAction';
|
|
import { Database } from '../classes/Database';
|
|
|
|
export class RenameColumn implements Serializable, PatchAction {
|
|
public table: string;
|
|
public column: string;
|
|
public new_name: string;
|
|
|
|
public constructor (
|
|
column: string,
|
|
new_name: string|null = null,
|
|
table: string|null = null
|
|
) {
|
|
if (new_name === null || table === null) {
|
|
const regex
|
|
= /(?<table>[a-z_]+) (?<column>[a-z_]+) (?<new_name>[a-z_]+)/iu;
|
|
const res = regex.exec (column);
|
|
if (res === null || typeof res.groups === 'undefined')
|
|
throw new Error ('invalid string to deserialize');
|
|
|
|
this.column = res.groups.column;
|
|
this.new_name = res.groups.new_name;
|
|
this.table = res.groups.table;
|
|
return;
|
|
}
|
|
|
|
this.column = column;
|
|
this.table = table;
|
|
this.new_name = new_name;
|
|
}
|
|
|
|
public serialize (): string {
|
|
return `${this.table} ${this.column} ${this.new_name}`;
|
|
}
|
|
|
|
public apply (db: Database): void {
|
|
const table = db.get_table (this.table);
|
|
const column = table?.get_column(this.column);
|
|
column?.name = this.new_name;
|
|
}
|
|
}
|