diff --git a/lib/snippets/database/classes/Database.ts b/lib/snippets/database/classes/Database.ts
index 254447e..386d388 100644
--- a/lib/snippets/database/classes/Database.ts
+++ b/lib/snippets/database/classes/Database.ts
@@ -2,4 +2,13 @@ import { Table } from './Table';
export class Database {
public tables: Array
= [];
+
+ public get_table (name: string): Table|null {
+ for (const table of this.tables) {
+ if (table.name === name)
+ return table;
+ }
+
+ return null;
+ }
}
diff --git a/lib/snippets/database/classes/PatchAction.ts b/lib/snippets/database/classes/PatchAction.ts
new file mode 100644
index 0000000..aa91222
--- /dev/null
+++ b/lib/snippets/database/classes/PatchAction.ts
@@ -0,0 +1,5 @@
+import { Database } from './Database';
+
+export interface PatchAction {
+ apply(db: Database): void;
+}
diff --git a/lib/snippets/database/classes/Table.ts b/lib/snippets/database/classes/Table.ts
index d05cfaa..94abde1 100644
--- a/lib/snippets/database/classes/Table.ts
+++ b/lib/snippets/database/classes/Table.ts
@@ -7,4 +7,13 @@ export class Table {
public constructor (name: string) {
this.name = name;
}
+
+ public get_column (name: string): Column|null {
+ for (const col of this.columns) {
+ if (col.name === name)
+ return col;
+ }
+
+ return null;
+ }
}
diff --git a/lib/snippets/database/patch_actions/RenameColumn.ts b/lib/snippets/database/patch_actions/RenameColumn.ts
index 09e4a50..19f6773 100644
--- a/lib/snippets/database/patch_actions/RenameColumn.ts
+++ b/lib/snippets/database/patch_actions/RenameColumn.ts
@@ -1,26 +1,42 @@
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 new_name: string;
- public constructor (column: string, new_name = null) {
- if (new_name === null) {
- const regex = /(?[a-z_]+) (?[a-z_]+)/iu;
+ public constructor (
+ column: string,
+ new_name: string|null = null,
+ table: string|null = null
+ ) {
+ if (new_name === null || table === null) {
+ const regex
+ = /(?[a-z_]+) (?[a-z_]+) (?[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.new_name = new_name as unknown as string;
+ this.table = table;
+ this.new_name = new_name;
}
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;
}
}