Skip to content

feat: add renameIndex method#1638

Merged
Shinigami92 merged 88 commits into
salsita:mainfrom
beterrabaA:beterraba-add-alter-index
Jul 5, 2026
Merged

feat: add renameIndex method#1638
Shinigami92 merged 88 commits into
salsita:mainfrom
beterrabaA:beterraba-add-alter-index

Conversation

@beterrabaA

Copy link
Copy Markdown
Contributor

I created a method and tests cases to rename index with options.
Solving the problem from issue #1178

image

@github-actions

github-actions Bot commented May 21, 2026

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 93.71% (🎯 90%)
⬆️ +0.03%
1565 / 1670
🔵 Statements 93.62% (🎯 90%)
⬆️ +0.03%
1586 / 1694
🔵 Functions 95.1% (🎯 90%)
⬆️ +0.04%
350 / 368
🔵 Branches 87.33% (🎯 85%)
🟰 ±0%
993 / 1137
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/migrationBuilder.ts 95.53%
⬆️ +0.04%
62.5%
🟰 ±0%
90%
🟰 ±0%
95.53%
⬆️ +0.04%
842-845, 983-987
src/operations/indexes/index.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
src/operations/indexes/renameIndex.ts 100% 100% 100% 100%
Unchanged Files
src/db.ts 78.43% 75% 76.92% 78% 88-90, 123-135, 171, 175-176
src/index.ts 100% 100% 100% 100%
src/logger.ts 100% 100% 100% 100%
src/migration.ts 76.53% 69.11% 80% 76.28% 129-131, 153-168, 233-268, 334, 361-363, 371, 389-391, 421
src/migrationLoader.ts 91.26% 84.74% 90.9% 93.87% 277, 417, 421, 424, 427, 461, 480, 487-488
src/migrationOptions.ts 100% 100% 100% 100%
src/pgType.ts 100% 100% 100% 100%
src/runner.ts 78.86% 63.88% 82.6% 78.15% 217-219, 238-240, 292, 337, 347-350, 364, 376-378, 402, 443-454, 459-461, 482-483, 493, 495-503
src/sqlMigration.ts 100% 100% 100% 100%
src/operations/sql.ts 100% 50% 100% 100%
src/operations/casts/createCast.ts 100% 100% 100% 100%
src/operations/casts/dropCast.ts 100% 100% 100% 100%
src/operations/casts/index.ts 100% 100% 100% 100%
src/operations/domains/alterDomain.ts 100% 100% 100% 100%
src/operations/domains/createDomain.ts 100% 95.45% 100% 100%
src/operations/domains/dropDomain.ts 100% 100% 100% 100%
src/operations/domains/index.ts 100% 100% 100% 100%
src/operations/domains/renameDomain.ts 100% 100% 100% 100%
src/operations/domains/shared.ts 100% 100% 100% 100%
src/operations/extensions/createExtension.ts 100% 100% 100% 100%
src/operations/extensions/dropExtension.ts 100% 100% 100% 100%
src/operations/extensions/index.ts 100% 100% 100% 100%
src/operations/extensions/shared.ts 100% 100% 100% 100%
src/operations/functions/createFunction.ts 96.29% 92% 100% 96.29% 71-73
src/operations/functions/dropFunction.ts 100% 100% 100% 100%
src/operations/functions/index.ts 100% 100% 100% 100%
src/operations/functions/renameFunction.ts 100% 100% 100% 100%
src/operations/functions/shared.ts 100% 100% 100% 100%
src/operations/grants/grantOnSchemas.ts 100% 100% 100% 100%
src/operations/grants/grantOnTables.ts 100% 100% 100% 100%
src/operations/grants/grantRoles.ts 100% 100% 100% 100%
src/operations/grants/index.ts 100% 100% 100% 100%
src/operations/grants/revokeOnSchemas.ts 100% 100% 100% 100%
src/operations/grants/revokeOnTables.ts 100% 100% 100% 100%
src/operations/grants/revokeRoles.ts 100% 100% 100% 100%
src/operations/grants/shared.ts 100% 75% 100% 100%
src/operations/indexes/createIndex.ts 100% 100% 100% 100%
src/operations/indexes/dropIndex.ts 100% 100% 100% 100%
src/operations/indexes/shared.ts 100% 88.23% 100% 100%
src/operations/materializedViews/alterMaterializedView.ts 100% 100% 100% 100%
src/operations/materializedViews/createMaterializedView.ts 100% 100% 100% 100%
src/operations/materializedViews/dropMaterializedView.ts 100% 100% 100% 100%
src/operations/materializedViews/index.ts 100% 100% 100% 100%
src/operations/materializedViews/refreshMaterializedView.ts 100% 100% 100% 100%
src/operations/materializedViews/renameMaterializedView.ts 100% 100% 100% 100%
src/operations/materializedViews/renameMaterializedViewColumn.ts 100% 100% 100% 100%
src/operations/materializedViews/shared.ts 100% 83.33% 100% 100%
src/operations/operators/addToOperatorFamily.ts 100% 100% 100% 100%
src/operations/operators/createOperator.ts 100% 88.88% 100% 100%
src/operations/operators/createOperatorClass.ts 100% 75% 100% 100%
src/operations/operators/createOperatorFamily.ts 100% 100% 100% 100%
src/operations/operators/dropOperator.ts 100% 100% 100% 100%
src/operations/operators/dropOperatorClass.ts 100% 100% 100% 100%
src/operations/operators/dropOperatorFamily.ts 100% 100% 100% 100%
src/operations/operators/index.ts 100% 100% 100% 100%
src/operations/operators/removeFromOperatorFamily.ts 100% 100% 100% 100%
src/operations/operators/renameOperatorClass.ts 100% 100% 100% 100%
src/operations/operators/renameOperatorFamily.ts 100% 100% 100% 100%
src/operations/operators/shared.ts 81.81% 77.77% 100% 81.81% 24, 38
src/operations/policies/alterPolicy.ts 100% 100% 100% 100%
src/operations/policies/createPolicy.ts 100% 100% 100% 100%
src/operations/policies/dropPolicy.ts 100% 100% 100% 100%
src/operations/policies/index.ts 100% 100% 100% 100%
src/operations/policies/renamePolicy.ts 100% 100% 100% 100%
src/operations/policies/shared.ts 100% 100% 100% 100%
src/operations/roles/alterRole.ts 100% 100% 100% 100%
src/operations/roles/createRole.ts 100% 92.3% 100% 100%
src/operations/roles/dropRole.ts 100% 100% 100% 100%
src/operations/roles/index.ts 100% 100% 100% 100%
src/operations/roles/renameRole.ts 100% 100% 100% 100%
src/operations/roles/shared.ts 100% 86.66% 100% 100%
src/operations/schemas/createSchema.ts 100% 100% 100% 100%
src/operations/schemas/dropSchema.ts 100% 100% 100% 100%
src/operations/schemas/index.ts 100% 100% 100% 100%
src/operations/schemas/renameSchema.ts 100% 100% 100% 100%
src/operations/sequences/alterSequence.ts 90% 83.33% 100% 90% 23
src/operations/sequences/createSequence.ts 100% 100% 100% 100%
src/operations/sequences/dropSequence.ts 100% 100% 100% 100%
src/operations/sequences/index.ts 100% 100% 100% 100%
src/operations/sequences/renameSequence.ts 100% 100% 100% 100%
src/operations/sequences/shared.ts 81.48% 83.33% 100% 81.48% 41, 43, 49, 63, 69
src/operations/tables/addColumns.ts 100% 85.71% 100% 100%
src/operations/tables/addConstraint.ts 100% 100% 100% 100%
src/operations/tables/alterColumn.ts 91.17% 81.25% 100% 91.17% 69, 76-83
src/operations/tables/alterTable.ts 100% 100% 100% 100%
src/operations/tables/createTable.ts 88% 82.14% 100% 88% 51-54, 92
src/operations/tables/dropColumns.ts 100% 100% 100% 100%
src/operations/tables/dropConstraint.ts 100% 100% 100% 100%
src/operations/tables/dropTable.ts 100% 100% 100% 100%
src/operations/tables/index.ts 100% 100% 100% 100%
src/operations/tables/renameColumn.ts 100% 100% 100% 100%
src/operations/tables/renameConstraint.ts 100% 100% 100% 100%
src/operations/tables/renameTable.ts 100% 100% 100% 100%
src/operations/tables/shared.ts 86.91% 77.66% 76.47% 86.91% 159, 242, 262, 288, 292-298, 302, 450-471
src/operations/triggers/createTrigger.ts 88.23% 71.79% 100% 88.23% 53, 66, 70, 74-76
src/operations/triggers/dropTrigger.ts 100% 100% 100% 100%
src/operations/triggers/index.ts 100% 100% 100% 100%
src/operations/triggers/renameTrigger.ts 100% 100% 100% 100%
src/operations/triggers/shared.ts 100% 100% 100% 100%
src/operations/types/addTypeAttribute.ts 100% 100% 100% 100%
src/operations/types/addTypeValue.ts 100% 100% 100% 100%
src/operations/types/createType.ts 100% 100% 100% 100%
src/operations/types/dropType.ts 100% 100% 100% 100%
src/operations/types/dropTypeAttribute.ts 100% 100% 100% 100%
src/operations/types/index.ts 100% 100% 100% 100%
src/operations/types/renameType.ts 100% 100% 100% 100%
src/operations/types/renameTypeAttribute.ts 100% 100% 100% 100%
src/operations/types/renameTypeValue.ts 100% 100% 100% 100%
src/operations/types/setTypeAttribute.ts 100% 100% 100% 100%
src/operations/views/alterView.ts 100% 100% 100% 100%
src/operations/views/alterViewColumn.ts 100% 100% 100% 100%
src/operations/views/createView.ts 100% 100% 100% 100%
src/operations/views/dropView.ts 100% 100% 100% 100%
src/operations/views/index.ts 100% 100% 100% 100%
src/operations/views/renameView.ts 100% 100% 100% 100%
src/operations/views/shared.ts 100% 50% 100% 100%
src/utils/PgLiteral.ts 100% 100% 100% 100%
src/utils/comparators.ts 100% 100% 100% 100%
src/utils/createSchemalize.ts 96.42% 93.54% 100% 96.29% 84
src/utils/createTransformer.ts 100% 100% 100% 100%
src/utils/decamelize.ts 100% 100% 100% 100%
src/utils/escapeValue.ts 100% 100% 100% 100%
src/utils/fileNameUtils.ts 100% 100% 100% 100%
src/utils/formatLines.ts 100% 100% 100% 100%
src/utils/formatParams.ts 100% 87.5% 100% 100%
src/utils/formatPartitionColumns.ts 100% 100% 100% 100%
src/utils/getMigrationTableSchema.ts 100% 100% 100% 100%
src/utils/getSchemas.ts 100% 100% 100% 100%
src/utils/identity.ts 100% 100% 100% 100%
src/utils/index.ts 100% 100% 100% 100%
src/utils/intersection.ts 100% 100% 100% 100%
src/utils/makeComment.ts 100% 100% 100% 100%
src/utils/quote.ts 100% 100% 100% 100%
src/utils/stringIdGenerator.ts 100% 100% 100% 100%
src/utils/toArray.ts 100% 100% 100% 100%
src/utils/types.ts 100% 100% 100% 100%
Generated in workflow #3509 for commit cd59918 by the Vitest Coverage Report Action

@Shinigami92 Shinigami92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs test/migrations test

@Shinigami92 Shinigami92 added c: feature Request for new feature needs test More tests are needed labels May 22, 2026
@beterrabaA

Copy link
Copy Markdown
Contributor Author

Needs test/migrations test

I'm working on it.I didn't see

@beterrabaA

Copy link
Copy Markdown
Contributor Author

I'm still working onit.I'm traveling to another region of my country.I'm not full operational at the moment.As soon as possible will be back.Few days to be more precisely

@Shinigami92

Copy link
Copy Markdown
Collaborator

I think I figured it out. My brain is boiling.Too much screen time.I'll take a break.

a-close-up-of-a-person-standing-in-front-of-a-fire

@Shinigami92

Copy link
Copy Markdown
Collaborator

@beterrabaA could you please come back and work further on this PR? I would like to merge it and plan the v9 stable release :3

@beterrabaA

beterrabaA commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

Ok. Maybe it's better keep this method simple. Alter Index has too many variations. It's ok to do this? @Shinigami92 . I can't make tests for table spaces

@Shinigami92

Copy link
Copy Markdown
Collaborator

Ok. Maybe it's better keep this method simple. Alter Index has too many variations. It's ok to do this? @Shinigami92 . I can't make tests for table spaces

sure I can live with a first version and we can iterate incrementally

@beterrabaA

Copy link
Copy Markdown
Contributor Author

@Shinigami92 check it out. I fixed the renameIndex and removed the alterIndex method.I just don't know how to use reverse in migration tests.There's no examples using on down. If u gimme the answer I'll use it.

test/migrations/098_rename_index.js

Captura de tela de 2026-07-04 16-23-47

@Shinigami92 Shinigami92 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shinigami92 check it out. I fixed the renameIndex and removed the alterIndex method.I just don't know how to use reverse in migration tests.There's no examples using on down. If u gimme the answer I'll use it.

test/migrations/098_rename_index.js

Captura de tela de 2026-07-04 16-23-47

Hey @beterrabaA, thanks for the update! 🙌

You don't need to call reverse manually — and you actually shouldn't write a down at all here. When a migration exports only up and every operation in it is reversible, node-pg-migrate auto-generates the down for you by calling each operation's .reverse in reverse order.

Both operations you use are reversible:

  • createIndex.reversedropIndex
  • renameIndex.reverse → the one you just added 🎉

There are already examples of this pattern in the test suite that omit down entirely, e.g. test/migrations/006_table_rename.js:

export const up = (pgm) => {
  pgm.renameTable('t2', 't2r');
};

So test/migrations/098_rename_index.js can be simplified to just:

export const up = (pgm) => {
  pgm.createIndex('t1', ['nmbr'], { name: 'idxfoo' });
  pgm.renameIndex('idxfoo', 'quxfoo');
};

The auto-generated down will then run the reverses in reverse order:

ALTER INDEX "quxfoo" RENAME TO "idxfoo";
DROP INDEX "idxfoo";

This is also better than the current hand-written down: right now down only renames quxfoo back to idxfoo and leaves the created index behind, so it isn't a true inverse of up. The auto-reverse drops it too.

One last thing: after this change the integration snapshots need refreshing (the migrations-down.*.stdout.log files), since the down output now includes the DROP INDEX. You can update them with the test runner's update flag.

Comment thread docs/src/migrations/indexes.md Outdated
Comment thread src/operations/generalTypes.ts Outdated
Comment thread src/operations/indexes/renameIndex.ts Outdated
Comment thread docs/src/migrations/indexes.md Outdated
Comment thread test/migrations/098_rename_index.js Outdated
Comment thread src/migrationBuilder.ts
@beterrabaA

Copy link
Copy Markdown
Contributor Author

I'm preparing some food to eat.After it I'll make the changes

@beterrabaA

Copy link
Copy Markdown
Contributor Author

@Shinigami92 I did it

@beterrabaA
beterrabaA marked this pull request as ready for review July 5, 2026 00:43
@Shinigami92
Shinigami92 added this pull request to the merge queue Jul 5, 2026
Merged via the queue into salsita:main with commit 45735d2 Jul 5, 2026
28 checks passed
@beterrabaA
beterrabaA deleted the beterraba-add-alter-index branch July 5, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c: feature Request for new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support ALTER INDEX expressions (or the same thing in ALTER TABLE)

3 participants