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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a custom text array type in postgres results in the migration generator to always include alter statements #713

Closed
yanniz0r opened this issue Aug 6, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@yanniz0r
Copy link

yanniz0r commented Aug 6, 2020

Hey folks!

At first: Thanks for this awesome library! I really like the developer experience and the code base is one of the cleanest in TS I've seen so far. 馃帀

Describe the bug
Adding a custom type, in my case a StringArrayType which is basically the solution provided here, results in the migration creation to generate the "dirty" migration file. My type looks like this:

import { Type } from 'mikro-orm';

/**
 * This type used until version 4 of mikro orm is released as it supports array types natively
 */
class StringArrayType extends Type {
  convertToDatabaseValue(javascriptValue?: string[]) {
    const stringValues = javascriptValue?.map(value => JSON.stringify(value)).join(',');
    return `{${stringValues}}`;
  }
  toJSON(javascriptValue: string[]) {
    return JSON.stringify(javascriptValue);
  }
  convertToJSValue(databaseValue: string) {
    return databaseValue;
  }
  getColumnType(){
    return 'text[]';
  }
}

export default StringArrayType;

I found out, that the database returns a udt_name of _text which does neither match the provided type from getColumnType nor the types provided in the PostgreSqlSchemaHelper.TYPES. Adding textArray: ['_text', 'text[]'], to the TYPES object resolves the issue for me. If this solution is viable for mikro-orm I'm happy to provide a pull request - However, I think this is not the best solution.

To Reproduce
Steps to reproduce the behavior:

  1. Add the custom type specified above as a type on an entity
  2. Create an migration with yarn mikro-orm migration:create
  3. Migrate the database with yarn mikro-orm migration:up
  4. Create another migration with yarn mikro-orm migration:create

Observed behavior
The migration contains something like the following:

    this.addSql('alter table "events" drop constraint if exists "events_genres_check";');
    this.addSql('alter table "events" alter column "genres" type text[] using ("genres"::text[]);');

Even if you keep this, run the migration and create another one, a similar file is always created.
Expected behavior
The created migration should be empty as no changes have happend.

Versions

Dependency Version
node v14.4.0
typescript 3.9.6
mikro-orm 3.6.15
pg 8.3.0
@yanniz0r yanniz0r added the bug Something isn't working label Aug 6, 2020
@yanniz0r
Copy link
Author

yanniz0r commented Aug 6, 2020

I just had a look at you solution for version 4. The problem is solved as follows: https://github.com/mikro-orm/mikro-orm/pull/559/files#diff-71ca1e86f35878d6b6b39e1b45e91a84R77

So my issue will probably be resolved with version 4. How is the current estimation for the release? Is there anything I can do to help?

@B4nan
Copy link
Member

B4nan commented Aug 7, 2020

Ok, so closing this as resolved in v4 (I am not planning to ship more v3 releases).

How is the current estimation for the release?

Will be merging it during the weekend and releasing RC. Then probably week or two to adjust docs and write a release article, but in general its ready.

Is there anything I can do to help?

Migrate to v4 and test it :] I am using the latest alpha in my production app, working fine for me, so I'd say nothing to be afraid of, especially if you have your app covered with tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants