-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Generating migrations with cli #8810
Generating migrations with cli #8810
Comments
CLI works fine till version - 0.2.45. Looping in (@pleerock ) |
Had the same issue, the workaround I found for now is to use the compiled js file For example
As the cli command fails to import the .ts file for some reason, probably because of the file url it tries to generate, also for some reason it will only import a ts file if the closest package.json file is set to a type => module File URL -> typeorm/src/util/ImportUtils.ts Line 15 in eacecce
package.json module type check -> typeorm/src/util/ImportUtils.ts Line 32 in eacecce
|
How did 0.3.0 get past the testing phase when the CLI is completely broken? |
+1, pretty unusable atm, will switch off for my newer projects |
I have the same issue |
This is not work
And I have the same issue |
Thanks to @pleerock and this post I now have migration creation/generation working. The CLI now requires the full path to the output file, so if you have an npm script like
then an example call to that script to generate a migration could look something like
@pleerock thank you for that! |
I also got the migrations to work but only by compiling my code and using .js data-source file. I still can't get the migrations to work using the .ts files. I also can't get the entities path in the data-source file to work and have to declare each entity. |
@SleeveShirtholes - may want to try: |
@SleeveShirtholes In my example I'm using all TS. In addition to @kylevillegas93 's reply, my data-source.ts looks like import { DataSource } from 'typeorm';
import { SqlServerConnectionOptions } from 'typeorm/driver/sqlserver/SqlServerConnectionOptions';
// eslint-disable-next-line node/no-missing-import
import { CustomNamingStrategy } from '../src/database/models/naming-strategy';
const options: SqlServerConnectionOptions = {
type: 'mssql',
url: '',
host: 'localhost',
port: 1433,
dropSchema: false,
synchronize: false,
extra: {
trustServerCertificate: true
},
options: {
useUTC: true,
abortTransactionOnError: true, // SET XACT_ABORT ON|OFF
enableArithAbort: true
},
username: '...',
password: '...',
database: '...',
logging: false, // TypeORM true, false, ['query', 'error', 'schema', 'warn', 'info', 'log']
logger: 'advanced-console', // TypeORM 'advanced-console' (default), 'simple-console', 'file'
entities: ['src/database/models/**/*.ts'],
migrations: ['src/database/migrations/**/*.ts'],
subscribers: ['src/database/subscribers/**/*.ts'],
namingStrategy: new CustomNamingStrategy()
};
export default new DataSource(options); Hope that helps in some way... |
So I just tried both suggestions and still can't get migrations to work with the .ts file. Here is my .ts file:
Here is my package.json file:
The error I am getting now is: I was getting this error before: But I think the reason I am getting that error is because I didn't originally have the typeorm script in my package.json. Also, I am working within a docker container so don't know if that has anything to do with it. |
@SleeveShirtholes here's my relevant package.json stuff... typeorm version is different than yours I noticed... {
"dependencies": {
"mssql": "^8.0.2",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.3.4"
},
"devDependencies": {
"@types/node": "^17.0.23",
"@types/tedious": "^4.0.7",
"ts-node": "10.7.0",
"typescript": "^4.6.2"
},
"scripts": {
"typeorm": "typeorm-ts-node-esm -d ./config/data-source.ts"
}
} Note: use |
@msalsbery I am getting this error now when running the following command: Error: Command: Do you know why that is? Sorry to keep bugging you. |
@SleeveShirtholes are you sure all dependencies are updated to latest versions and installed? That looks like it's not seeing ts-node |
@msalsbery Yea thats the weird thing...I have ts-node installed. I also confirmed that ts-node is in my node_modules folder and it is there. Not sure why I am getting this error. |
@SleeveShirtholes hmm, I got nothin ;) If you added the typeorm script to package.json and that script runs typeorm-ts-node-esm then the only difference from what I'm doing is I'm not running in a container |
@msalsbery Ok...it does look like its the container that is the issue. I ran the command outside the container and everything worked fine. Thanks for your help! |
I could not make the run command work Here is my files: ./src/database/data_source.tsimport { DataSource } from "typeorm";
const AppDataSource = new DataSource({
type: process.env.DB_TYPE,
host: process.env.DB_HOST,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: process.env.DB_PORT,
entities: [],
migrations: ["./src/database/migrations/*.ts"],
});
export default AppDataSource;
AppDataSource.initialize()
.then(() => console.log("Data Source has been initialized"))
.catch((error) => console.error("Error initializing Data Source", error)); ./src/database/migrations/1648475278557-CreateCategories.tsimport { MigrationInterface, QueryRunner, Table } from "typeorm";
export class CreateCategories1648475278557 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "categories",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
},
{
name: "name",
type: "varchar",
},
],
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable("categories");
}
} Command I'm using to run the migrationyarn typeorm-ts-node-esm -d ./src/database/data_source.ts migration:run I always got the error:
I tried to convert this two files to JS, but still got the same error. |
For me the following is working:
...
"scripts": {
...
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
} I use a import { DataSource } from 'typeorm';
const devDataSource = new DataSource({
type: 'mariadb',
host: 'localhost',
port: 3306,
username: 'user',
password: 'password',
database: 'database',
entities: ['**/*.entity.js'],
synchronize: false,
migrations: ['src/migrations/*.js'],
});
export default devDataSource; Then I run the migrations with Maybe this works for others too. My problem however is that the |
I'v just tried all examples here with no success still getting and this file is present, tried with .js files, but no success, tried ts-node, typeorm-ts-node-commonjs, typeorm-ts-node-esm and every where got the same error |
I found the problem 🎉 It looks like @MrPostie solution is working, in my case there was problem with my ormconfig.ts, I used old options for package.json
ormconfig.ts import { DataSource } from 'typeorm';
const source = new DataSource({
"type": "sqlite",
"database": "./storage/nest.db",
"entities": ["src/**/*.entity.ts"],
"migrationsTableName": "migrations",
"migrations": ["src/db/migrations/*.ts"],
});
export default source; command
it also works with this package.json
Thanks 💪 |
MrPostie then this command:
works normally IF it is NOT using the .env variables Now we need to figure out how make this file read the .env variables |
Somewhere I read that the cli doesn't parse/read |
Yes, you have to use OK, i had some other issue, some of the commands don't work, for example so my final working code is like this:
import { DataSource } from 'typeorm';
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions';
const source = new DataSource({
"type": "sqlite",
"database": "./storage/nest.db",
"entities": ["src/**/*.entity.ts"],
"migrationsTableName": "migrations",
"migrations": ["src/db/migrations/*.ts"],
});
export default source;
{
"scripts": {
"typeorm": "typeorm-ts-node-commonjs -d ./src/db/ormconfig.ts",
"migration:generate": "npm run typeorm migration:generate",
"migration:show": "npm run typeorm migration:show",
"migration:run": "npm run typeorm migration:run",
"migration:revert": "npm run typeorm migration:revert",
"migration:create": "typeorm-ts-node-commonjs migration:create"
}
} In your terminal use it like this: npm run migration:generate src/db/migrations/init-photos
npm run migration:create src/db/migrations/update-photos-data
npm run migration:show
npm run migration:run
npm run migration:revert |
@myflowpl How does migration:run work without -d option? How does cli find the migration? Do you have an ormconfig file still that it’s picking up using deprecated behavior? I’m just curious, as I wouldn’t think it should work without specifying a DataSource and I’d want to make sure my cli and app runs are always using the same DataSource… edit: Also, I use -d option on all cli commands without issue, so I’m curious why that doesn’t work for you |
@msalsbery note that I did npm script called My goal is to have predefined npm scripts so my team doesn't have to remember any extra options, they just give simple command to generate the migration and run it, and me as a one who knows how configurations works, I prepare the scripts. That interesting it works for you with the |
@myflowpl I reread and noticed it was only the create that didn’t use -d and realized that operation wouldn’t need the database so that’s why it works. Sorry for the confusion and thank you for setting my head right! |
Issue Description
Expected Behavior
Migrations get generated using the cli
Actual Behavior
I am following the documentation outlined here https://typeorm.io/migrations and I feel like the documentation is way different then what is actually happening in the cli. I have tried just running the following command:
typeorm migration:generate -n PostRefactoring
When I run this command I get the following:
Not enough non-option arguments: got 0, need at least 1
So then I modify the command to something like this:
Missing required argument: dataSource
So then from there I add the datasource since that is what it is asking for:
npx typeorm migration:generate test -d app-data-source.ts
(I found the -d somewhere online while investigating this issue)But once I run that command I get the following error:
File must contain a TypeScript / JavaScript code and export a DataSource instance.
I have been trying to get this to work for days and I am about ready to throw the towl in and use a different ORM...which I don't want to do. I know that everything is working fine because I can start up the app and the app connects to the database and creates rows in the database. So it is just something with the cli and trying to generate the migrations.
Here is what is in my app-data-source.ts file:
This is the contents of my app.ts file:
All these files were setup using the instructions outlined in the express typeorm documentation:
Example with Express
Which BTW, this also has issues and doesn't run from what is outlined in the directions. I had to make some modifications to actually make the app run compared to what is outlined in that guide. One of the changes I made in the data source file was putting the actual Entity name in the entity property of the datasource compared to the guide that has you put the path in.
Anyway, am I crazy...am I doing something wrong? You guys might not have enough information to answer the first question but can you at least help with trying to figure out how to make the migrations work?
Steps to Reproduce
Set up you app using the guide outlined above to use express
My Environment
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: