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

postgres: typeorm_metadata shouldn't depends to database/schema name #9776

Open
1 of 18 tasks
rinsuki opened this issue Feb 11, 2023 · 4 comments
Open
1 of 18 tasks

postgres: typeorm_metadata shouldn't depends to database/schema name #9776

rinsuki opened this issue Feb 11, 2023 · 4 comments

Comments

@rinsuki
Copy link
Contributor

rinsuki commented Feb 11, 2023

Issue description

currently TypeORM puts database/schema name when migration:generate, and expects to match when comparing schema with current DB status, but these are depends to environment (e.g. some developer uses app_dev to DB name, but another developer may uses app_development or generated name by SaaS user_12345_app_development), so we shouldn't depends to it.

Expected Behavior

even migration:run and migration:generate runs on another DB name, it shouldn't make any new migration file.

Actual Behavior

If migration:generate runs on another DB name, it will make new migration file.

Steps to reproduce

  1. Create new project
  2. Add some fields that needs typeorm_metadata table (e.g. generated columns) and run typeorm migration:generate to make migration.
  3. Create & Set to use new database with another name, and run typeorm migration:run.
  4. run typeorm migration:generate again.

My Environment

Dependency Version
Operating System macOS 13.2
Node.js version 19.2.0
Typescript version 4.9.4
TypeORM version 0.3.11
PostgreSQL 14.6 (Homebrew)

Additional Context

I'm not sure about my thoughts (=typeorm_metadata shouldn't depends to database/schema name) is correct.

If my thoughts is correct, please tell me. then I will start working for this.

related: #4925 (but it is on MariaDB/MySQL)

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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

Yes, I have the time, and I know how to start.

@boronine
Copy link

This issue makes migration:generate impossible to rely on for GENERATED columns. Our workaround is to edit the migration file manually like so:

    if (queryRunner.connection.options.type !== "postgres") {
      throw new Error("invalid database");
    }
    const database = queryRunner.connection.options.database;
    const schema = queryRunner.connection.options.schema ?? "public";
    await queryRunner.query(
      `INSERT INTO "typeorm_metadata"("database", "schema", "table", "type", "name", "value")
       VALUES ($1, $2, $3, $4, $5, $6)`,
      [
        database,
        schema,
        "mytable",
        "GENERATED_COLUMN",
        "mycolumn",
        "...",
      ]
    );

@rinsuki
Copy link
Contributor Author

rinsuki commented May 14, 2023

Sorry I forgot this issue, I will work this after beats Zelda TotK.
alternative workaround: you can UPDATE existing typeorm_metadata everytime before generates migration, It would be like UPDATE typeorm_metadata SET database=current_database()

@boronine
Copy link

My workaround above somehow wasn't good enough and our team started encountering strange behaviour which we have no time to debug.

We just got rid of the generated column in favour of calculating the value dynamically, luckily our use case is not resource-constrained. This issue is a bummer, generated columns would be a really neat checksumming solution for us.

@Philipinho
Copy link

I am experiencing the same issue (postgres).
typeorm migration:generate creates new migration file for already run Generated Column if the database name does not match.
This makes Generated Columns unusable.

I might have to use triggers for my use case.

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

No branches or pull requests

3 participants