mirror of
https://github.com/immich-app/immich.git
synced 2026-04-20 01:18:47 -04:00
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { asColumnComment, getColumnModifiers, getColumnType } from 'src/sql-tools/helpers';
|
|
import { SqlTransformer } from 'src/sql-tools/transformers/types';
|
|
import { ColumnChanges, DatabaseColumn } from 'src/sql-tools/types';
|
|
|
|
export const transformColumns: SqlTransformer = (ctx, item) => {
|
|
switch (item.type) {
|
|
case 'ColumnAdd': {
|
|
return asColumnAdd(item.column);
|
|
}
|
|
|
|
case 'ColumnAlter': {
|
|
return asColumnAlter(item.tableName, item.columnName, item.changes);
|
|
}
|
|
|
|
case 'ColumnDrop': {
|
|
return asColumnDrop(item.tableName, item.columnName);
|
|
}
|
|
|
|
default: {
|
|
return false;
|
|
}
|
|
}
|
|
};
|
|
|
|
const asColumnAdd = (column: DatabaseColumn): string => {
|
|
return (
|
|
`ALTER TABLE "${column.tableName}" ADD "${column.name}" ${getColumnType(column)}` + getColumnModifiers(column) + ';'
|
|
);
|
|
};
|
|
|
|
const asColumnDrop = (tableName: string, columnName: string): string => {
|
|
return `ALTER TABLE "${tableName}" DROP COLUMN "${columnName}";`;
|
|
};
|
|
|
|
export const asColumnAlter = (tableName: string, columnName: string, changes: ColumnChanges): string[] => {
|
|
const base = `ALTER TABLE "${tableName}" ALTER COLUMN "${columnName}"`;
|
|
const items: string[] = [];
|
|
if (changes.nullable !== undefined) {
|
|
items.push(changes.nullable ? `${base} DROP NOT NULL;` : `${base} SET NOT NULL;`);
|
|
}
|
|
|
|
if (changes.default !== undefined) {
|
|
items.push(`${base} SET DEFAULT ${changes.default};`);
|
|
}
|
|
|
|
if (changes.storage !== undefined) {
|
|
items.push(`${base} SET STORAGE ${changes.storage.toUpperCase()};`);
|
|
}
|
|
|
|
if (changes.comment !== undefined) {
|
|
items.push(asColumnComment(tableName, columnName, changes.comment));
|
|
}
|
|
|
|
return items;
|
|
};
|