Reputation: 2480
I know typeorm migration:run
to run all migration files, but some time I want run a file like insert
file, or a modified file. But i can't find any doc for that. How to do that thanks.
Upvotes: 8
Views: 27098
Reputation: 1
You can run a specific migration that way:
typeorm migration:run --name YourMigrationName
Example:
typeorm migration:run --name 1675397201995-AddNewColumn
Upvotes: 0
Reputation: 125
One can write custom code to run one migration like inserting a record or just creating/ deleting a single table kind of things. typeorm provides "QueryRunner" which basically run queries in the background. So just create a file with the code given following code to write custom single migration
Upvotes: -2
Reputation: 125
I dont think we can run any migration particular with cli, but we can run it through query runner. please refer the following code:
const connection: Connection = getConnection();
const queryRunner: QueryRunner = connection.createQueryRunner();
//createDatabase16413343434 is class/migration generated from cli
const createDatabase = new createDatabase16413343434();
await createDatabase.up(queryRunner);
Upvotes: 0
Reputation: 2540
For running a specific migration my solution is in ormconfig.js
add:
module.exports = {
...
migrations: [process.env.DB_MIGRATION_DIR || 'src/database/migrations/*.ts'],
...
}
and now use DB_MIGRATION_DIR=path/to/migration.ts npm run typeorm migration:run
run your specific migration
Another tip for force re-running a migration, but might need some customization for connection string variables - add in your package.json:
{
"scripts": {
...
"migration:rerun": "psql postgresql://$DB_USER@localhost:5432/$DB_NAME -c "delete from migrations where name like '%$SEED_NAME%';" && DB_MIGRATION_DIR='$DB_MIGRATION_DIR' npm run typeorm migration:run"
...
}
...
}
Upvotes: 1
Reputation: 4444
If you want to seed the database with data you shouldn't use migrations. Migrations' purpose is to create the database's structure, This is why migrations are executed serially, therefore running a single migration non-serially misses the point.
For database seeding with typeorm you can use typeorm cli query, you can create a script that read the SQL from a file.
For example in a node.js app & bash:
package.json:
"scripts": {
...,
"typeorm": "npx ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli --config ormconfig-migrations.ts",
"db:seed": "bash scripts/db-seed.sh"
}
db-seed.sh:
#!/bin/bash
query=""
for filename in sql/seed/*.seed.sql; do
query=${query}"$(< ${filename})";
done
npm run typeorm query "${query}";
Pay attention there is a bug in this solution, there is always an error thrown from the logger typeorm uses - ignore it. it doesn't relate to the db operation, and does not represent the success of the process.
A migration file that was shared with others shouldn't be modified, in this case, create a new migration.
If that file wasn't shared and is in development, you can run migration:revert
as many as you need, then modify and run migration:run
again.
Upvotes: 4
Reputation: 396
This is not possible. You can see the docs of the migration with
typeorm migration:run -h
During development you can locally change your connection settings, e.g. change the path to the migration files in your ormconfig.json to match only your file.
"migrations": [
"src/db/migration/**/yourmigration.ts"
]
Upvotes: 2