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
0.3.0 generate migrations path #8762
Comments
You need to specify full path now. |
-d option is not working either |
There is no -d option |
You should specify the file where you exported your data source object with the typeorm migration:generate -d <path_to_data_source>/datasource.ts <path_to_migrations_folder>/MigrationName.ts |
would really appreciate if someone submit a PR with updates in the documentation. |
what the hell? it's exhausting and not convenient at all. How to configure path once? you should've thought about it. And please update documentation, it's confusing. |
Ok, if someone will be here with that question I created two scripts for package.json
so you don't have to write all that stuff everytime |
@pleerock this breaking change is not a great direction. It completely breaks our workflows without a good workaround. We use How can we work around this? |
@pleerock absolutely ridiculous change |
@jkalberer -- just stumbled on this thread after scraping against version 3 breaking changes. We do the same as you with .env files and a ConfigService wrapper for dotenv. I don't have a solution for the full path when generating migrations, but we are able to generate a datasource that re-uses the typeorm options that the app uses. I added a file with the following in it: src/libs/typeorm/datasource.ts // ... (construct configService and logger first...)
const datasource = new DataSource(getDataSourceOptions(configService, logger, '/services/'));
/**
* This is for typeorm CLI so it can be passed -d [.../datasource.js] (see package.json)
*/
export default datasource; ...then the options are handled like this... src/libs/typeorm/typeorm-options.ts export function getDataSourceOptions(
configService: ConfigService,
logger: LoggerService,
entitiesPath: string
): DataSourceOptions {
return {
type: configService.getString('TYPEORM_CONNECTION') as 'postgres',
host: configService.getString('TYPEORM_HOST'),
port: configService.getNumber('TYPEORM_PORT'),
database: configService.getString('TYPEORM_DATABASE'),
username: configService.getString('TYPEORM_USERNAME'),
password: configService.getString('TYPEORM_PASSWORD'),
entities: [__dirname + '/../..' + entitiesPath + configService.getString('TYPEORM_ENTITIES_APP')],
synchronize: configService.getBoolean('TYPEORM_SYNCHRONIZE'),
logger: new TypeOrmLogger(configService, logger),
extra: {
max: configService.getNumber('TYPEORM_POOL_SIZE_MAX')
},
connectTimeoutMS: configService.getNumber('TYPEORM_CONNECT_TIMEOUT_MS'),
logNotifications: configService.getBoolean('TYPEORM_LOG_NOTIFICATIONS'),
migrations: [__dirname + '/../../../' + configService.getString('TYPEORM_MIGRATIONS')],
poolErrorHandler
};
}
// following function is for NestJS bootstrapping...
export function getTypeOrmModuleOptionsFactory(
configService: ConfigService,
logger: LoggerService,
entitiesPath: string
): (...args: any[]) => Promise<TypeOrmModuleOptions> {
return async () => getDataSourceOptions(configService, logger, entitiesPath);
} package.json "typeorm:gen": "run-s build:clean build:dist build:pkg \"typeorm migration:run\" \"typeorm migration:generate -- {1} {2}\" --",
"typeorm:migrate": "run-s \"typeorm migration:run\"",
"typeorm:revert": "run-s \"typeorm migration:revert\"",
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d dist/libs/typeorm/datasource.js", The CLI commands in package.json get passed the datasource, and the NestJS app(s) uses the factory function. All the actual config values are handled in .env files and our ConfigService wrapper. |
Why? This makes everything much more cumbersome. |
@mlandisbqs - yeah, that's the pattern I settled on. It's definitely a lot less convenient
@DanielMenke who are you asking? |
@jkalberer nobody specific. Just wanted to know the intentions behind this invasive change. |
@DanielMenke -- not sure if this is what you are asking... but my theory as to why the path is now required on the CLI is that the configuration for finding migrations takes an array so it can't be used to generate migrations without knowing which entry to use. @pleerock could maybe confirm this. |
Is it a good option to continue in version 0.2.x? |
@mlandisbqs Can you provide a mini-repo about setting up? |
Example setup here, if it can help someone stuck in there.. |
@pleerock Why did you change the workflow without notifying anyone ? This breaking change make everything more complex without any explained reason, it's just non-sense |
It made me entire 3 day to get to this issue post, I was completely lost it. Thanks, because everyone seem have the same problem |
Posting my Solution to help anyone who wondered how to :
I have put my datasource inside the
If I need to generate a Migration I simple just run Some notes because I am using an nx workspace with NestJS
Hope this helps anyone with the same use-case |
Hello, in my application i have two databases and i used many times /* eslint-disable no-console */
import { exec } from 'child_process';
import minimist from 'minimist'; // eslint-disable-line import/no-extraneous-dependencies
const argv = minimist(process.argv.slice(2));
const option = argv._[0];
const config = argv.c === 'company' ? 'base' : 'general';
const dataSource = argv.d || './src/shared/infra/typeorm/dataSource.ts';
const name = argv.n || 'undefined';
if (!option) {
console.log(
'Please, choose an option: \n - migration:create\n - migration:run\n - migration:revert\n - migration:show',
);
process.exit(0);
}
if (option === 'migration:create') {
exec(
`yarn typeorm-ts-node-esm migration:create ./src/shared/infra/typeorm/migrations/${config}/${name}`,
(_err, stdout, stderr) => {
console.log(`${stdout}`);
console.log(`${stderr}`);
},
);
} else {
exec(
`yarn typeorm-ts-node-esm ${option} --dataSource=${dataSource}`,
(_err, stdout, stderr) => {
console.log(`${stdout}`);
console.log(`${stderr}`);
},
);
} And in
Finally, to use
|
Thanks for sharing, but the fact that we have to implement a CLI on our end seem a little bit shaky |
Is this still the only solution at the moment? And does typeorm support typescript files for the cli? I heard that it currently only supports js files. |
Why this kind of nonsense and breaking changes? |
You guys have a grate tool. TypeORM is really nice. But this kind of thing is not great. It doesn't seem really finished. Following the documentation this kind of error is happening all time. Thanks for support. |
hello, guys! Are you going to fix this issues? |
@g1coder Taken from part of my medium article: https://sadhakbj.medium.com/lets-create-fully-dockerized-nodejs-backend-application-with-express-js-typescript-typeorm-and-1f7396623301 We want to create migrations using the CLI, but the latest version of typeorm has some breaking changes, so we need to take advantage of the package: yargs . Create a folder called scripts on the root directory and create a file: migration-create.js with following contents: #!/usr/bin/env node
const yargs = require("yargs");
const { execSync } = require("child_process");
// Parse the command-line arguments
const {
_: [name],
path,
} = yargs.argv;
// Construct the migration path
const migrationPath = `src/database/migrations/${name}`;
// Run the typeorm command
execSync(`typeorm migration:create ${migrationPath}`, { stdio: "inherit" }); Now let’s update our package.json to include the scripts to create new migration, show the list of the migrations and revert it: "scripts": {
"build": "tsc",
"dev": "nodemon --watch src --exec 'ts-node' src/index.ts",
"start": "node dist/index.js",
"typeorm": "typeorm-ts-node-commonjs -d src/database/data-source.ts",
"migration:show": "yarn typeorm migration:show",
"migration:create": "node scripts/migration-create.js",
"migration:revert": "yarn typeorm migration:revert"
} With this we can run the command: yarn migration:create CreateAuthorsTable |
Thank you so mush. It is just working. Magic |
It works! 😄 |
Just discovered that when you specify name for migration you can also specify full path: this script will generate migration in ./migrations folder ormconfig.ts is adatasource configuration |
Issue Description
Expected Behavior
When running
migration:generate
, I expect the migration generated goes into themigrations
path set in the parameter of theDataSource
constructor.Basically, I expect it to generate a migration into the path:
E:\Projects\Discord Bots\commissions\luigical\src\migrations\<the_migration_file_here>.ts
Actual Behavior
Generating and creating migrations works but it goes into project root folder.
Steps to Reproduce
project\src\entities\table-name.entity.ts
migration:generate
command - in my case the scripts I used are:My Environment
Additional Context
Relevant Database Driver(s)
aurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: