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

[jdbc] Fix tableCaseSensitiveItemNames for PostgreSQL/TimescaleDB #17587

Merged
merged 12 commits into from
Oct 19, 2024

Conversation

JonathanvdGHU
Copy link
Contributor

Case-sensitive tables working for TimescaleDB database via JDBC persistence service

Description

The following issue is resolved. My implementation is maybe not the best one because I didn't take the time to understand everything. So I don't mind if someone makes some small adjustments and makes a new pull request :)

@JonathanvdGHU JonathanvdGHU requested a review from a team as a code owner October 18, 2024 15:08
@jlaur jlaur linked an issue Oct 18, 2024 that may be closed by this pull request
Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
@jlaur
Copy link
Contributor

jlaur commented Oct 18, 2024

Thanks for the fix. I'll review this in the weekend.

@jlaur jlaur added the bug An unexpected problem or unintended behavior of an add-on label Oct 18, 2024
@JonathanvdGHU
Copy link
Contributor Author

Thanks for the fix. I'll review this in the weekend.

Good to hear. Is it a big problem I made a mess with the commits? I am trying to do a rebase but it's not really working.

@jlaur
Copy link
Contributor

jlaur commented Oct 18, 2024

Good to hear. Is it a big problem I made a mess with the commits?

It seems some commits are incorrectly signed. If there is at least one correctly signed commit, I can use that when squash-merging. Otherwise you will need to rebase to fix it.

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
@JonathanvdGHU
Copy link
Contributor Author

Good to hear. Is it a big problem I made a mess with the commits?

It seems some commits are incorrectly signed. If there is at least one correctly signed commit, I can use that when squash-merging. Otherwise you will need to rebase to fix it.

I fixed it. This was my first rebase so I am also a little proud about myself xD

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
@jlaur jlaur changed the title Case-sensitive tables working for TimescaleDB database via JDBC persistence service [jdbc] Case-sensitive tables working for TimescaleDB database via JDBC persistence service Oct 18, 2024
Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

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

The fix looks valid, but I have provided some feedback on the approach. I think it would be preferable to keep the DTO's unchanged and instead directly fix the queries which only concern PostgreSQL/TimescaleDB.

@jlaur jlaur changed the title [jdbc] Case-sensitive tables working for TimescaleDB database via JDBC persistence service [jdbc] Fix tableCaseSensitiveItemNames for PostgreSQL/TimescaleDB Oct 19, 2024
Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
@JonathanvdGHU
Copy link
Contributor Author

The fix looks valid, but I have provided some feedback on the approach. I think it would be preferable to keep the DTO's unchanged and instead directly fix the queries which only concern PostgreSQL/TimescaleDB.

I implemented your requested changes :)

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
@jlaur
Copy link
Contributor

jlaur commented Oct 19, 2024

I implemented your requested changes :)

Thanks! I'll take one more look. In the meantime, I see there are formatting changes for comments, why is that? I just want to avoid having this changed back and forth depending on some individual IDE settings, if that's the case. It also disturbs diff'ing. 😉

Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

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

Last batch of comments, I hope. 🙂

@jlaur
Copy link
Contributor

jlaur commented Oct 19, 2024

I think it would be preferable to keep the DTO's unchanged and instead directly fix the queries which only concern PostgreSQL/TimescaleDB.

I can see now after the latest changes the dilemma here when overridden queries differ only in table name being quoted. With the approach I suggested, those queries are now overridden, which feels like duplication. For example:

sqlDropTable = "DROP TABLE \"#tableName#\"";

overriding:

    protected String sqlDropTable = "DROP TABLE #tableName#";

With your original approach, instead we would have had to overload the methods only to call a different DTO method, which would have been even worse.

So it seems we would need to do something different to fully avoid duplication. What would you think about introducing a protected method getTableName in JdbcBaseDAO, something like this:

    protected String getTableName(ItemsVO vo) {
        return vo.getTableName();
    }

which could then be used from all query methods, i.e. getTableName(vo) rather than vo.getTableName()?

This would allow for overriding this method for PostgreSQL to return the quoted table name, but without changing any query methods or queries when they are not otherwise needed to be adapted for PostgreSQL?

This is just from the top of my head and all optional, and if you prefer to stick with your current implementation, that's fine as well.

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
@JonathanvdGHU
Copy link
Contributor Author

I implemented your requested changes :)

Thanks! I'll take one more look. In the meantime, I see there are formatting changes for comments, why is that? I just want to avoid having this changed back and forth depending on some individual IDE settings, if that's the case. It also disturbs diff'ing. 😉

It was the IDE 😅.

@JonathanvdGHU
Copy link
Contributor Author

JonathanvdGHU commented Oct 19, 2024

I think it would be preferable to keep the DTO's unchanged and instead directly fix the queries which only concern PostgreSQL/TimescaleDB.

I can see now after the latest changes the dilemma here when overridden queries differ only in table name being quoted. With the approach I suggested, those queries are now overridden, which feels like duplication. For example:

sqlDropTable = "DROP TABLE \"#tableName#\"";

overriding:

    protected String sqlDropTable = "DROP TABLE #tableName#";

With your original approach, instead we would have had to overload the methods only to call a different DTO method, which would have been even worse.

So it seems we would need to do something different to fully avoid duplication. What would you think about introducing a protected method getTableName in JdbcBaseDAO, something like this:

    protected String getTableName(ItemsVO vo) {
        return vo.getTableName();
    }

which could then be used from all query methods, i.e. getTableName(vo) rather than vo.getTableName()?

This would allow for overriding this method for PostgreSQL to return the quoted table name, but without changing any query methods or queries when they are not otherwise needed to be adapted for PostgreSQL?

This is just from the top of my head and all optional, and if you prefer to stick with your current implementation, that's fine as well.

I also thought it was duplication so that's why I added the function getQuotedTableName to the DTO's. I think it's a good idea but to be honest, I don't have much time to do it. So maybe someone can change it later to improve the code.

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
@JonathanvdGHU
Copy link
Contributor Author

Last batch of comments, I hope. 🙂

I think it's now done.

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>

re-added deleted changed from rebase

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>

removed not related code to the bug

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>

implemented feedback

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>

changes also implied on itemsmanagetable

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>

reverted notes

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>

reverted missed notes

removed unnecessary methods

Signed-off-by: JonathanvdGHU <jonathan.vandegiessen@student.hu.nl>
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Copy link
Contributor

@jlaur jlaur left a comment

Choose a reason for hiding this comment

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

LGTM - thanks! I'll wait for confirmation that everything still works correctly after the latest changes. 🙂

@jlaur
Copy link
Contributor

jlaur commented Oct 19, 2024

I also thought it was duplication so that's why I added the function getQuotedTableName to the DTO's.

The problem with that is that you would need to overload all methods in order to call the alternative DTO method, so that would generate even more duplication - it would just be duplicated methods rather than duplicated query strings.

I think it's a good idea but to be honest, I don't have much time to do it. So maybe someone can change it later to improve the code.

That's fine, I'll see if I can find some time to refactor this later.

@JonathanvdGHU
Copy link
Contributor Author

LGTM - thanks! I'll wait for confirmation that everything still works correctly after the latest changes. 🙂

Thanks for the patience for all this 😌

@jlaur
Copy link
Contributor

jlaur commented Oct 19, 2024

Thanks for the patience for all this 😌

Likewise. 😉 Before merging, besides test confirmation, there is one more thing needed: When checking the failed DCO, I noticed that your real (full) name is missing in your signoffs. You can fix both issues by rebasing your commits. If you click "Details" right to "DCO", you can see how to do that. If you are not comfortable with that, I can fix the signoff when squash-merging the PR. In that case I'd just need your full name. Thanks!

@JonathanvdGHU
Copy link
Contributor Author

JonathanvdGHU commented Oct 19, 2024

Thanks for the patience for all this 😌

Likewise. 😉 Before merging, besides test confirmation, there is one more thing needed: When checking the failed DCO, I noticed that your real (full) name is missing in your signoffs. You can fix both issues by rebasing your commits. If you click "Details" right to "DCO", you can see how to do that. If you are not comfortable with that, I can fix the signoff when squash-merging the PR. In that case I'd just need your full name. Thanks!

My full name is included in my email, that you can see in the signoff. I'll update it if I return for any future bug fixes! 😅. So you can use that name for squash-merging. I tested on another branch how to rebase successfully (there are a lot of merge conflicts) but I think it's the easiest way if you do it.

@JonathanvdGHU
Copy link
Contributor Author

Thanks for the patience for all this 😌

Likewise. 😉 Before merging, besides test confirmation, there is one more thing needed: When checking the failed DCO, I noticed that your real (full) name is missing in your signoffs. You can fix both issues by rebasing your commits. If you click "Details" right to "DCO", you can see how to do that. If you are not comfortable with that, I can fix the signoff when squash-merging the PR. In that case I'd just need your full name. Thanks!

What is your estimate when the pull request will be merged?

@jlaur
Copy link
Contributor

jlaur commented Oct 19, 2024

What is your estimate when the pull request will be merged?

I"ll merge when you have confirmed that everything is still working after the latest changes.

@JonathanvdGHU
Copy link
Contributor Author

JonathanvdGHU commented Oct 19, 2024

What is your estimate when the pull request will be merged?

I"ll merge when you have confirmed that everything is still working after the latest changes.

Oh a small misunderstanding I guess. Everything related to the bug is working now, so it's fixed.

@jlaur jlaur merged commit e9a6cb6 into openhab:main Oct 19, 2024
4 of 5 checks passed
@jlaur jlaur added this to the 4.3 milestone Oct 19, 2024
@JonathanvdGHU
Copy link
Contributor Author

@jlaur I double checked and found out I missed one table in the PostgresqlDOA😶. Without the fix you can't see the graphs in OpenHAB, beside that everything works fine. The fix is a oneliner that I commited on the same branch. Would you suggest to make a new pull request or are you gonna take it with your refactor (in case you are going to do it)?

@jlaur
Copy link
Contributor

jlaur commented Oct 20, 2024

@jlaur I double checked and found out I missed one table in the PostgresqlDOA😶. Without the fix you can't see the graphs in OpenHAB, beside that everything works fine. The fix is a oneliner that I commited on the same branch. Would you suggest to make a new pull request or are you gonna take it with your refactor (in case you are going to do it)?

It would be highly appreciated if you could create a new PR with the fix. Using the same branch is a bit messy, because all the previous commits will appear again. Since it's a one-liner and since we use squash-merge, it will be fine this time. For your next contributions (hoping there will be some 😉) it is recommended to create a new branch from your local main branch after pulling latest changes.

Comment on lines +76 to +77
WHERE table_name='\"#tableName#\"' AND table_catalog='#jdbcUriDatabaseName#' AND table_schema=(SELECT table_schema FROM information_schema.tables WHERE table_type='BASE TABLE' \
AND table_name='\"#itemsManageTable#\"')\
Copy link
Contributor

Choose a reason for hiding this comment

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

@JonathanvdGHU - I think the quotes here were a mistake, since the table names are not used for referencing the tables directly, but rather being used to query the information schema. Do you agree? See also #17597.

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 understand what you mean, but to be honest I am not sure how postgreSQL reacts when you remove them.

Copy link
Contributor

Choose a reason for hiding this comment

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

It depends how it internally stores the table names. Would it possibly for you to run a query so we can see?

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 will try it when I have the time.

Copy link
Contributor Author

@JonathanvdGHU JonathanvdGHU Oct 21, 2024

Choose a reason for hiding this comment

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

It's the same case here. (it's PostgreSQL btw, I know a little bit confusing)
image

Copy link
Contributor

@jlaur jlaur Oct 21, 2024

Choose a reason for hiding this comment

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

Can you try:

SELECT table_name
FROM information_schema.tables

I would expect it to return e.g. TotalUsage rather than "TotalUsage".

Or to be specific as in your query (single quotes were missing):

SELECT *
FROM information_schema.tables
WHERE table_name = 'TotalUsage'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
2. (still postgreSQL)
image

Copy link
Contributor

@jlaur jlaur Oct 21, 2024

Choose a reason for hiding this comment

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

Excellent, thanks. So indeed the double-quotes should not be included in those queries, thus already fixed as part of #17597.

@jlaur jlaur removed the bug An unexpected problem or unintended behavior of an add-on label Oct 21, 2024
KaaNee pushed a commit to KaaNee/openhab-addons that referenced this pull request Nov 8, 2024
…penhab#17587)

Signed-off-by: Jonathan van de Giessen <jonathan.vandegiessen@student.hu.nl>
matchews pushed a commit to matchews/openhab-addons that referenced this pull request Dec 16, 2024
…penhab#17587)

Signed-off-by: Jonathan van de Giessen <jonathan.vandegiessen@student.hu.nl>
cipianpascu pushed a commit to cipianpascu/openhab-addons that referenced this pull request Jan 2, 2025
…penhab#17587)

Signed-off-by: Jonathan van de Giessen <jonathan.vandegiessen@student.hu.nl>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
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.

[jdbc] TimescaleDB persistence didn’t create correct tables
2 participants