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

PostgreSQL storage #50

Closed
wants to merge 22 commits into from
Closed

PostgreSQL storage #50

wants to merge 22 commits into from

Conversation

sheshenia
Copy link
Contributor

PostgreSQL storage added.
Based on mysql storage package. Much of the code is duplicated, but as Go-authors said, sometimes a little more copy-paste is better than a little more dependencies.
github.com/lib/pq - is used as db driver.

@sheshenia sheshenia marked this pull request as ready for review May 27, 2022 21:09
@sheshenia
Copy link
Contributor Author

PostgreSQL integration test results
photo_2022-06-01_12-04-11
photo_2022-06-01_12-07-05
photo_2022-06-01_12-10-28 (2)
photo_2022-06-01_12-11-19
photo_2022-06-01_12-13-06
photo_2022-06-01_12-13-54

Copy link
Member

@jessepeterson jessepeterson left a comment

Choose a reason for hiding this comment

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

Thanks for this! Overall looks good, but there are a few changes requested. In general I'm fine with the copy/paste, so no worries there.

A few questions/requests:

  • Could we rename the module to pgsql from postgresql (and same with the backend name for the CLI)?
  • Could you add docs to the docs/operations-guide.md file?
  • Could you add in the deleter functionality from Optionally delete commands in mysql storage backend #48? (This will get merged soon)
  • Thanks for running the integration tests and adding screenshots, that's helpful!
  • Have you run through actual tests with "real" devices and such? There's also mdmb if you want to play with that.

Again thanks for this!

README.md Outdated Show resolved Hide resolved
}

// AssociateCertHash
// TODO primary key consists of two to cols: id & sha256
Copy link
Member

Choose a reason for hiding this comment

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

I think the reason for this in the mysql backend was to have a way to ignore/bypass errors if the insert already exists. I can't think of a good reason why we're updating sha256 to the same value it already is. I.e. so either an insert or update will succeed silently. Perhaps the better thing is to just insert and check for the the duplicate error (and ignore it)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The solution for PostgreSQL is:
ON CONFLICT ON CONSTRAINT cert_auth_associations_pkey DO NOTHING

Copy link
Contributor Author

Choose a reason for hiding this comment

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

INSERT IGNORE - possible solution for MySQL. What do you think?

serial_number = EXCLUDED.serial_number,
authenticate = EXCLUDED.authenticate,
authenticate_at = CURRENT_TIMESTAMP;`,
r.ID, nullEmptyString(string(pemCert)), nullEmptyString(msg.SerialNumber), msg.Raw,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
r.ID, nullEmptyString(string(pemCert)), nullEmptyString(msg.SerialNumber), msg.Raw,
r.ID, pemCert, nullEmptyString(msg.SerialNumber), msg.Raw,

Does pemCert need to be cast to string? For the MySQL driver the nil/zero-value []byte gets converted into a NULL column.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, pg driver converts nil bytes slice into an empty string, but not NULL. Tested additionally this functionality. So casting here is the simplest solution.

storage/postgresql/postgresql.go Outdated Show resolved Hide resolved
storage/postgresql/postgresql.go Outdated Show resolved Hide resolved
storage/postgresql/schema.sql Outdated Show resolved Hide resolved
storage/postgresql/schema.sql Outdated Show resolved Hide resolved
enabled BOOLEAN NOT NULL DEFAULT TRUE,
token_update_tally INTEGER NOT NULL DEFAULT 1,

last_seen_at TIMESTAMP NOT NULL, -- DEFAULT CURRENT_TIMESTAMP, tests pass, but real test push error
Copy link
Member

Choose a reason for hiding this comment

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

real test push error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I've just started working, I tried to test with MySQL DB. And after connecting with iPhone, I've received such log message:
while pushing got error: handler=checkin-command msg=check-in request err=tokenupdate service: Error 1364: Field 'last_seen_at' doesn't have a default value
After forking I've changed in MySQL schema:
last_seen_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
and the real device worked fine. I've forgotten about this, copied MySQL package, and started working on PostgreSQL.
After finishing, I started integration tests, but in MySQL they failed because of DEFAULT CURRENT_TIMESTAMP.
I rolled back to the original in MySQL - and integration tests passed ok.
I think such a situation needs some investigation :))

storage/postgresql/schema.sql Outdated Show resolved Hide resolved
storage/postgresql/schema.sql Outdated Show resolved Hide resolved
sheshenia and others added 3 commits June 3, 2022 15:36
Co-authored-by: Jesse Peterson <jessepeterson@users.noreply.github.com>
Co-authored-by: Jesse Peterson <jessepeterson@users.noreply.github.com>
Co-authored-by: Jesse Peterson <jessepeterson@users.noreply.github.com>
@sheshenia
Copy link
Contributor Author

Thanks, Jesse for your review! I'll do the requested changes.

sheshenia and others added 3 commits June 4, 2022 14:13
Co-authored-by: Jesse Peterson <jessepeterson@users.noreply.github.com>
@sheshenia
Copy link
Contributor Author

I've done the requested changes, except "deleter" functionality.
Can I ask if "deleter" in MySQL storage is tested and works as expected? Because after implementing it in pgsql I have some difficulties. My integration tests fail. So I'm debugging and trying to find where is the problem.
Thanks!

@sheshenia
Copy link
Contributor Author

delete_pgsql_test

@sheshenia sheshenia requested a review from jessepeterson June 7, 2022 15:58
@sheshenia
Copy link
Contributor Author

The latest commit includes the "deleter" functionality of pgsql, and MySQL from #48
Also all previous remarks.
Integration tests pass ok. Also tested with the real device in "multistorage" mode. "Live" comparing tables and content of MySQL with PostgreSQL, after sending commands using cmdr.py

@sheshenia sheshenia closed this Jun 21, 2022
@jessepeterson jessepeterson mentioned this pull request Jul 15, 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