Skip to content

Commit

Permalink
feat: alterColumn with expressionGenerated (#1305)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeYHJ authored Jan 11, 2025
1 parent c50d7ca commit c4700f9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/operations/tables/alterColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface AlterColumnOptions {
generated?: null | false | SequenceGeneratedOptions;

sequenceGenerated?: null | false | SequenceGeneratedOptions;
expressionGenerated?: null | string;
}

export type AlterColumn = (
Expand All @@ -48,6 +49,7 @@ export function alterColumn(mOptions: MigrationOptions): AlterColumn {
notNull,
allowNull,
comment,
expressionGenerated,
} = options;

const sequenceGenerated =
Expand Down Expand Up @@ -92,6 +94,16 @@ export function alterColumn(mOptions: MigrationOptions): AlterColumn {
}
}
if (expressionGenerated !== undefined) {
if (typeof expressionGenerated === 'string') {
actions.push(`SET EXPRESSION AS (${expressionGenerated})`);
}
if (expressionGenerated === null) {
actions.push('DROP EXPRESSION');
}
}
const queries: string[] = [];
if (actions.length > 0) {
Expand Down
8 changes: 4 additions & 4 deletions test/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('migration', () => {
const filePaths = await getMigrationFilePaths(dir, { logger });

expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(92);
expect(filePaths).toHaveLength(93);
expect(filePaths).not.toContainEqual(expect.stringContaining('nested'));

for (const filePath of filePaths) {
Expand All @@ -78,7 +78,7 @@ describe('migration', () => {
});

expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(67);
expect(filePaths).toHaveLength(68);

for (const filePath of filePaths) {
expect(isAbsolute(filePath)).toBeTruthy();
Expand All @@ -94,7 +94,7 @@ describe('migration', () => {
});

expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(105);
expect(filePaths).toHaveLength(106);
expect(filePaths).toContainEqual(expect.stringContaining('nested'));

for (const filePath of filePaths) {
Expand All @@ -114,7 +114,7 @@ describe('migration', () => {
});

expect(Array.isArray(filePaths)).toBeTruthy();
expect(filePaths).toHaveLength(104);
expect(filePaths).toHaveLength(105);
expect(filePaths).toContainEqual(expect.stringContaining('nested'));

for (const filePath of filePaths) {
Expand Down
25 changes: 25 additions & 0 deletions test/migrations/093_alter_column_expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const getMajorVersion = async (pgm) => {
const [{ server_version: version }] = await pgm.db.select(
'SHOW "server_version"'
);
const [major] = version.split('.');
return Number(major);
};

const isSupportedVersion = (major) => major >= 17;

exports.up = async (pgm) => {
const major = await getMajorVersion(pgm);
if (isSupportedVersion(major)) {
pgm.createTable('t093', { id: { type: 'integer', notNull: true } });
pgm.alterColumn('t093', 'col1', { expressionGenerated: 'other + 1' });
pgm.alterColumn('t093', 'col2', { expressionGenerated: null });
}
};

exports.down = async (pgm) => {
const major = await getMajorVersion(pgm);
if (isSupportedVersion(major)) {
pgm.dropTable('t093');
}
};
47 changes: 46 additions & 1 deletion test/operations/columns/alterColumn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,59 @@ describe('operations', () => {
sequenceGenerated: null,
comment: 'Address of the distributor',
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER TABLE "distributors"
ALTER "address" DROP DEFAULT,
ALTER "address" SET DATA TYPE varchar(30) COLLATE C USING address::text,
ALTER "address" DROP NOT NULL,
ALTER "address" DROP IDENTITY;
COMMENT ON COLUMN "distributors"."address" IS $pga$Address of the distributor$pga$;`
);
});

it('should return sql statement with columnOptions and expressionGenerated', () => {
const statement = alterColumnFn('distributors', 'address', {
default: null,
type: 'varchar(30)',
collation: 'C',
using: 'address::text',
notNull: false,
sequenceGenerated: null,
comment: 'Address of the distributor',
expressionGenerated: 'other+1',
});
expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER TABLE "distributors"
ALTER "address" DROP DEFAULT,
ALTER "address" SET DATA TYPE varchar(30) COLLATE C USING address::text,
ALTER "address" DROP NOT NULL,
ALTER "address" DROP IDENTITY,
ALTER "address" SET EXPRESSION AS (other+1);
COMMENT ON COLUMN "distributors"."address" IS $pga$Address of the distributor$pga$;`
);
});

it('should return sql statement with columnOptions and drop expression', () => {
const statement = alterColumnFn('distributors', 'address', {
default: null,
type: 'varchar(30)',
collation: 'C',
using: 'address::text',
notNull: false,
sequenceGenerated: null,
comment: 'Address of the distributor',
expressionGenerated: null,
});
expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER TABLE "distributors"
ALTER "address" DROP DEFAULT,
ALTER "address" SET DATA TYPE varchar(30) COLLATE C USING address::text,
ALTER "address" DROP NOT NULL,
ALTER "address" DROP IDENTITY,
ALTER "address" DROP EXPRESSION;
COMMENT ON COLUMN "distributors"."address" IS $pga$Address of the distributor$pga$;`
);
});
Expand Down

0 comments on commit c4700f9

Please sign in to comment.