initial menu design, simpler patch serialization
This commit is contained in:
20
lib/snippets/database/patch_actions/PatchActions.ts
Normal file
20
lib/snippets/database/patch_actions/PatchActions.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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>, June 2020
|
||||
*/
|
||||
|
||||
export { RenameColumn } from './RenameColumn';
|
||||
export { AddColumn } from './AddColumn';
|
||||
export { DropColumn } from './DropColumn';
|
||||
export { AddRelation } from './AddRelation';
|
||||
export { DropRelation } from './DropRelation';
|
||||
export { SetColumnType } from './SetColumnType';
|
||||
export { AddTable } from './AddTable';
|
||||
export { DropTable } from './DropTable';
|
||||
export { RenameTable } from './RenameTable';
|
||||
export { InsertData } from './InsertData';
|
||||
export { UpdateData } from './UpdateData';
|
||||
export { MutateDate } from './MutateDate';
|
||||
export { DeleteData } from './DeleteData';
|
@ -5,40 +5,39 @@
|
||||
* Created by Timo Hocker <timo@scode.ovh>, May 2020
|
||||
*/
|
||||
|
||||
import { Serializable } from '../classes/Serializable';
|
||||
import { Persistent } from '@sapphirecode/modelling';
|
||||
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;
|
||||
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
|
||||
) {
|
||||
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.properties = { column: 'string', table: 'string', new_name: 'string' };
|
||||
|
||||
this.column = res.groups.column;
|
||||
this.new_name = res.groups.new_name;
|
||||
this.table = res.groups.table;
|
||||
if (new_name === null || table === null) {
|
||||
const parsed = JSON.parse (column);
|
||||
this.assign_object (parsed);
|
||||
return;
|
||||
}
|
||||
|
||||
this.column = column;
|
||||
this.table = table;
|
||||
this.new_name = new_name;
|
||||
}
|
||||
|
||||
public serialize (): string {
|
||||
return `${this.table} ${this.column} ${this.new_name}`;
|
||||
this.set ('column', column);
|
||||
this.set ('new_name', new_name);
|
||||
this.set ('table', table);
|
||||
}
|
||||
|
||||
public apply (db: Database): void {
|
||||
|
Reference in New Issue
Block a user