Skip to content

feat: implement proper schema support in mysql, snowflake, add queryInterface.listDatabases - #14999

Merged
ephys merged 34 commits into
mainfrom
ephys/mysql-create-schema
Sep 24, 2022
Merged

ephys merged 34 commits into
mainfrom
ephys/mysql-create-schema

Conversation

@ephys

@ephys ephys commented Sep 17, 2022 •

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • Does yarn test or yarn test-DIALECT pass with this change (including linting)?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description Of Change

This PR blocks #14687

This PR aims to fix the way we support schemas in mysql.

Before this PR, creating a table "users" in the schema "archive" would result in a table archive.users being created in the default schema.

After this PR, it would create the table users in the schema archive.

I believe the source of the issue is a confusion caused by the fact that in mysql/mariadb, CREATE SCHEMA and CREATE DATABASE are synonymous. However, the behavior of mysql's schema/database is that of a schema (as understood by the rest of our codebase and how other dialects define what a schema is), not a database.

Surprisingly, our MariaDB implementation already did this correctly. All I had to (more or less) was hoist the implementation from MariaDB to MySQL, as MariaDB extends MySQL.

Other Changes

  • Added an integration test for createDatabase & dropDatabase, did you know we had none?
  • Added a new method queryInterface.listDatabases (needed it for integration tests)
  • Added typings for queryGenerator.{create,drop}DatabaseQuery, as well as queryGenerator.{create,drop}SchemaQuery, queryGenerator.listSchemasQuery
  • createDatabaseQuery and createSchemaQuery: now throws if it receives an option that specific dialect does not support (so people don't think the option is supported just because it's listed in our types)
  • Moved their tests to group them by function rather than by dialect. Makes it much easier to determine whether one of them is doing something wrong.
  • Disabled schema tests in dialects that silently ignored schemas, because they now throw.
  • QueryGenerator#{create,drop}Schema are now named QueryGenerator#{create,drop}SchemaQuery to be aligned with other QueryGenerator methods. showSchemasQuery renamed to listSchemasQuery
  • Added support for the skip option in all implementations of listSchemasQuery
  • Fixed a few values not being escaped in ibmi
  • Implemented schema methods in snowflake

TODO

  • Finish proper schema support
  • Unify createSchema/dropSchema tests

Squash Commit Message Body


BREAKING CHANGE: the 'schema' option in MySQL now references actual schemas instead of adding a prefix to table names.

@ephys ephys added type: bug DEPRECATED: replace with the "bug" issue type type: feature DEPRECATED: replace with the "feature" issue type labels Sep 17, 2022
@ephys ephys self-assigned this Sep 17, 2022
@ephys ephys changed the title feat(mysql)!: implement proper schema support feat(mysql)!: implement proper schema support, add queryInterface.listDatabases Sep 17, 2022
@ephys ephys changed the title feat(mysql)!: implement proper schema support, add queryInterface.listDatabases feat!: implement proper schema support in mysql, add queryInterface.listDatabases Sep 17, 2022
@ephys
ephys marked this pull request as ready for review September 17, 2022 19:14
@ephys
ephys marked this pull request as draft September 17, 2022 19:15
@ephys ephys changed the title feat!: implement proper schema support in mysql, add queryInterface.listDatabases feat: implement proper schema support in mysql, add queryInterface.listDatabases Sep 17, 2022
Comment thread src/dialects/mariadb/query-generator.js Resolved
@ephys ephys changed the title feat: implement proper schema support in mysql, add queryInterface.listDatabases feat: implement proper schema support in mysql, snowflake, add queryInterface.listDatabases Sep 17, 2022
@ephys
ephys requested a review from a team September 18, 2022 11:18
@WikiRik

WikiRik commented Sep 18, 2022

Copy link
Copy Markdown
Member

I haven't really looked at the code yet, just mainly the description;

  • Do I understand correctly that people using MySQL have to update the names and schema of the database tables in order to upgrade to v7? So if a table is now called archive.users they need to update the name to users and set the schema to archive? Can you provide an example query to do this in the upgrade guide?
  • Is there a backwards compatible config option for this? Probably not enabled by default since I can understand that people might want to use . in their table names

@ephys

ephys commented Sep 18, 2022 •

Copy link
Copy Markdown
Member Author

So if a table is now called archive.users they need to update the name to users and set the schema to archive? Can you provide an example query to do this in the upgrade guide?

That is correct, and they have two ways of migrating:

Option 1: Rename & move the tables

Let's say you had a table called archive.users in the default schema, you can rename it & move it to the archive schema like this:

CREATE SCHEMA `archive`;
ALTER TABLE `archive.users` RENAME TO `archive`.`users`;

Option 2: Rename your model

If you have a model that uses the schema option, like in the following example:

sequelize.define('Users', {/* attributes */}, {
  tableName: 'users',
  schema: 'archive',
});

you can continue using the old naming by changing its definition to this:

sequelize.define('Users', {/* attributes */}, {
  tableName: 'archive.users',
});

Is there a backwards compatible config option for this?

Because the migration is decently simple if you want to continue with the old fake schema (see option 2 above), and considering the complexity such an option would add, I don't think we should add one

@WikiRik

WikiRik commented Sep 18, 2022

Copy link
Copy Markdown
Member

Ah yes, I forgot about option 2. But that's a very valid workaround and indeed does not require further backwards compatibility

fzn0x
fzn0x previously approved these changes Sep 18, 2022

@fzn0x fzn0x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Other than that, looks good to me, many improvements on the syntax too.

Comment thread test/support.ts Resolved

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very good PR as always, nice additions as well.

I didn't have time to look into why some dialects do not have native databases support, but I also want to do that soon for my own knowledge.
I also didn't have the time yet to properly take a look at if all new functions have tests with them which I want to do in the next few days.

Comment thread test/unit/sql/where.test.ts Resolved
Comment thread test/unit/sql/create-schema.test.js Resolved
Comment thread src/utils/check.ts Resolved
Comment thread src/dialects/abstract/index.ts Resolved
Comment thread src/dialects/mysql/query-generator.js Resolved
Comment thread test/integration/model.test.js Resolved
@WikiRik WikiRik mentioned this pull request Sep 23, 2022
6 tasks done
@ephys
ephys requested review from WikiRik and fzn0x September 23, 2022 11:12

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like all my comments have been resolved. I think we're good to go!

@ephys
ephys merged commit 2ea7a6e into main Sep 24, 2022
@ephys
ephys deleted the ephys/mysql-create-schema branch September 24, 2022 10:58
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 7.0.0-alpha.18 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released on @v7 type: bug DEPRECATED: replace with the "bug" issue type type: feature DEPRECATED: replace with the "feature" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants