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

queryInterface: Support DROP COLUMN IF EXISTS #11774

Open
jy95 opened this issue Dec 29, 2019 · 4 comments · May be fixed by #11885
Open

queryInterface: Support DROP COLUMN IF EXISTS #11774

jy95 opened this issue Dec 29, 2019 · 4 comments · May be fixed by #11885

Comments

@jy95
Copy link

@jy95 jy95 commented Dec 29, 2019

Issue Description

Is your feature request related to a problem? Please describe.

When doing some migrations, I would like to delete a column created in a previous migration.
However, this is not possible with the current code for some scenarios.

An example :
migration v3

'use strict';

let opts = {tableName: 'Exercises'};

module.exports = {
    up: (queryInterface, Sequelize) => {
        if (queryInterface.sequelize.options.schema) {
            opts.schema = queryInterface.sequelize.options.schema;
        }
        return Promise.all([
            queryInterface.addColumn(opts, "isValidated", {
                type: Sequelize.BOOLEAN,
                allowNull: false,
                defaultValue: false
            })
        ]);
    },

    down: (queryInterface, Sequelize) => {
        if (queryInterface.sequelize.options.schema) {
            opts.schema = queryInterface.sequelize.options.schema;
        }
        return Promise.all([
            queryInterface.removeColumn(opts, "isValidated")
        ]);
    }
};

migration v4

'use strict';

let opts = {tableName: 'Exercises'};

module.exports = {
    up: (queryInterface, Sequelize) => {
        if (queryInterface.sequelize.options.schema) {
            opts.schema = queryInterface.sequelize.options.schema;
        }
        return Promise.all([
            queryInterface.removeColumn(opts, "isValidated"),
            queryInterface.addColumn(opts, "state", {
                type: Sequelize.ENUM("42", "42-42"),
                default: "42"
            })
        ]);
    },

    down: (queryInterface, Sequelize) => {
        if (queryInterface.sequelize.options.schema) {
            opts.schema = queryInterface.sequelize.options.schema;
        }
        return Promise.all([
            queryInterface.removeColumn(opts, "state")
        ]);
    }
};

image

Describe the solution you'd like

Just add an option in the options of removeColumn

If I take the test in the codebase as example :

      it('schema', () => {
        expectsql(sql.removeColumnQuery({
          schema: 'archive',
          tableName: 'user'
        }, 'email', { mustExist: false } ), {
          mssql: 'ALTER TABLE [archive].[user] DROP COLUMN [email];',
          mariadb: 'ALTER TABLE `archive`.`user` DROP `email`;',
          mysql: 'ALTER TABLE `archive.user` DROP `email`;',
          postgres: 'ALTER TABLE IF EXISTS "archive"."user" DROP COLUMN "email";'
        });
      });

Why should this be in Sequelize

Just read what I said above ;)

Describe alternatives/workarounds you've considered

None as truncate removeColumn promise to deal with this case is horrible :(

Issue Template Checklist

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): PostgreSQL

Would you be 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, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@papb papb changed the title PostgreSQL : Drop column IF EXISTS queryInterface: Support DROP COLUMN IF EXISTS Jan 16, 2020
@papb
Copy link
Member

@papb papb commented Jan 16, 2020

Thanks for the request; are you sure this is postgres-only? None of the other dialects support DROP COLUMN IF EXISTS?

@jy95
Copy link
Author

@jy95 jy95 commented Jan 16, 2020

It depends :

@papb
Copy link
Member

@papb papb commented Jan 16, 2020

Hmm okay, so at best it is not so straightforward in the other dialects; let's keep this request postgres-only then, to make things easier for everyone :)

@nassimmoussa
Copy link

@nassimmoussa nassimmoussa commented Jan 27, 2020

i would like to work on this, can you give any suggestions or just go for it on my own ?

@nassimmoussa nassimmoussa linked a pull request that will close this issue Jan 28, 2020
5 of 6 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.