Skip to content

Mssql can remove column with default - #4990

Merged
mickhansen merged 5 commits into
sequelize:masterfrom
MichaelKearnsSoftwire:mssql_can_remove_column_with_default
Feb 3, 2016
Merged

mickhansen merged 5 commits into
sequelize:masterfrom
MichaelKearnsSoftwire:mssql_can_remove_column_with_default

Conversation

@MichaelKearnsSoftwire

Copy link
Copy Markdown
Contributor

I've verified that all the MSSQL tests pass, but haven't yet run the tests against other vendors -- will attempt to do that soon.

I think this is otherwise mostly ready, but there are some rough edges I have some questions about:

  • I've noticed that some of the tests in query-interface.test.js don't actually assert that the correct modification to the DB has been made (e.g. addColumn doesn't ever verify that the column has been added, merely that some SQL was run and no errors occurred.) I've followed this pattern for removeColumn, but if there's no good reason for it I'm inclined to use describeTable to verify that the column has actually been removed (and likewise for existing tests)
  • My mssql query-generator.test.js is copy-pasted-and-adjusted from the similar ones for other vendors; am happy to commonise the describe block (which seems to be pretty much the same across vendors) so that each test merely contains a bunch of scenarios.

This is my first non-trivial PR to Sequelize, so let me know if I've missed anything!

@MichaelKearnsSoftwire
MichaelKearnsSoftwire force-pushed the mssql_can_remove_column_with_default branch from be7659e to 76d83b7 Compare December 7, 2015 16:55
@mickhansen

Copy link
Copy Markdown
Contributor

I've followed this pattern for removeColumn, but if there's no good reason for it I'm inclined to use describeTable to verify that the column has actually been removed (and likewise for existing tests)

Since lazyness is not a good reason, there is none, all schema modifying calls should really have integration tests that verify correctness via a describe call.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move tests for sql generation to test/unit instead.

@mickhansen

Copy link
Copy Markdown
Contributor

New tests appear to fail for postgres. https://github.com/sequelize/sequelize/blob/master/lib/dialects/postgres/query-generator.js#L196 needs to be refactored to use this.quoteTable(this.extractTableDetails(tableName)) rather than just this.quoteTable(tableName).

Thank you for this contribution, the schema modification queries need a lot of love :)

@MichaelKearnsSoftwire

Copy link
Copy Markdown
Contributor Author

Thanks for the comments! I should have time to address them later in the week.

@MichaelKearnsSoftwire

Copy link
Copy Markdown
Contributor Author

As you can tell my definition of "later in the week" is somewhat liberal :)

Since lazyness is not a good reason, there is none, all schema modifying calls should really have integration tests that verify correctness via a describe call.

@mickhansen I'm doing this now -- am I right in thinking that I can get rid of the assertions against count for most places where I make assertions against the result of a describe call? I don't see them being particularly valuable now we're verifying that changes to the DB have actually occurred.

@mickhansen

Copy link
Copy Markdown
Contributor

Should be no reason to verify the amount of sql queries run since that's really an implementation detail.

@MichaelKearnsSoftwire

Copy link
Copy Markdown
Contributor Author

@mickhansen I've finally fixed the PG tests -- it looks like they were failing because the tests were not being torn down correctly.

I've added a call to dropAllSchemas in the afterEach for this test to ensure this doesn't happen -- please let me know if there's a better way.

Also, I've implemented your previous suggestion of calling this.extractTableDetails in lib/dialects/postgres/query-generator.js, but this is no longer required for the tests to pass -- I'm not very familiar with Postgres so some guidance here would be helpful! (I also had to change lib/dialects/abstract/query-generator.js for extractTableDetails to work)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind if I put this in a separate PR? This is consistent with the other pre-existing query-generator tests, and I'd like to avoid making this PR much bigger :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query generator tests from the integration suite are a deprecated format, and not one we're looking to keep on using.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay -- I've done this for the mssql one as I created the new file, but I don't have time to do this for the other dialects soon I'm afraid.

@mickhansen

Copy link
Copy Markdown
Contributor

Looks good, just a single comment and i'd also love to see the commits squashed to one or two descriptive ones instead of the current 10+

@MichaelKearnsSoftwire
MichaelKearnsSoftwire force-pushed the mssql_can_remove_column_with_default branch from 061d291 to dd7a59e Compare February 2, 2016 21:16
* this fixes Github issue sequelize#4943
* previously, calling removeColumn for a column with a defaultValue
  specified would cause an error -- this is because in MSSQL you have
  to drop the constraint before the column. See:
      http://stackoverflow.com/questions/8641954/how-to-drop-column-with-constraint
* also added integration tests for removeColumn function
* all tests covering functionality which modifies schema
  now verify that the schema has actually changed, instead of
  just verifying against the number of logs output
* remove assertions against sql count in query-interface.test
* remove unused rawSelect against table in test

Fixes required for new query-interface tests for PG / MySQL / MariaDB

* PG: changing a column to DataTypes.FLOAT results in it being stored (and
  returned as) 'DOUBLE PRECISION'
* ensure that test schema is torn down after each test, to avoid tests
  interacting with each other
* remove unneeded calls to dropAllSchemas in query-interface.test.js
  - this is done in the afterEach call now
* MySQL/MariaDB: we have to change our expectation depending on the
  dialect, as the DataTypes names are too inconsistent
* tableName objects have a "tableName" property, not a "table"
  property
* addColumnQuery was broken by commit 3559ad, only on the
  mssql_can_remove_column_with_default branch
* as these are not integration tests
* also double-quote mocha test path to ensure tests in sub-directories
  are run (e.g. previously, connection-manager.js was not run)
  see: http://stackoverflow.com/a/19902459
* remove unused beforeEach in postgres/query-generator.test.js
@MichaelKearnsSoftwire
MichaelKearnsSoftwire force-pushed the mssql_can_remove_column_with_default branch from dd7a59e to e3ab345 Compare February 2, 2016 21:53
@MichaelKearnsSoftwire

Copy link
Copy Markdown
Contributor Author

Thanks -- I've squashed the previous tower of commits down to 6 pretty-independent commits.

Since my previous commits, a new test has been added to query-interface.test.js which uses changeColumn to add a FK reference:

        it('able to change column to foreign key', function() {
          return this.queryInterface.changeColumn('users', 'level_id', {
            type: DataTypes.INTEGER,
            references: {
              model: 'level',
              key:   'id'
            },
            onUpdate: 'cascade',
            onDelete: 'cascade'
          }, {logging: log}).then(function() {
            expect(count).to.be.equal(1);
            count = 0;
          });
        });

As far as I can tell, there's no way to verify that this FK has been added using describeTable as we now do for all the other tests -- are you aware of any other way to verify that this changeColumn has actually worked? I've left it as-is for now.

Also, do you have any comment re: my query on this.extractTableDetails above?

Thanks!

@mickhansen

Copy link
Copy Markdown
Contributor

quoteTable/extractTableDetails should be required on most things, if the tests arent failing it's because we have insufficient schema tests for those cases.

If it's not part of describeTable there's likely not a good way to verify this at the moment.

mickhansen added a commit that referenced this pull request Feb 3, 2016
…column_with_default

Mssql can remove column with default
@mickhansen
mickhansen merged commit 7378a16 into sequelize:master Feb 3, 2016
@MichaelKearnsSoftwire

Copy link
Copy Markdown
Contributor Author

Thanks!

@MichaelKearnsSoftwire
MichaelKearnsSoftwire deleted the mssql_can_remove_column_with_default branch February 3, 2016 08:50
@mickhansen

Copy link
Copy Markdown
Contributor

Released in 3.19.2

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

Successfully merging this pull request may close these issues.

2 participants