implementing patches
This commit is contained in:
parent
858d778039
commit
b7706f9a42
@ -2,4 +2,13 @@ import { Table } from './Table';
|
|||||||
|
|
||||||
export class Database {
|
export class Database {
|
||||||
public tables: Array<Table> = [];
|
public tables: Array<Table> = [];
|
||||||
|
|
||||||
|
public get_table (name: string): Table|null {
|
||||||
|
for (const table of this.tables) {
|
||||||
|
if (table.name === name)
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
5
lib/snippets/database/classes/PatchAction.ts
Normal file
5
lib/snippets/database/classes/PatchAction.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { Database } from './Database';
|
||||||
|
|
||||||
|
export interface PatchAction {
|
||||||
|
apply(db: Database): void;
|
||||||
|
}
|
@ -7,4 +7,13 @@ export class Table {
|
|||||||
public constructor (name: string) {
|
public constructor (name: string) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get_column (name: string): Column|null {
|
||||||
|
for (const col of this.columns) {
|
||||||
|
if (col.name === name)
|
||||||
|
return col;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,42 @@
|
|||||||
import { Serializable } from '../classes/Serializable';
|
import { Serializable } from '../classes/Serializable';
|
||||||
|
import { PatchAction } from '../classes/PatchAction';
|
||||||
|
import { Database } from '../classes/Database';
|
||||||
|
|
||||||
export class RenameColumn implements Serializable {
|
export class RenameColumn implements Serializable, PatchAction {
|
||||||
|
public table: string;
|
||||||
public column: string;
|
public column: string;
|
||||||
public new_name: string;
|
public new_name: string;
|
||||||
|
|
||||||
public constructor (column: string, new_name = null) {
|
public constructor (
|
||||||
if (new_name === null) {
|
column: string,
|
||||||
const regex = /(?<column>[a-z_]+) (?<new_name>[a-z_]+)/iu;
|
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);
|
const res = regex.exec (column);
|
||||||
if (res === null || typeof res.groups === 'undefined')
|
if (res === null || typeof res.groups === 'undefined')
|
||||||
throw new Error ('invalid string to deserialize');
|
throw new Error ('invalid string to deserialize');
|
||||||
|
|
||||||
this.column = res.groups.column;
|
this.column = res.groups.column;
|
||||||
this.new_name = res.groups.new_name;
|
this.new_name = res.groups.new_name;
|
||||||
|
this.table = res.groups.table;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.column = column;
|
this.column = column;
|
||||||
this.new_name = new_name as unknown as string;
|
this.table = table;
|
||||||
|
this.new_name = new_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public serialize (): string {
|
public serialize (): string {
|
||||||
return `${this.column} ${this.new_name}`;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user