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

Possible problem with migration diff with enums involved. #1142

Closed
tuhlmann opened this issue Nov 29, 2020 · 3 comments
Closed

Possible problem with migration diff with enums involved. #1142

tuhlmann opened this issue Nov 29, 2020 · 3 comments
Assignees

Comments

@tuhlmann
Copy link

Describe the bug
I think I see a bug with schema diffing (or migration) for my app using a Postgresql db:

I created an initial migration (using migrator.createInitialMigration()) from an existing schema (one that was updated through orm.getSchemaGenerator().updateSchema().

  • The migration file was created fine but it was not logged to the migrations table because schemaExists returned false.
  • To narrow down further I dropped all tables and created them by executing migrate up using the initial migration file. This worked fine.
  • I then created a new migration. And this added some lines to the new migration file that it thinks are different between the current schema and the entities. I also ran this file. The next migration created would contain the same lines.
  • The fields in that seem to get processed over and over are relatedType and mailType from queued-mail.entity.ts.

I attach the following files:

  • the initial migration,
  • the subsequent migration
  • the entity and enum definitions in question.
  • the ddl for this table as exported from my database. The table was created with the initial migration.

Let me know if I can debug anything to help sort this out! Thanks!

migration-issue.zip

To Reproduce
Steps to reproduce the behavior:
I'm sorry I don't have a reproduction repo yet. Please let me know if you need one.

Expected behavior

  • Initial migration should have been created and logged
  • Subsequent migration should not contain any model changes.

Additional context
Add any other context about the problem here.

Versions

Dependency Version
node 12.18.3
typescript 4.0.2
mikro-orm 4.3.2
your-driver postgres, using pg from mikro-orm
@tuhlmann
Copy link
Author

tuhlmann commented Nov 29, 2020

I think I found one problem:

I changed the entity definition for the 2 enums from

  @Enum({ type: "MailType", items: () => MailType, default: MailType.Unknown })
  mailType: MailType // FIXME: set default value how?

  @Enum({
    type: "RelatedType",
    items: () => RelatedType,
    default: RelatedType.None,
    nullable: true,
  })
  relatedType?: RelatedType

to

  @Enum({ default: MailType.Unknown })
  mailType: MailType

  @Enum({ default: RelatedType.None, nullable: true })
  relatedType?: RelatedType

It seems that defining type and items for an int enum is not allowed?

Changing the enum definition does create them the same way they were (int2), but does not cause the newly created migration to contain schema changes over and over. A newly created migration is empty as it should be.

However, the initial migration created from the existing schema was still not logged to the migrations table. I'll try to figure out what causes this.

@tuhlmann
Copy link
Author

I think I found why the initial migration doesn't get logged.
I added some comments to Migrator.validateInitialMigration and it seems the comparison between existing and expected is wrong.

I'm logging all the table names that are found in the database versus all those found in the schema. They can't match.
Here's the code with logging:

schema.getTables().forEach(table => {
            /* istanbul ignore next */
            const tableName = table.schema ? `${table.schema}.${table.name}` : table.name;
            console.log("found table ", tableName, expected.has(tableName));
            if (expected.has(tableName)) {
                exists.add(tableName);
            }
        });

Here's the names found in the DB (second are is expected.has(tableName)):

found table  public.attachment false
found table  public.attendee_event false
found table  public.email_preferences false
found table  public.event false
found table  public.event_documents false
found table  public.queued_mail false
found table  public.sent_mail false
found table  public.user false

Here's the set of expected entries:

Set {
  'attachment',
  'attendee_event',
  'email_preferences',
  'event',
  'queued_mail',
  'sent_mail',
  'user',
  'event_documents'
}

The set of existing entries is empty.

@B4nan B4nan closed this as completed in 429d832 Dec 2, 2020
@B4nan
Copy link
Member

B4nan commented Dec 2, 2020

Fixed in 4.3.3-dev.12

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

No branches or pull requests

2 participants