Skip to content

feat(config): Automatically create directory for SQLite database - #158

Merged
iFurySt merged 1 commit into
AmoyLab:mainfrom
kerwin612:patch-1
Jun 26, 2025
Merged

iFurySt merged 1 commit into
AmoyLab:mainfrom
kerwin612:patch-1

Conversation

@kerwin612

@kerwin612 kerwin612 commented Jun 24, 2025 •

Copy link
Copy Markdown
Contributor

https://github.com/AmoyLab/docs/blob/f4df89dc3069e78a6d7cc247494ff43806ef7a6d/docs/development/local-development.md?plain=1#L59-L61

APISERVER_DB_NAME=./data/unla.db

Currently, when the application is configured to use SQLite, it fails to start if the directory for the database file (e.g., data/unla.db) does not exist. The application is unable to create the database file, and the user must manually create the parent directory (data/) to resolve the issue.

To improve usability and adhere to the "fail-fast" principle, this PR enhances the database configuration logic.

Within the (c *DatabaseConfig) GetDSN() function, for the sqlite case, the application now ensures the parent directory for the database file exists by attempting to create it with os.MkdirAll. If directory creation fails (e.g., due to a permissions issue), the application will panic immediately with a descriptive error message. This prevents a more obscure error from appearing later when the database driver attempts to open the file.

Summary by Sourcery

Enhancements:

  • Automatically create the parent directory for the SQLite database file in the configuration to prevent obscure startup errors and enforce fail-fast behavior

@sourcery-ai

sourcery-ai Bot commented Jun 24, 2025 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

In GetDSN(), the sqlite branch now auto-creates the parent directory for the database file using os.MkdirAll and immediately panics with a clear error if directory creation fails, preventing obscure file-open errors later.

Sequence diagram for SQLite DSN retrieval with directory creation

sequenceDiagram
    participant App as Application
    participant Config as DatabaseConfig
    participant OS as Operating System
    App->>Config: GetDSN()
    Config->>OS: MkdirAll(parentDir(DBName), 0755)
    alt Directory creation succeeds
        Config-->>App: Return DBName
    else Directory creation fails
        Config-->>App: Panic with error
    end
Loading

Class diagram for updated DatabaseConfig.GetDSN() logic

classDiagram
    class DatabaseConfig {
        +string DBType
        +string DBName
        +GetDSN() string
    }
    DatabaseConfig : +GetDSN() now creates parent dir for SQLite
    DatabaseConfig : +GetDSN() panics if MkdirAll fails
Loading

File-Level Changes

Change Details Files
Auto-create parent directory for SQLite database file
  • Invoke os.MkdirAll on filepath.Dir(c.DBName) with 0755 permissions
  • Panic with a descriptive error message upon MkdirAll failure
  • Preserve existing return of DBName for SQLite DSN
internal/common/config/apiserver.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @kerwin612 - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@iFurySt iFurySt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Thanks!

@iFurySt
iFurySt merged commit bbb4887 into AmoyLab:main Jun 26, 2025
@kerwin612
kerwin612 deleted the patch-1 branch June 26, 2025 06:22
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