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

create database migration for the gateway #1768

Merged
merged 4 commits into from
Feb 28, 2024

Conversation

zkokelj
Copy link
Contributor

@zkokelj zkokelj commented Jan 30, 2024

Why this change is needed

In the next task I need to add additional tables to the database.
Currently the database logic for creating desired schema for MariaDB is in github action which restarts the database. Restarting will cause us to lose all stored userIDs and viewingkeys.
In order to prevent that I created this PR which takes the logic from the database and runs .sql migration files on gateway start.

What changes were made as part of this PR

Please provide a high level list of the changes made

PR checks pre-merging

Please indicate below by ticking the checkbox that you have read and performed the required
PR checks

  • PR checks reviewed and performed

Copy link

coderabbitai bot commented Jan 30, 2024

Walkthrough

The updates encompass enhancements to the wallet extension tool, focusing on database integration and migration management. Modifications include the addition of a Dockerfile setup for gateway and migration files, SQL script adjustments for database and user setup, creation of tables with constraints in MariaDB, and enabling foreign keys in SQLite. A new migration mechanism is introduced for applying SQL migrations, alongside refactoring of database-related code to accommodate specific database implementations (MariaDB and SQLite), streamlining the process of database schema management.

Changes

File(s) Change Summary
tools/walletextension/Dockerfile Updated to include copying of gateway executable and .sql migration files.
tools/walletextension/storage/database/001_init.sql
tools/walletextension/storage/database/mariadb/001_init.sql
tools/walletextension/storage/database/sqlite/001_init.sql
Added/updated SQL scripts for database creation, user permissions, and table setup with constraints.
tools/walletextension/storage/database/mariadb/mariadb.go
tools/walletextension/storage/database/sqlite/sqlite.go
Package name changed, added imports, and logic for applying migrations. Refactored to use specific database implementations.
tools/walletextension/storage/database/migration.go Introduced for applying SQL migrations using a specified directory of migration files.
tools/walletextension/storage/storage.go Updated to use specific database implementations (mariadb and sqlite) instead of a generic database package.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@zkokelj zkokelj marked this pull request as ready for review January 31, 2024 07:52
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 42d9a38 and 5f03dc0.
Files selected for processing (7)
  • .github/workflows/manual-deploy-obscuro-gateway-database.yml (1 hunks)
  • tools/walletextension/storage/database/mariadb/001_init.sql (1 hunks)
  • tools/walletextension/storage/database/mariadb/mariadb.go (2 hunks)
  • tools/walletextension/storage/database/migration.go (1 hunks)
  • tools/walletextension/storage/database/sqlite/001_init.sql (1 hunks)
  • tools/walletextension/storage/database/sqlite/sqlite.go (7 hunks)
  • tools/walletextension/storage/storage.go (2 hunks)
Additional comments: 12
tools/walletextension/storage/database/sqlite/001_init.sql (3)
  • 1-2: Enabling foreign keys is a good practice for relational integrity. However, ensure that all related operations in your application are designed to handle cascading effects properly.
  • 5-8: The users table definition looks correct. Using binary(20) for user_id and binary(32) for private_key is appropriate for fixed-size data, ensuring efficient storage.
  • 11-15: The accounts table correctly establishes a foreign key relationship with the users table, which is good for maintaining data integrity. Ensure that the ON DELETE CASCADE option aligns with your application's data management policies.
tools/walletextension/storage/database/mariadb/001_init.sql (4)
  • 1-3: The comment provides clear context for the purpose of this migration file. It's good practice to include such comments for maintainability.
  • 6-6: Granting specific permissions to a user is a secure practice. However, ensure that the 'obscurouser' has the minimum necessary permissions for its role to adhere to the principle of least privilege.
  • 11-15: The users table definition in MariaDB uses varbinary for user_id and private_key, which is consistent with the SQLite version. This consistency is good for maintaining similar data types across different database systems.
  • 17-23: The accounts table structure is consistent with the SQLite version, including the foreign key relationship. This consistency is beneficial for cross-database compatibility.
tools/walletextension/storage/storage.go (2)
  • 6-7: The imports have been correctly updated to reflect the new package structure for mariadb and sqlite. This change aligns with the PR's objective to use specific database implementations.
  • 24-26: The New function has been updated to use the new constructors mariadb.NewMariaDB and sqlite.NewSqliteDatabase. This change correctly reflects the updated package structure and function signatures.
tools/walletextension/storage/database/mariadb/mariadb.go (1)
  • 26-36: The logic to determine the migrations directory and apply migrations is correctly implemented. However, ensure that the migrations directory path is correctly resolved in all deployment environments.
tools/walletextension/storage/database/sqlite/sqlite.go (1)
  • 36-44: The approach to apply migrations in the SQLite database implementation is consistent with the MariaDB implementation. This consistency is good, but again, ensure the migrations directory path is correctly resolved across environments.
.github/workflows/manual-deploy-obscuro-gateway-database.yml (1)
  • 109-114: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-1]

The removal of the volume mount for the 001_init.sql file in the GitHub Actions workflow aligns with the PR's objective to manage database migrations through application startup rather than at the database container initialization. This change supports the new migration strategy.

@ten-protocol ten-protocol deleted a comment from coderabbitai bot Jan 31, 2024
@ten-protocol ten-protocol deleted a comment from coderabbitai bot Jan 31, 2024
@ten-protocol ten-protocol deleted a comment from coderabbitai bot Jan 31, 2024
Copy link
Collaborator

@BedrockSquirrel BedrockSquirrel left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5f03dc0 and 744aa26.
Files selected for processing (2)
  • tools/walletextension/storage/database/mariadb/mariadb.go (2 hunks)
  • tools/walletextension/storage/database/migration.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • tools/walletextension/storage/database/mariadb/mariadb.go
  • tools/walletextension/storage/database/migration.go

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5f03dc0 and da1619b.
Files selected for processing (1)
  • tools/walletextension/Dockerfile (1 hunks)
Additional comments: 2
tools/walletextension/Dockerfile (2)
  • 58-58: The command to copy the gateway executable is correctly placed and uses the --from=build-wallet directive to ensure that the executable is copied from the appropriate build stage. This is a good practice as it ensures that only the necessary files are included in the final image, keeping it lightweight.
  • 62-62: Copying the .sql migration files from the build-wallet stage to the final image is essential for applying database migrations at the gateway's startup. This change is aligned with the PR's objectives. However, ensure that the directory structure in the final image (/home/obscuro/go-obscuro/tools/walletextension/storage/database) is correctly set up to be accessed by the application, and that permissions issues are considered, especially if the application runs as a non-root user.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between da1619b and 8ee83c4.
Files selected for processing (1)
  • tools/walletextension/storage/database/migration.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tools/walletextension/storage/database/migration.go

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 22b397d and 3e9ddc0.
Files selected for processing (8)
  • tools/walletextension/Dockerfile (1 hunks)
  • tools/walletextension/storage/database/001_init.sql (1 hunks)
  • tools/walletextension/storage/database/mariadb/001_init.sql (1 hunks)
  • tools/walletextension/storage/database/mariadb/mariadb.go (2 hunks)
  • tools/walletextension/storage/database/migration.go (1 hunks)
  • tools/walletextension/storage/database/sqlite/001_init.sql (1 hunks)
  • tools/walletextension/storage/database/sqlite/sqlite.go (7 hunks)
  • tools/walletextension/storage/storage.go (2 hunks)
Files skipped from review as they are similar to previous changes (7)
  • tools/walletextension/Dockerfile
  • tools/walletextension/storage/database/mariadb/001_init.sql
  • tools/walletextension/storage/database/mariadb/mariadb.go
  • tools/walletextension/storage/database/migration.go
  • tools/walletextension/storage/database/sqlite/001_init.sql
  • tools/walletextension/storage/database/sqlite/sqlite.go
  • tools/walletextension/storage/storage.go
Additional comments: 3
tools/walletextension/storage/database/001_init.sql (3)
  • 1-3: The addition of a comment block explaining the purpose of the SQL script is a good practice. It enhances the maintainability and readability of the code by providing context to future developers or maintainers.
  • 5-5: The CREATE DATABASE ogdb; command is straightforward and correctly formatted. However, it's important to ensure that this script is executed in a controlled environment, as it assumes the database ogdb does not already exist. Attempting to create a database that already exists without checking can lead to errors.

Consider adding a conditional check to ensure the database does not already exist or handling the potential error in a way that does not disrupt the migration process.

  • 5-5: Using USE ogdb; immediately after creating the database is correct for setting the context for subsequent operations within the same script. This ensures that the permissions are granted to the newly created database.


CREATE DATABASE ogdb;
Copy link

Choose a reason for hiding this comment

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

The GRANT SELECT, INSERT, UPDATE, DELETE ON ogdb.* TO 'obscurouser'; command correctly assigns the necessary permissions for the specified user on the ogdb database. However, it's crucial to ensure that 'obscurouser' is a placeholder or a generic username for demonstration purposes. In production environments, using more descriptive and secure usernames is recommended to avoid potential security risks.

Consider replacing 'obscurouser' with a more descriptive and secure username, especially if this script is intended for use in production environments.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3e9ddc0 and e3e4a2d.
Files selected for processing (2)
  • tools/walletextension/storage/database/001_init.sql (1 hunks)
  • tools/walletextension/storage/database/mariadb/001_init.sql (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • tools/walletextension/storage/database/001_init.sql
  • tools/walletextension/storage/database/mariadb/001_init.sql

Copy link
Collaborator

@BedrockSquirrel BedrockSquirrel left a comment

Choose a reason for hiding this comment

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

lgtm

@zkokelj zkokelj force-pushed the ziga/gateway_database_migration branch from a84f003 to 4e632a9 Compare February 14, 2024 08:54
@zkokelj zkokelj force-pushed the ziga/gateway_database_migration branch 6 times, most recently from a4e611d to 693721d Compare February 21, 2024 17:42
@zkokelj zkokelj force-pushed the ziga/gateway_database_migration branch from 245ffc4 to 90073c8 Compare February 22, 2024 15:01
@ten-protocol ten-protocol deleted a comment from coderabbitai bot Feb 22, 2024
@zkokelj zkokelj merged commit b17c332 into main Feb 28, 2024
4 checks passed
@zkokelj zkokelj deleted the ziga/gateway_database_migration branch February 28, 2024 13:39
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.

3 participants