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

Timestamp should be in filename for migration:create command #8794

Closed
cabutter opened this issue Mar 24, 2022 · 4 comments · Fixed by #8802
Closed

Timestamp should be in filename for migration:create command #8794

cabutter opened this issue Mar 24, 2022 · 4 comments · Fixed by #8802

Comments

@cabutter
Copy link

cabutter commented Mar 24, 2022

Issue Description

The timestamp is not appended to the filename when using typeorm migration:create <path>

Expected Behavior

When the script is ran

typeorm migration:create ./src/migrations/test

Output a file ./src/migrations/123343242-test.ts

Actual Behavior

When the script is ran

typeorm migration:create ./src/migrations/test

File output is: ./src/migrations/test.ts

Steps to Reproduce

Run the migration:create command from anywhere typeorm is installed

typeorm migration:create ./test

My Environment

Dependency Version
Operating System
Node.js version 16.14.2
Typescript version 4.3.5
TypeORM version 0.3.3

Additional Context

I think the solution is fairly simple - but I also don't understand what the timestamp argument is doing.

-t, --timestamp  Custom timestamp for the migration name
[number] [default: false]

I have tried running commands with argument

npx typeorm migration:create ./src/migrations/test -t true

and the output is not changed.

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

I posted an updated snippet below, this is working locally. I'm not too familiar with writing/running tests to propose a PR
MigrationCreateCommand.ts change the handler function

async handler(args: yargs.Arguments) {
        try {
            const timestamp = CommandUtils.getTimestamp(args.timestamp)
            const extension = args.outputJs ? ".js" : ".ts"
            const fullPath = (args.path as string).startsWith("/")
                ? (args.path as string)
                : path.resolve(process.cwd(), args.path as string)
            const filename = timestamp + "-" + path.basename(fullPath) + extension

            const fileContent = args.outputJs
                ? MigrationCreateCommand.getJavascriptTemplate(
                      path.dirname(fullPath) + "/" + filename,
                      timestamp,
                  )
                : MigrationCreateCommand.getTemplate(filename, timestamp)

            await CommandUtils.createFile(
                filename,
                fileContent,
            )
            console.log(
                `Migration ${chalk.blue(
                    filename,
                )} has been generated successfully.`,
            )
        } catch (err) {
            PlatformTools.logCmdErr("Error during migration creation:", err)
            process.exit(1)
        }
    }
@cduff
Copy link
Contributor

cduff commented Mar 24, 2022

I've encountered this too. migration:generate prefixes the timestamp as expected but migration:create does not.

@michalorman
Copy link

Same for me.

In addition documentation under https://typeorm.io/migrations states to create a migration via: typeorm migration:create -n PostRefactoring but the -n parameter is not supported anymore.

I was also confused with the -t parameter and tried -t true (since in the CLI help default value for -t is false) but apparently it's a number. Thing is that specifying -t 12345 doesn't include timestamp in a filename as well (it's only included in class name).

@cabutter
Copy link
Author

After looking at it again, I realize the -t argument is in fact supposed to be a number. You could put anything yourself or generate a timestamp a different way.

The snippet I proposed above appears to be working locally for me. I just compared the two commands files in the source

MigrationCreate
vs
MigrationGenerate (does properly append the timestamp to the filename)

You can see the handler functions are different, specially the filename variable which gets used in createFile.

@AashishKJha
Copy link

I am getting error

Invalid file path given: "/Users/aj/Desktop/projects/demo/server/datasource.ts". File must contain a TypeScript / JavaScript code and export a DataSource instance.
at Function.loadDataSource (/Users/aj/.nvm/versions/node/v16.14.0/lib/node_modules/typeorm/commands/CommandUtils.js:22:19)
at async Object.handler (/Users/aj/.nvm/versions/node/v16.14.0/lib/node_modules/typeorm/commands/MigrationGenerateCommand.js:68:26)

My DataSource file look like this

export const AppDataSource = new DataSource({
    type: 'cockroachdb',
    host: database.host,
    port: database.port,
    username: database.user,
    password: database.password,
    database: database.name,
    migrationsTableName: 'migrations_typeorm',
    entities: [
      UsersEntity
    ],
    migrationsRun : true,
    migrations: ['server/migrations/*{.ts,.js}'],
    logging : true,
})

can anyone help me out with this ??

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants