-
-
Notifications
You must be signed in to change notification settings - Fork 563
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
Support Postgres as a persistence backend #8202
Merged
etorreborre
merged 1 commit into
develop
from
etorreborre/feat/sqlx-any-remove-sqlx-type
Jun 28, 2024
Merged
Support Postgres as a persistence backend #8202
etorreborre
merged 1 commit into
develop
from
etorreborre/feat/sqlx-any-remove-sqlx-type
Jun 28, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
etorreborre
force-pushed
the
etorreborre/feat/sqlx-any-remove-sqlx-type
branch
23 times, most recently
from
June 26, 2024 13:10
d5ae7af
to
78fffa8
Compare
implementations/rust/ockam/ockam_vault/src/storage/secrets_repository_sql.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_vault/src/storage/secrets_repository_sql.rs
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_api/src/kafka/portal_worker.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_vault/src/storage/secrets_repository_sql.rs
Show resolved
Hide resolved
implementations/rust/ockam/ockam_api/src/cli_state/storage/journeys_repository_sql.rs
Show resolved
Hide resolved
.../storage/database/migrations/node_migrations/sql/postgres/20240613100000_create_database.sql
Show resolved
Hide resolved
Nice! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! I pray to the gods that we never have to support oracle 😄
implementations/rust/ockam/ockam_api/src/cli_state/storage/vaults_repository_sql.rs
Show resolved
Hide resolved
implementations/rust/ockam/ockam_command/src/environment/static/env_info.txt
Show resolved
Hide resolved
etorreborre
force-pushed
the
etorreborre/feat/sqlx-any-remove-sqlx-type
branch
from
June 28, 2024 09:42
c23fd8b
to
1c4cfc0
Compare
SanjoDeundiak
previously approved these changes
Jun 28, 2024
etorreborre
force-pushed
the
etorreborre/feat/sqlx-any-remove-sqlx-type
branch
from
June 28, 2024 13:22
a7a17bd
to
859e94d
Compare
etorreborre
force-pushed
the
etorreborre/feat/sqlx-any-remove-sqlx-type
branch
from
June 28, 2024 13:54
859e94d
to
4e2ed06
Compare
SanjoDeundiak
approved these changes
Jun 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the possibility to use Postgres as a backend to persist data for an Ockam node.
In order to use a Postgres database simply set the following environment variables:
OCKAM_POSTGRES_HOST
: for examplelocalhost
OCKAM_POSTGRES_PORT
: for example5432
OCKAM_POSTGRES_DATABASE_NAME
(optional): for examplepostgres
OCKAM_POSTGRES_USER
(optional): for exampleadmin
OCKAM_POSTGRES_PASSWORD
(optional): for examplepassword
Implementation notes
This PR touches a large number of files and the following sections will help the reviewer understanding the changes.
Driver
The
sqlx
library provides anAny
driver, which replaces theSQLite
driver used before this PR.With the
Any
driver and its associated types,AnyConnection
,AnyPool
,Type<Any>
etc... we can open a connection to either a SQLite or a Postgres database by specifying the proper URL:sqlite:file:in-memory?mode=memory&cache=shared
for an in-memory SQLite databasesqlite:file://my-file?mode=rwc
for an on-disk SQLite databasepostgres://admin:password@localhost:5432/postgres
for a Postgres databaseThis logic is encapsulated in a new
DatabaseConfiguration
type.The result of this driver change is that a number of method signatures had to be changed from
... SQLite ...
to... Any ...
.Queries
Some queries had to be changed in order to work with both databases.
In particular the queries of the form
INSERT OR REPLACE
don't work with Postgres. Those queries have been replaced withINSERT ... ON CONFLICT ... DO UPDATE | NOTHING
.Data types serialization
The mapping of Rust types to database types is quite different between SQLite and Postgres.
To support this properly:
The
ToSqlxType
typeclass has been removed in favor of using theEncode
andType
typeclasses fromsqlx
directly. While this typeclass was useful when using SQLite only, it became a source of encoding bugs with both Postgres and SQLite.Reading boolean and optional values from the database was not working out of the box. This was fixed via:
Boolean
datatype to read a boolean value from a database columnNullable<T>
datatype to read an optional value from a database column possibly containing NULL valuessqlx
library:Bool
type.sqlx
dependencies mentioned in the Cargo files now point to a fork.Tests
Repositories
All the repositories code is now tested with 3 databases: SQLite in-memory, SQLite on-disk and Postgres (if available in the environment). This test support is provided by the
sqlx_database::with_dbs
function.Execution
Note that you need to run the tests sequentially if you run them with a local Postgres database since it will be shared between all tests. For example:
Clean-up
For the same reason, some system tests are now calling
TestNode::clean()
in order to make sure that the database is cleaned-up before the test runs.CI
I have added a CI job to run all the
nextest
test suite with a Postgres database. That job only selects test names containingsql
, to cover all the repositories, andcli_state
to test the high-level behavior which generally involves some persistence.Vaults
The modeling for vaults has changed slightly. An enum has been introduced to represent exactly a vault metadata:
A vault is either stored directly in the database, with some dedicated tables, or in a file (using SQLite).
The naming of
is_kms
has been changed touse_aws_kms
to indicate that:KMS
it is specifically an AWS oneockam vault list/show
to better reflect the vault types:ockam vault show
command also changes. This is not necessarily the best JSON output but I didn't try to improve it for now.