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

feat: migrate to turso #202

Merged
merged 6 commits into from
Mar 15, 2024
Merged

feat: migrate to turso #202

merged 6 commits into from
Mar 15, 2024

Conversation

arunanshub
Copy link
Owner

@arunanshub arunanshub commented Mar 15, 2024

  • feat(db): migrate to sqlite codebase
  • build(deps-dev): update dependencies
  • fix(migrations): update migrations
  • fix(crud): ignore bogus type errors
  • build: use new turso based config

Summary by CodeRabbit

  • New Features

    • Switched database driver to improve performance and compatibility.
    • Enhanced database migration process with additional security checks.
  • Bug Fixes

    • Fixed issues related to database operations by updating data types and constraints.
  • Refactor

    • Updated database connection details and methods for enhanced efficiency and security.
    • Revised database seeding process for more robust data insertion.
  • Chores

    • Added necessary comments and updated configurations to align with new database requirements.

Copy link

vercel bot commented Mar 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
distrohop ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 15, 2024 0:35am

Copy link

coderabbitai bot commented Mar 15, 2024

Walkthrough

The project has transitioned from using the mysql2 database driver to turso, affecting the database configuration, migrations, and schema definitions across various files. This includes changes in database connection details, the addition of authentication token checks, and updates from MySQL to SQLite data types and table creation parameters. Additionally, error handling for missing environment variables and type expectation adjustments in CRUD operations have been implemented.

Changes

Files Change Summary
drizzle.config.ts Switched database driver to turso, updated connection details, added environment variable checks.
migrate.ts, .../middleware/0.db.ts Added authentication token checks for database operations.
nuxt.config.ts Added turso configuration with authToken.
src/server/crud/... Added // @ts-expect-error comments, updated CRUD functions with new database queries.
src/server/database/db.ts Updated client creation method and added authentication token in getDb options.
src/server/database/migrations/... Introduced new SQL migration scripts for various tables.
src/server/database/schema/... Updated schemas from MySQL to SQLite, including data types and table creation parameters.
src/server/database/seed.ts Updated seeding process with authentication token and new insertion methods.

🐇✨
In the land of code and byte,
Changes bloom under the moonlight.
From MySQL's embrace, we take flight,
To SQLite's realm, with futures bright.
🌟📚 With turso as our guiding light.
🐾🥕 Cheers, to a transition so right!

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.
    • Open a follow-up GitHub issue for this discussion.
  • 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 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.

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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5cabd11 and fd402f6.
Files ignored due to path filters (4)
  • package.json is excluded by: !**/*.json
  • pnpm-lock.yaml is excluded by: !**/*.yaml
  • src/server/database/migrations/meta/0000_snapshot.json is excluded by: !**/*.json
  • src/server/database/migrations/meta/_journal.json is excluded by: !**/*.json
Files selected for processing (15)
  • drizzle.config.ts (1 hunks)
  • migrate.ts (1 hunks)
  • nuxt.config.ts (1 hunks)
  • src/server/crud/answer.ts (2 hunks)
  • src/server/crud/question.ts (2 hunks)
  • src/server/crud/section.ts (1 hunks)
  • src/server/database/db.ts (1 hunks)
  • src/server/database/migrations/0000_famous_ultimo.sql (1 hunks)
  • src/server/database/schema/answers.ts (2 hunks)
  • src/server/database/schema/distributions.ts (1 hunks)
  • src/server/database/schema/questions.ts (1 hunks)
  • src/server/database/schema/results.ts (2 hunks)
  • src/server/database/schema/sections.ts (1 hunks)
  • src/server/database/seed.ts (4 hunks)
  • src/server/middleware/0.db.ts (1 hunks)
Additional comments: 25
src/server/middleware/0.db.ts (2)
  • 13-14: Ensure that databaseUrl and turso.authToken are always defined in the runtime configuration. It might be beneficial to add error handling or default values if these configurations are not set to prevent runtime errors.
  • 16-16: The use of getDb with { url: databaseUrl, authToken } is a good practice as it encapsulates the database connection details. However, consider verifying the connection or handling potential errors that could arise from an unsuccessful connection attempt.
drizzle.config.ts (1)
  • 13-16: Switching the database driver to 'turso' and updating the dbCredentials to include url and authToken is aligned with the migration objectives. Ensure that all other parts of the application that rely on the database connection are updated to use these new credentials.
src/server/database/schema/distributions.ts (1)
  • 1-12: The migration from drizzle-orm/mysql-core to drizzle-orm/sqlite-core and the change of field types from varchar to text are well-executed. Ensure that these changes are reflected in all related database operations and that the length specifications for text fields are compatible with SQLite's limitations.
migrate.ts (1)
  • 12-16: Including an authentication token check before running migrations is a good security practice. Ensure that the migrate function's error handling is robust, as indicated by the catch block, and consider logging more detailed information about the error to aid in troubleshooting.
src/server/database/schema/sections.ts (1)
  • 5-10: The update from MySQL to SQLite, including changing column types from varchar to text, is consistent with the migration objectives. Ensure that these changes do not affect the application's functionality and that all related CRUD operations are tested with the new schema.
src/server/database/db.ts (2)
  • 9-12: The addition of authToken to the Options interface is a necessary update for the migration to the 'turso' driver. Ensure that all uses of getDb throughout the application are updated to include the authToken.
  • 14-16: Creating a new database client with createClient and passing the options including authToken is a good practice. Ensure that error handling is in place for potential connection issues or misconfigurations.
src/server/database/schema/questions.ts (1)
  • 6-19: The migration of the questions table schema to SQLite-specific types and the adjustments in default values and constraints are well-executed. Ensure that these changes are thoroughly tested, especially the boolean representation with integer and the cascade behavior on foreign keys.
src/server/database/schema/answers.ts (2)
  • 5-12: The migration from MySQL to SQLite and the use of sqliteTable along with data type changes from char to text for the id, msgid, and questionId fields are correctly implemented. The use of crypto.randomUUID() as a default value for id is appropriate for generating unique identifiers in SQLite. The cascading options for foreign key references are also correctly set, ensuring referential integrity.
  • 29-38: The changes to the _answers_blocked table, including the migration to SQLite, data type adjustments for answerId and blockedByAnswerId, and the correct setup of foreign key references with cascading options, are correctly implemented. The use of a composite primary key (pk) for the relationship between blockedByAnswerId and answerId is appropriate for ensuring uniqueness and referential integrity.
src/server/database/schema/results.ts (3)
  • 5-11: The migration of the results table to SQLite, including the change of the id column to text and the createdAt column to integer with a default value of CURRENT_TIMESTAMP, is correctly implemented. The use of crypto.randomUUID() for generating unique identifiers and the correct data type for timestamps align with SQLite's requirements.
  • 14-23: The migration of the _selected_answers table to SQLite, including the adjustments for resultId and selectedAnswerId columns to text and the correct setup of foreign key references with cascading options, is correctly implemented. The use of a composite primary key is appropriate for ensuring uniqueness and referential integrity.
  • 36-45: The migration of the _important_answers table to SQLite, including the adjustments for resultId and importantAnswerId columns to text and the correct setup of foreign key references with cascading options, is correctly implemented. The use of a composite primary key is appropriate for ensuring uniqueness and referential integrity.
src/server/crud/answer.ts (2)
  • 31-31: The use of // @ts-expect-error before the await getAnswerByMsgid call in the createAnswer function indicates an expected type error. It's important to ensure that this is a temporary workaround and that the underlying type issues are addressed in the future. Consider adding a comment explaining the specific type error expected and any plans for resolution.
  • 43-43: Similarly, the use of // @ts-expect-error before the await getAnswersByMsgids call in the createAnswers function should be accompanied by a comment explaining the specific type error expected and any plans for resolution. This helps maintain code clarity and aids in future refactoring efforts to address the type issues.
src/server/database/seed.ts (5)
  • 14-18: The addition of an authentication token (authToken) alongside the database URL for the database connection setup is a significant change. Ensure that the authToken is securely managed and not exposed in any logs or error messages. This change aligns with the migration to the 'turso' database driver and its authentication requirements.
  • 23-23: The updated insertion logic for distributions using the spread operator ({ ...distro }) is a straightforward and effective way to insert distribution records into the database. Ensure that the distro object structure aligns with the distributions table schema in SQLite.
  • 37-37: The use of // @ts-expect-error for the type assertion in the createQuestion call within the seeding process should be accompanied by a comment explaining the specific type error expected and any plans for resolution. This helps maintain code clarity and aids in future refactoring efforts to address the type issues.
  • 44-44: Similarly, the use of // @ts-expect-error for the type assertion in the createAnswers call within the seeding process should be accompanied by a comment explaining the specific type error expected and any plans for resolution. This helps maintain code clarity and aids in future refactoring efforts to address the type issues.
  • 78-81: The addition of error handling for the main function with process.exit(1) ensures that the seeding process exits with an error code if an exception occurs. This is a good practice for identifying issues during the seeding process and ensuring that errors are not silently ignored.
src/server/crud/question.ts (2)
  • 58-58: The addition of a call to getQuestionByMsgid after inserting a question in the createQuestion function is a good practice. It ensures that the newly created question, along with its related data, is returned to the caller. This approach enhances the function's usability by providing immediate feedback on the operation's result.
  • 71-71: Similarly, the addition of a call to getQuestionsByMsgids after inserting multiple questions in the createQuestions function is beneficial. It allows the function to return the newly created questions, providing immediate feedback on the operation's result. This enhances the usability and consistency of the CRUD operations.
nuxt.config.ts (1)
  • 109-111: The addition of the turso configuration under runtimeConfig with an authToken field is correctly implemented. This configuration is essential for the migration to the 'turso' database driver and its authentication requirements. Ensure that the authToken is securely managed and not exposed in any logs or error messages. This change aligns with the project's migration objectives and enhances its security posture.
src/server/database/migrations/0000_famous_ultimo.sql (1)
  • 1-78: The SQL migration scripts for creating tables and unique indexes are correctly adapted for SQLite. The use of text data type for identifiers and the setup of foreign key references with cascading options are appropriate for SQLite's requirements. The creation of unique indexes for msgid columns in answers, questions, and sections tables ensures data integrity and uniqueness. Ensure that all references and data types align with the project's schema requirements and SQLite's limitations.

drizzle.config.ts Outdated Show resolved Hide resolved
migrate.ts Show resolved Hide resolved
src/server/crud/section.ts Outdated Show resolved Hide resolved
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 fd402f6 and 8427b8d.
Files selected for processing (3)
  • drizzle.config.ts (1 hunks)
  • migrate.ts (1 hunks)
  • src/server/crud/section.ts (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • drizzle.config.ts
  • migrate.ts
  • src/server/crud/section.ts

@arunanshub arunanshub merged commit 1809e9c into master Mar 15, 2024
4 checks passed
@arunanshub arunanshub deleted the feat/migrate-to-turso branch March 15, 2024 12:40
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.

1 participant