Skip to content

MSSQL Schema update and validation #970

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

Merged
merged 9 commits into from
Sep 29, 2021

Conversation

DavideD
Copy link
Member

@DavideD DavideD commented Sep 22, 2021

Fixes #912
A test for the following pr: #967

I've rebased it to the latest changes and it's using the available HIbernate ORM (5.6.0.Beta2)

Waiting for this PR to be merged and released

@DavideD
Copy link
Member Author

DavideD commented Sep 22, 2021

@gbadner, @blafond I was expecting this PR to fail but it seems to work fine. What am I missing?

@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

I'm not sure how it could be passing?!? I'm looking into it...

@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

@DavideD, take a look at the standard output for the SQL Server test:
hibernate-reactive-core/build/reports/tests/test/classes/org.hibernate.reactive.SchemaUpdateSqlServerTestBase$IndividuallySchemaUpdateSqlServerTestBase.html

The drop sequence statement fails:

?[33mWARN?[m ?[34mReactiveGenerationTarget?[m [vert.x-eventloop-thread-0] HR000021: DDL command failed [io.vertx.mssqlclient.MSSQLException: {number=166, state=1, severity=15, message=''DROP SEQUENCE' does not allow specifying the database name as a prefix to the object name.', serverName='8ec40056a401', lineNumber=1, additional=[io.vertx.mssqlclient.MSSQLException: {number=8180, state=1, severity=16, message='Statement(s) could not be prepared.', serverName='8ec40056a401', lineNumber=1}]}]

Later in the output, you'll see that the create sequence statement fails for the same reason.

Later in the test, you'll see that select next value for master.dbo.hibernate_sequence statements succeed.
(you can't actually search for that string in the output because of all the embedded "?[34m".)

Apparently, the hibernate_sequence is left over from a previous test, and that is what is being used.

Using a local ORM 5.6.0-SNAPSHOT, these messages are gone, and the drop/create sequence statements succeed.

@DavideD
Copy link
Member Author

DavideD commented Sep 22, 2021

Thanks @gbadner, makes sense.

@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

Supersedes #967.

@gbadner gbadner added the waiting We are waiting for another PR or issue to be solved before merging this one label Sep 22, 2021
@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

We need to wait for hibernate/hibernate-orm#4235 to get merged and released before this PR can be merged.

@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

Using a local ORM 5.6.0-SNAPSHOT, these messages are gone, and the drop/create sequence statements succeed.

To clarify, after publishing the hibernate-core jar from hibernate/hibernate-orm#4235 (which is not merged yet) to a local repository, and setting gradle.properties to use that jar, the drop/create sequence statements succeed.

@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

Pushed a commit to fix inconsistent test arguments.

@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

@DavideD, is the DB2 test failure expected?

@DavideD
Copy link
Member Author

DavideD commented Sep 22, 2021

I will restart the job... not sure why it failed

@DavideD
Copy link
Member Author

DavideD commented Sep 22, 2021

I've added a commit that should make the tests fail consistently.
It will delete the sequence if it exists before running the creation of the schema.
@gbadner or @blafond could you check that it works when using the right ORM snapshot, please?

@DavideD DavideD changed the title 912 schema update sql server 1 MSSQL Schema update and validation Sep 22, 2021
@gbadner
Copy link
Contributor

gbadner commented Sep 22, 2021

@DavideD, I pulled your commit, confirmed the tests fail using 5.6.0.Beta2, and confirmed the tests pass using hibernate/hibernate-orm#4235.

FWIW, specifying the catalog when creating a sequence on ORM worked fine. Vert.x throws an exception when the catalog is specified.

SQL Server documentation of the create sequence syntax only allows the schema, not the catalog:
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver15&viewFallbackFrom=sql-server-ver12 . This is mentioned in ORM code that that was changed.

@DavideD DavideD force-pushed the 912-schema-update-sql-server-1 branch 3 times, most recently from c33cbd2 to 3b80f9c Compare September 23, 2021 09:11
@DavideD
Copy link
Member Author

DavideD commented Sep 23, 2021

Thanks @gbadner, I've updated the comment

@DavideD DavideD force-pushed the 912-schema-update-sql-server-1 branch from 5a78810 to dca6ad6 Compare September 23, 2021 11:02
@DavideD DavideD requested a review from gbadner September 23, 2021 14:01
@blafond
Copy link
Member

blafond commented Sep 23, 2021

tested successfully locally with default ORM 5.6.0.Beta2 as well as my local 5.6.0-SNAPSHOT

@gbadner
Copy link
Contributor

gbadner commented Sep 23, 2021

@davide, I agree that what you did will make the test pass. I'm not sure if it will always be correct though. The problem is that ORM doesn't have a way to find out the catalog name (which is the database name) unless Hibernate Reactive provides it.

I think it would be fine to deal with the catalog name separately.

@gbadner
Copy link
Contributor

gbadner commented Sep 23, 2021

@DavideD, I have some ideas about how the catalog name might be resolved. Should I go ahead and create the follow-on issue for catalog, or would like to do that?

@DavideD
Copy link
Member Author

DavideD commented Sep 23, 2021

@DavideD, I have some ideas about how the catalog name might be resolved. Should I go ahead and create the follow-on issue for catalog, or would like to do that?

Yes, please. Create a separate issue. I've tried to play a bit with it but could figure out what to do.
It seems the property also causes issues with MySQL and MariaDB

@DavideD
Copy link
Member Author

DavideD commented Sep 23, 2021

@davide, I agree that what you did will make the test pass. I'm not sure if it will always be correct though. The problem is that ORM doesn't have a way to find out the catalog name (which is the database name) unless Hibernate Reactive provides it.

Sure but I see this as a separate issue. If I'm not mistaken, everything will work for users that don't set the catalog name

@gbadner
Copy link
Contributor

gbadner commented Sep 23, 2021

@DavideD, I'm really not sure if it will always work.

SQL Server's default catalog name is "master". I've read that it is possible to change it. I am not sure if/how that would impact what gets returned by information_schema views.

I have not figured out how to change it with podman, so I haven't tested it.

@DavideD
Copy link
Member Author

DavideD commented Sep 23, 2021

We can just test with the default name and make sure that it works with that

@gbadner
Copy link
Contributor

gbadner commented Sep 23, 2021

Your change will make the queries not take the catalog name into account at all. If it is possible for information_schema to have more than one catalog, then query results that ORM relies on might not be correct.

This may not be an issue; I don't know...

@DavideD
Copy link
Member Author

DavideD commented Sep 23, 2021

What I mean is that the issue with the catalog is not just for schema validation/update. It's something that won't work in other cases as well. I would expect this test to pass with MSSQL: DavideD@5b979c2

@gbadner
Copy link
Contributor

gbadner commented Sep 23, 2021

@DavideD, I think it really only matters for SchemaMigrator and SchemaValidator.

@gbadner
Copy link
Contributor

gbadner commented Sep 23, 2021

Yes, the catalog name will be used if provided for SchemaExport, but it is not needed SchemaExport.

@gbadner
Copy link
Contributor

gbadner commented Sep 23, 2021

I suppose it might be needed if the catalog is a non-default, but honestly, I don't know. This is not an issue for ORM because ORM can get the information it needs.

@DavideD
Copy link
Member Author

DavideD commented Sep 24, 2021

I've refactored the code and added back the test with the default catalog name.
The latest changes in the PR test the following cases:

  1. Property hibernate.default_catalog is not set and MSSQL uses the default catalog name -> Works
  2. Property hibernate.default_catalog is set to master (the default catalog name for MSSQL) -> requires this fix for ORM

private String anotherString;
}

public static class AOtherId implements Serializable {
Copy link
Member Author

Choose a reason for hiding this comment

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

@gbadner I've just noticed this class but I don't think we are using it. Is it necessary for the test or is this a leftover?

Copy link
Contributor

Choose a reason for hiding this comment

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

@DavideD, yes, it is needed. It is an IdClass used by AOther.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes, sorry

@DavideD DavideD force-pushed the 912-schema-update-sql-server-1 branch from 3e3ea79 to 79b1938 Compare September 29, 2021 16:05
@DavideD DavideD force-pushed the 912-schema-update-sql-server-1 branch from 79b1938 to c5f48c0 Compare September 29, 2021 16:08
@DavideD DavideD marked this pull request as ready for review September 29, 2021 16:08
@DavideD DavideD merged commit c5f48c0 into hibernate:main Sep 29, 2021
@DavideD
Copy link
Member Author

DavideD commented Sep 29, 2021

Merged!

@gavinking
Copy link
Member

Excellent!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting We are waiting for another PR or issue to be solved before merging this one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SchemaMigrator/SchemaValidator support for MS SQL Server
4 participants