Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runner): resolve migration file for esm #1207

Closed
wants to merge 4 commits into from

Conversation

Shinigami92
Copy link
Collaborator

@Shinigami92 Shinigami92 commented Jun 19, 2024

extracted from #1206


Edit: behavior changed to tryout using process cwd

@Shinigami92 Shinigami92 added the c: bug Something isn't working label Jun 19, 2024
@Shinigami92 Shinigami92 added this to the v7.x milestone Jun 19, 2024
@Shinigami92 Shinigami92 self-assigned this Jun 19, 2024
@Shinigami92
Copy link
Collaborator Author

@aprendendofelipe could you do a review for this?

Copy link

github-actions bot commented Jun 19, 2024

Coverage Report

Status Category Percentage Covered / Total
🟢 Lines 93.96% (🎯 90%)
⬇️ -1.48%
6638 / 7064
🟢 Statements 93.96% (🎯 90%)
⬇️ -1.48%
6638 / 7064
🟢 Functions 93.96% (🎯 90%)
⬇️ -1.51%
249 / 265
🟢 Branches 90.05% (🎯 85%)
⬆️ +0.09%
770 / 855
File Coverage
File Stmts % Branch % Funcs % Lines Uncovered Lines
Unchanged Files
src/db.ts 88.09% 88.46% 87.5% 88.09% 68-70, 100, 103-115, 155-157
src/index.ts 100% 100% 100% 100%
src/migration.ts 72.13% 75.67% 61.53% 72.13% 64, 67-69, 71-83, 112-118, 123-159, 216-217, 242-250, 253-254, 265-266, 271-274, 295-297, 303-304
src/migrationBuilder.ts 95.68% 77.77% 66.66% 95.68% 353-361, 497-502, 512-514, 517-519, 526-527
src/runner.ts 49.72% 50% 60% 49.72% 42, 58-68, 80-81, 84-92, 130-131, 154-204, 206-219, 221-230, 238, 240-246, 249, 262, 274-287, 290-293, 301-344, 347-354
src/sqlMigration.ts 90.19% 100% 80% 90.19% 45-49
src/types.ts 100% 100% 100% 100%
src/operations/sql.ts 100% 100% 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% 100% 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 100% 100% 100% 100%
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 98.63% 85.71% 100% 98.63% 71
src/operations/indexes/createIndex.ts 98.03% 95.23% 100% 98.03% 74-75
src/operations/indexes/dropIndex.ts 100% 100% 100% 100%
src/operations/indexes/index.ts 100% 100% 100% 100%
src/operations/indexes/shared.ts 91.17% 86.36% 100% 91.17% 22, 32-35, 47
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% 87.5% 100% 100%
src/operations/operators/addToOperatorFamily.ts 100% 100% 100% 100%
src/operations/operators/createOperator.ts 100% 100% 100% 100%
src/operations/operators/createOperatorClass.ts 100% 83.33% 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 90% 75% 100% 90% 24-25, 37-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% 75% 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 98.97% 76.92% 100% 98.97% 78
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 96.87% 75% 100% 96.87% 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 87.67% 68.75% 100% 87.67% 41, 43-44, 49-50, 63-64, 69-70
src/operations/tables/addColumns.ts 100% 80% 100% 100%
src/operations/tables/addConstraint.ts 100% 100% 100% 100%
src/operations/tables/alterColumn.ts 90.83% 68.75% 100% 90.83% 63-64, 75, 82-89
src/operations/tables/alterTable.ts 100% 100% 100% 100%
src/operations/tables/createTable.ts 90.47% 60% 100% 90.47% 44-48, 65, 75-76
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 87.36% 76.56% 80% 87.36% 137-138, 141-142, 195-199, 224, 228-229, 236-237, 248-249, 274-275, 278-285, 288-289, 334-336, 426-451
src/operations/triggers/createTrigger.ts 90.83% 68.18% 100% 90.83% 53-54, 66-67, 70-71, 74-77, 113
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% 66.66% 100% 100%
src/utils/PgLiteral.ts 96.49% 100% 75% 96.49% 37-38
src/utils/StringIdGenerator.ts 100% 100% 100% 100%
src/utils/createSchemalize.ts 100% 100% 100% 100%
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/formatLines.ts 100% 100% 100% 100%
src/utils/formatParams.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/toArray.ts 100% 100% 100% 100%
src/utils/types.ts 100% 100% 100% 100%
Generated in workflow #890

@Shinigami92 Shinigami92 force-pushed the fix-esm-resolve-migration-file-in-runner branch 3 times, most recently from 0721729 to 9a3a8f8 Compare June 19, 2024 21:14
@Shinigami92 Shinigami92 marked this pull request as draft June 19, 2024 21:14
@Shinigami92 Shinigami92 force-pushed the fix-esm-resolve-migration-file-in-runner branch 4 times, most recently from cfdb4da to 5bc0b7b Compare June 19, 2024 21:36
@Shinigami92
Copy link
Collaborator Author

Okay... I need more time to find out what's going on 😕 but ran out of time today

src/runner.ts Outdated
const relativeFilePath = relative(directory, filePath);

return (
globalThis.require?.(relativeFilePath) ?? (await import(relativeFilePath))
Copy link
Contributor

Choose a reason for hiding this comment

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

import will not work with .ts at runtime.

What do you think about #1206:

        const filePath = resolve(options.dir, file);
        const actions: MigrationBuilderActions =
          extname(filePath) === '.sql'
            ? await migrateSqlFile(filePath)
            : createRequire(resolve('noop.js'))(filePath);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

First, pls read all my comments in your other PR 🙂

But regarding .ts files, I was shocked a bit to discover what's really going on here 🤔 (I just took over maintenance and I'm not the original creator)
When we will go full esm only in v9, I thought that we will import the migration file and consume it as a real js file. But now there is potential also .ts files at runtime? 👀
Either we could try to compile them at runtime, as we are aware that typescript is installed as peer dependency anyway (cause the user is already using .ts files) or ... I dont know 🤔 I'm not so deep into that right now, I need to build up some knowledge...

@Shinigami92
Copy link
Collaborator Author

FFR: globalThis.require and globalThis.__dirname is NOT WORKING. It will always result in undefined.

Co-authored-by: Felipe Barso <aprendendofelipe@users.noreply.github.com>
@Shinigami92
Copy link
Collaborator Author

@aprendendofelipe assuming that noop.js and cwd() is the same, I did especially not touch the test files and now the resolving of these are not working 😬
This is why I would also like to not touch the test files together while solving the ESM fix

Error: Can't get migration files: Error: Cannot find module 'test/migrations/001_noop.js'

Can we find a solution that works, so that the tests are not failing (without touching them obviously)

@Shinigami92 Shinigami92 deleted the fix-esm-resolve-migration-file-in-runner branch June 21, 2024 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants