typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
34.51k stars 6.34k forks source link

Migration runs: Duplicate Migration Create #8099

Open libterty opened 3 years ago

libterty commented 3 years ago

Issue Description

When I try to run migration the orm didn't detect that the same migration has already been generated and executed before.

Expected Behavior

Expect orm could detect same migration for same database under different role

Actual Behavior

Orm didn't detect different user role with same migration, like following example entity. If i have user Postgres and Test in Test database. And project one use Postgres to create migration while project two use Test to create migration. One of the migration would fails due to migration is already been executed. Later on I found out I have to execute migration with same user so that migration script will not meet duplicate create. I'm not sure if this is just not documented or it's a bug.

@Entity()
export class Example {
   @PrimaryGeneratedColumn('uuid')
   id: string

   @Column({ type: 'varchar', length: 255, nullable: false })
   @Index({ where: '"deletedAt" IS NULL' })
   name: string

   @VersionColumn({ type: 'integer', nullable: false })
   version: number

   /**
    * @description Time area
    */
   @CreateDateColumn({ type: 'timestamptz', nullable: false })
   createdAt: moment.Moment

   @UpdateDateColumn({ type: 'timestamptz', nullable: false })
   updatedAt: moment.Moment

   @DeleteDateColumn({ type: 'timestamptz', nullable: true })
   deletedAt?: moment.Moment
}

Steps to Reproduce

My Environment

Dependency Version
Operating System Debian
Node.js version 12.11.1
Typescript version 3.7.4
TypeORM version 0.2.34

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg 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?

lflfm commented 3 years ago

I don't use Postgres but from what I'm reading Postgres behaves a lot like Oracle so I think your problem is the schema. The two different users are likely using their own schemas when connecting instead of a shared or the public schema. The TypeORM documentation doesn't go into each database's structural functionality and it shouldn't unless directly impacting the code that we need to write - and schemas don't impact our code, just how we connect, so I wouldn't say the documentation is incomplete.

But then coming back to the point: are you sure both users are using the same schema?

libterty commented 3 years ago

@lflfm

Yes both users using the same schema, the configuration is like. So that's why i'm confusing about why same schema would retrigger migration when it use different db user

const masterConfig:  Partial<ConnectionOptions> = {
  host: config.TYPEORM.MASTER_HOST,
  port: config.TYPEORM.MASTER_PORT,
  username: config.TYPEORM.MASTER_USERNAME,
  password: config.TYPEORM.MASTER_PASSWORD,
  database: config.TYPEORM.MASTER_DB
}

export const ormConfig: ConnectionOptions = {
  ...
  type: config.TYPEORM.MASTER_CONNECTION,
  host: config.TYPEORM.MASTER_HOST,
  port: config.TYPEORM.MASTER_PORT,
  username: config.TYPEORM.MASTER_USERNAME,
  password: config.TYPEORM.MASTER_PASSWORD,
  database: config.TYPEORM.MASTER_DB,
  schema: config.TYPEORM.MASTER_SCHEMA
  ...
  replication: {
    master: masterConfig,
    slaves: [...]
  }
}
romadomma commented 7 months ago

Hi, I've been having this problem. It occurred because migrations dir contained the index.ts file that exported the migrations classes. The migrations loader loads the classes from each file, and then loads again from the index file.

I hope this helps.