Skip to content

Conversation

@rPraml
Copy link
Contributor

@rPraml rPraml commented Mar 25, 2022

Hello Rob, thanks for fixing #107
I did some tests and I think it is working as expected.
I have some suggestions and ideas, how to further improve the current procedure.
(they are non-critical)

Copy link
Contributor Author

@rPraml rPraml left a comment

Choose a reason for hiding this comment

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

For MySql/MariaDb it would be possible to obtain the lock BEFORE trying to create the table, but I didn't want to change thant now

private boolean obtainNamedLock(Connection connection) throws SQLException {
try (PreparedStatement query = connection.prepareStatement("select get_lock('ebean_migration', 10)")) {
String hash = Integer.toHexString(connection.getMetaData().getURL().hashCode());
try (PreparedStatement query = connection.prepareStatement("select get_lock('ebean_migration-" + hash + "', 10)")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this makes an application wide lock on the server. This means, if one ebean application runs a migration, no other ebean app (in different db) can run its migration.
I would suggest only to lock the database.
We have a multi tenat setup and we plan to run migration in different tenants parallel.

try {
obtainLockWithWait();
} catch (RuntimeException re) {
// catch "failed to obtain row locks"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes an issue, when createTableDdl succeeds, but createInitMetaRow will fail. It will give you the source of the error.

(while writing the tests, I forgot to set the sql-platform and I got the "failed to obtan row locks", because the init-sql had the wrong syntax.)

}

@Disabled
//@Disabled
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can disable this, if build takes too long

dropTable("m3");

if (withExisting) {
// create empty migration table
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added tests where it checks, if the migration will also work (so, if table exists, but contains no run migrations, yet)

try (Connection conn = dataSource.getConnection()) {
String derivedPlatformName = DbNameUtil.normalise(conn);

config.setPlatform(derivedPlatformName);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I forgot this line, and got wrong script for sql server (see other comment with exception handling)

}
}
assertThat(rawVersions).containsExactly("0", "hello", "1.1", "1.2", "1.2.1", "m2_view");
table.unlockMigrationTable();
Copy link
Contributor Author

@rPraml rPraml Mar 25, 2022

Choose a reason for hiding this comment

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

just for completeness, I added unlockMigrationTable in all tests

@rbygrave
Copy link
Member

Nice !!

@rbygrave rbygrave added this to the 12.16.2 milestone Mar 25, 2022
@rbygrave rbygrave merged commit f92cc6d into ebean-orm:master Mar 25, 2022
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