-
-
Notifications
You must be signed in to change notification settings - Fork 526
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
Allow both ts and js files to run migrations #715
Conversation
Just ran into the Will this change also work for the sequelize db:seed:all --config=./config/sequelize-config.ts |
Also look likes this would resolve #693, if merged. Hopefully this would be accepted soon. |
@orta did you create a fork of this that can be used until this PR is merged? Really could use this. This is a very tiny change that is fully backwards-compatible so not sure why it's been sitting for so long. |
Afraid not, I used patch-package which I use for any trivial PR change, it's very easy to use |
This is a temp solution. |
Hi I'd like to ask when the PR will be accept ? It is quite a while :/. I tried this fix and it look that is working very well |
Hmm, why this PR still not merged? |
Hi, us maintainers have been focusing on the main repo recently, but I intend to take a good look on the CLI in the near future. Thanks everyone for the patience! |
One possiblity is to allow the user to add his own check for js files or globbing expression. A related issue would be the ability to suppress the 'does not match pattern: /.js$/ We have a large project and wish to use subdirectories like /migrations/data and seed/data but it results in a lot of distracting messages. |
I forked this repository and tried this solution! And works! I just added the script on package:
import { QueryInterface, DataTypes, literal } from 'sequelize'
export function up(queryInterface: QueryInterface): Promise<void> {
return queryInterface.createTable('Estados', {
id: {
allowNull: false,
autoIncrement: true,
type: DataTypes.INTEGER,
comment: 'Identificador da tabela Estados',
primaryKey: true,
},
nome: {
allowNull: false,
type: DataTypes.STRING(20),
comment: 'Nome do estado',
},
sigla: {
allowNull: false,
type: DataTypes.STRING(2),
comment: 'Sigla do Estado',
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
comment: 'Data da criação do registro',
defaultValue: literal('NOW()'),
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
comment: 'Data da última alteração do registro',
defaultValue: literal('NOW()'),
},
deletedAt: {
allowNull: true,
type: DataTypes.DATE,
comment: 'Data da exclusão do registro',
},
})
}
export function down(queryInterface: QueryInterface): Promise<void> {
return queryInterface.dropTable('Estados')
} run with: |
@papb can this be merged? |
All checks passed, no conflicts why not merge? |
Looking at the main contributors |
The problem I would have with this commit is that it allows the following files to be considered different migrations:
To me those are the same migration, just one was compiled from the other. Compiling the TS into the same folder is indeed very bad form, but it still is a logical problem. Also I'd personally, though I'm by no means a maintainer of this project, that a PR exist for adding a flag that allows the user to specify the matching pattern or file extension with the default left as it stands in the project currently. That way the user can override but the default is left unaffected. Lastly, consider the principle of least surprise: which is worse, finding that after you upgraded your tools that all of a sudden both the TS and JS migrations files are being executed and you are being hit with failures left and right, or that an option now exists to change the pattern so that your TS files are run? I'd argue the latter is the least surprising and thus better. |
I have been able to work around this using a postinstall script to run this snippet: const { readFileSync, writeFileSync } = require('fs')
const { join } = require('path')
// Fix sequelize-cli doesnt support typescript extensions
// Will be run in the postinstall script in package.json
const p = join(__dirname, '../node_modules/sequelize-cli/lib/core/migrator.js')
const f = readFileSync(p, 'utf8').replace('.js$/', '.(j|t)sx?$/')
writeFileSync(p, f, 'utf8') |
@namnm |
push bump +1 :) |
So the best option would be a config setting in |
I switched my project over to Umzug, which is the sequelize library that this project uses to do migrations. Gave me a lot more fine grained control over the process and allowed for my application to run migrations on startup. |
TS migration blues led me to the same decision earlier today. In case anyone else comes looking for a way to get a ts-node Mocha integration test to load their TS migrations despite those migrations having no JS counterparts during testing because ts-node does not output files and it's all buried in a container - the simplest approach with umzug uses an arbitrary pattern parameter, |
@kf6kjg This is an option but AFAIK I don't think umzug does seeding out of the box like this library does? You could do some manual work to get umzug to work with seeding as evidence by this comment, but its a brittle workaround with a few caveats. |
@mkay581 in Umzug seeders are just another set of migrations. You are totally in control of how you want to structure all parts of your migration: you could have schema migrations, seeds, data migrations, and any other migration type your project needs just by running Umzug against either different folders, different file name patterns, or both. Of course Umzug isn’t a cli interface, but I really didn’t need a cli interface in my projects. |
Sure, although some people would rather not have to worry about such granular level of detail, which is why people choose to use this higher level library. |
If you would ask me what would I do different if I would start a typescript project again is that I would use typeorm. Sequelize isn't build for typescript, it's Javascript conversion to typescript |
JavaScript doesnt get converted to TypeScript |
@mkay581 Not automatically, no, only when you start with a plain JS project and later manually move the project to TS, migrations and all, learning all the typing tricks needed to make Sequelize keep working along the way. I think that's the point - that TypeORM is TS-native. |
@sushantdhiman can we please include this in v6? It's a super easy and long awaited change. And |
@sushantdhiman can you please explain why did you close this one? |
PR Cleanup, if anyone needs this feature they should open a new PR & add a test for this. For example #905 |
npx ts-node -T -r tsconfig-paths/register .\node_modules\sequelize-cli\lib\sequelize db:migrate |
Because you can use
ts-node node_modules/.bin/sequelize db:migrate
to trigger your migrations, you can safely use TypeScript using more or less vanilla sequelize-cli. The only blocker I've found so far is the check for js files in the migration file detection.This is great, because you get awesome tooling support
This PR allows a
.ts
file as well as a.js
to pass by default.There's a downside to this in that someone could try running a typescript file without the
ts-node
, but this app is not documented as supporting typescript out of the box, so I don't think people would have that expectation.