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

Use proper data type for timestamps in Postgres #1778

Merged
merged 9 commits into from
Apr 22, 2021
Merged

Use proper data type for timestamps in Postgres #1778

merged 9 commits into from
Apr 22, 2021

Conversation

pm47
Copy link
Member

@pm47 pm47 commented Apr 20, 2021

Did some refactoring in tests and introduced a new migrationCheck
helper method.

Note that the change of data type in sqlite for the commitment_number
field (from BLOB to INTEGER) is not a migration. If the table has
been created before, it will stay like it was. It doesn't matter due to
how sqlite stores data, and we make sure in tests that there is no
regression.

Did some refactoring in tests and introduced a new `migrationCheck`
helper method.

Note that the change of data type in sqlite for the `commitment_number`
field (from `BLOB` to `INTEGER`) is not a migration. If the table has
been created before, it will stay like it was. It doesn't matter due to
how sqlite stores data, and we make sure in tests that there is no
regression.
@pm47
Copy link
Member Author

pm47 commented Apr 20, 2021

I didn't add a specific v5->v6 migration test because existing tests already make sure that there are no issues with the Long/Timestamp conversion. The risk is essentially around the queries, which are tested in the add/list events test. I feel like a new test would add a lot of verbosity for little to no value. Let me know if you disagree.

edit: also, v5 was not part of a release.

@pm47 pm47 marked this pull request as ready for review April 20, 2021 14:31
@pm47 pm47 requested a review from t-bast April 20, 2021 14:31
@@ -52,15 +53,25 @@ class PgAuditDb(implicit ds: DataSource) extends AuditDb with Logging {
statement.executeUpdate("CREATE INDEX relayed_trampoline_payment_hash_idx ON relayed_trampoline(payment_hash)")
}

def migration56(statement: Statement): Unit = {
statement.executeUpdate("ALTER TABLE sent ALTER COLUMN timestamp SET DATA TYPE TIMESTAMP WITH TIME ZONE USING timestamp with time zone 'epoch' + timestamp * interval '1 millisecond'")
Copy link
Member Author

@pm47 pm47 Apr 20, 2021

Choose a reason for hiding this comment

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

This was provided as an example in the postgres documentation.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think the spaces are very useful, I don't see a good reason to align these statements together.

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea was to make similarities obvious between lines. Removed in 641abae.

Copy link
Member

Choose a reason for hiding this comment

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

It's still there in this file...?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, forgot one file. Fixed with 1ff9b7b.

@thomash-acinq
Copy link
Member

I didn't add a specific v5->v6 migration test because existing tests already make sure that there are no issues with the Long/Timestamp conversion. The risk is essentially around the queries, which are tested in the add/list events test. I feel like a new test would add a lot of verbosity for little to no value. Let me know if you disagree.

I agree that the migration is already covered by the other tests. However I find it awkward that the sqlite and postgres databases are now using different versions, I would bump the version for sqlite too just to stay consistent.

@codecov-commenter
Copy link

codecov-commenter commented Apr 20, 2021

Codecov Report

Merging #1778 (1ff9b7b) into master (e092677) will increase coverage by 0.01%.
The diff coverage is 95.23%.

@@            Coverage Diff             @@
##           master    #1778      +/-   ##
==========================================
+ Coverage   89.27%   89.29%   +0.01%     
==========================================
  Files         144      144              
  Lines       10903    10923      +20     
  Branches      475      458      -17     
==========================================
+ Hits         9734     9754      +20     
  Misses       1169     1169              
Impacted Files Coverage Δ
...main/scala/fr/acinq/eclair/db/jdbc/JdbcUtils.scala 95.91% <75.00%> (+0.08%) ⬆️
...c/main/scala/fr/acinq/eclair/db/pg/PgAuditDb.scala 98.27% <95.45%> (-0.83%) ⬇️
...ain/scala/fr/acinq/eclair/db/pg/PgChannelsDb.scala 96.34% <100.00%> (+0.45%) ⬆️
...a/fr/acinq/eclair/db/sqlite/SqliteChannelsDb.scala 95.94% <100.00%> (ø)
...cinq/eclair/blockchain/bitcoind/zmq/ZMQActor.scala 95.00% <0.00%> (+5.00%) ⬆️

@pm47
Copy link
Member Author

pm47 commented Apr 20, 2021

I find it awkward that the sqlite and postgres databases are now using different versions, I would bump the version for sqlite too just to stay consistent.

The reason is that sqlite has not been migrated, it still stores timestamps as INTEGERs. The rationale for not migrating sqlite is that it is much more difficult to do than on postgres (there is no ALTER DATA TYPE) and adds unnecessary risk on our production db. In the future we may significantly change the database format only in Postgres for optimization purposes (e.g. split serialized blobs into several columns), and I think it would make sense to have the version reflect the difference in the schema. Another argument: we could use a totally different backend (e.g. NOSQL) and the versioning would not be the same at all.

Copy link
Member

@t-bast t-bast left a comment

Choose a reason for hiding this comment

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

Ok I can see why having the version numbers diverge between sqlite and postgres make the migration tests quite harder to read...

But I think there's no way around it, we will want to make them evolve independently. I think it's ok to have completely separate migration tests for each DB type in the future. The important tests to share between DB types are the functional tests of the behavior of the latest DB version (e.g. add/remove/list channels for the channels DB).

@@ -52,15 +53,25 @@ class PgAuditDb(implicit ds: DataSource) extends AuditDb with Logging {
statement.executeUpdate("CREATE INDEX relayed_trampoline_payment_hash_idx ON relayed_trampoline(payment_hash)")
}

def migration56(statement: Statement): Unit = {
statement.executeUpdate("ALTER TABLE sent ALTER COLUMN timestamp SET DATA TYPE TIMESTAMP WITH TIME ZONE USING timestamp with time zone 'epoch' + timestamp * interval '1 millisecond'")
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the spaces are very useful, I don't see a good reason to align these statements together.

@pm47
Copy link
Member Author

pm47 commented Apr 22, 2021

🤟

@pm47 pm47 merged commit e14c40d into master Apr 22, 2021
@pm47 pm47 deleted the db-timestamps branch April 22, 2021 08:16
pm47 added a commit that referenced this pull request Jul 5, 2021
For some reason, the payments database was forgotten by #1778.
pm47 added a commit that referenced this pull request Jul 7, 2021
For some reason, the payments database was forgotten by #1778.
pm47 added a commit that referenced this pull request Jul 7, 2021
For some reason, the payments database was forgotten by #1778.
pm47 added a commit that referenced this pull request Jul 8, 2021
For some reason, the payments database was forgotten by #1778.
pm47 added a commit that referenced this pull request Jul 8, 2021
For some reason, the payments database was forgotten by #1778.
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.

4 participants