mirror of
https://github.com/immich-app/immich.git
synced 2025-12-25 09:14:58 +03:00
feat: naming strategy (#19848)
* feat: naming strategy * feat: detect renames
This commit is contained in:
@@ -13,6 +13,7 @@ describe(transformColumns.name, () => {
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
primary: false,
|
||||
type: 'character varying',
|
||||
nullable: false,
|
||||
isArray: false,
|
||||
@@ -30,6 +31,7 @@ describe(transformColumns.name, () => {
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
primary: false,
|
||||
type: 'character varying',
|
||||
nullable: true,
|
||||
isArray: false,
|
||||
@@ -47,6 +49,7 @@ describe(transformColumns.name, () => {
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
primary: false,
|
||||
type: 'character varying',
|
||||
enumName: 'table1_column1_enum',
|
||||
nullable: true,
|
||||
@@ -65,6 +68,7 @@ describe(transformColumns.name, () => {
|
||||
column: {
|
||||
name: 'column1',
|
||||
tableName: 'table1',
|
||||
primary: false,
|
||||
type: 'boolean',
|
||||
nullable: true,
|
||||
isArray: true,
|
||||
|
||||
@@ -12,8 +12,12 @@ export const transformColumns: SqlTransformer = (ctx, item) => {
|
||||
return asColumnAlter(item.tableName, item.columnName, item.changes);
|
||||
}
|
||||
|
||||
case 'ColumnRename': {
|
||||
return `ALTER TABLE "${item.tableName}" RENAME COLUMN "${item.oldName}" TO "${item.newName}";`;
|
||||
}
|
||||
|
||||
case 'ColumnDrop': {
|
||||
return asColumnDrop(item.tableName, item.columnName);
|
||||
return `ALTER TABLE "${item.tableName}" DROP COLUMN "${item.columnName}";`;
|
||||
}
|
||||
|
||||
default: {
|
||||
@@ -28,10 +32,6 @@ const asColumnAdd = (column: DatabaseColumn): string => {
|
||||
);
|
||||
};
|
||||
|
||||
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[] = [];
|
||||
|
||||
@@ -5,11 +5,15 @@ import { ActionType, ConstraintType, DatabaseConstraint } from 'src/sql-tools/ty
|
||||
export const transformConstraints: SqlTransformer = (ctx, item) => {
|
||||
switch (item.type) {
|
||||
case 'ConstraintAdd': {
|
||||
return asConstraintAdd(item.constraint);
|
||||
return `ALTER TABLE "${item.constraint.tableName}" ADD ${asConstraintBody(item.constraint)};`;
|
||||
}
|
||||
|
||||
case 'ConstraintRename': {
|
||||
return `ALTER TABLE "${item.tableName}" RENAME CONSTRAINT "${item.oldName}" TO "${item.newName}";`;
|
||||
}
|
||||
|
||||
case 'ConstraintDrop': {
|
||||
return asConstraintDrop(item.tableName, item.constraintName);
|
||||
return `ALTER TABLE "${item.tableName}" DROP CONSTRAINT "${item.constraintName}";`;
|
||||
}
|
||||
default: {
|
||||
return false;
|
||||
@@ -52,11 +56,3 @@ export const asConstraintBody = (constraint: DatabaseConstraint): string => {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const asConstraintAdd = (constraint: DatabaseConstraint): string | string[] => {
|
||||
return `ALTER TABLE "${constraint.tableName}" ADD ${asConstraintBody(constraint)};`;
|
||||
};
|
||||
|
||||
export const asConstraintDrop = (tableName: string, constraintName: string): string => {
|
||||
return `ALTER TABLE "${tableName}" DROP CONSTRAINT "${constraintName}";`;
|
||||
};
|
||||
|
||||
@@ -8,8 +8,12 @@ export const transformIndexes: SqlTransformer = (ctx, item) => {
|
||||
return asIndexCreate(item.index);
|
||||
}
|
||||
|
||||
case 'IndexRename': {
|
||||
return `ALTER INDEX "${item.oldName}" RENAME TO "${item.newName}";`;
|
||||
}
|
||||
|
||||
case 'IndexDrop': {
|
||||
return asIndexDrop(item.indexName);
|
||||
return `DROP INDEX "${item.indexName}";`;
|
||||
}
|
||||
|
||||
default: {
|
||||
@@ -50,7 +54,3 @@ export const asIndexCreate = (index: DatabaseIndex): string => {
|
||||
|
||||
return sql + ';';
|
||||
};
|
||||
|
||||
export const asIndexDrop = (indexName: string): string => {
|
||||
return `DROP INDEX "${indexName}";`;
|
||||
};
|
||||
|
||||
@@ -19,6 +19,7 @@ const table1: DatabaseTable = {
|
||||
},
|
||||
{
|
||||
name: 'column2',
|
||||
primary: false,
|
||||
tableName: 'table1',
|
||||
type: 'character varying',
|
||||
nullable: true,
|
||||
@@ -106,6 +107,7 @@ describe(transformTables.name, () => {
|
||||
columns: [
|
||||
{
|
||||
tableName: 'table1',
|
||||
primary: false,
|
||||
name: 'column1',
|
||||
type: 'character varying',
|
||||
isArray: false,
|
||||
@@ -137,6 +139,7 @@ describe(transformTables.name, () => {
|
||||
{
|
||||
tableName: 'table1',
|
||||
name: 'column1',
|
||||
primary: false,
|
||||
type: 'character varying',
|
||||
isArray: false,
|
||||
nullable: true,
|
||||
@@ -167,6 +170,7 @@ describe(transformTables.name, () => {
|
||||
columns: [
|
||||
{
|
||||
tableName: 'table1',
|
||||
primary: false,
|
||||
name: 'column1',
|
||||
type: 'character varying',
|
||||
length: 2,
|
||||
@@ -198,6 +202,7 @@ describe(transformTables.name, () => {
|
||||
columns: [
|
||||
{
|
||||
tableName: 'table1',
|
||||
primary: false,
|
||||
name: 'column1',
|
||||
type: 'character varying',
|
||||
isArray: true,
|
||||
|
||||
Reference in New Issue
Block a user