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

feature: add db:create command #8760

Merged
merged 11 commits into from
Aug 26, 2024
Merged

feature: add db:create command #8760

merged 11 commits into from
Aug 26, 2024

Conversation

thetutlage
Copy link
Contributor

@thetutlage thetutlage commented Aug 26, 2024

FIXES: FRMW-2664

This PR introduces a new command called db:create. The command is meant to create the database (if it is missing) and update the .env file after the operation has been successful.

Important

This command does not boot the application nor read the medusa-config.js file. The db:create command should work regardless of the state of the application. Otherwise, we might run into issues where a loader wants to establish the DB connection, but the database does not exist.

How it works?

This is how the command works under the hood.

  • The command reads the .env file using the EnvEditor and looks for DATABASE_URL and DB_NAME environment variables.
  • The command will exit if the DATABASE_URL is missing. We need the DATABASE_URL to make the database connection.
  • The user is prompted to specify the database name with a default value. The default value is set to the DB_NAME environment variable and falls back to the project directory name.
  • Finally, taking all this info. The following steps are performed.
    • Establish a connection with the database server.
    • Check if the database already exists
    • If not, create the database
    • Update the .env file.

Newly introduced utilities

The following utilities have been introduced to the @medusajs/utils package.

EnvEditor

It reads the .env file as a text file and exposes the API to edit the values inside it and persist them on the disk.

parseConnectionString

We need to parse the PG connection string and convert it into an object to safely remove the database name from the DATABASE_URL value. We need to remove the database name because we want to establish the connection without selecting this database since it might not exist in the first place.

createClient

Creates the PostgreSQL client instance (Not the Mikro ORM)

createDb

Creates the database

dbExists

Check if the database exists.

Copy link

changeset-bot bot commented Aug 26, 2024

⚠️ No Changeset found

Latest commit: 35b8b0f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Aug 26, 2024

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

Name Status Preview Comments Updated (UTC)
medusa-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 26, 2024 9:21am
6 Skipped Deployments
Name Status Preview Comments Updated (UTC)
api-reference ⬜️ Ignored (Inspect) Aug 26, 2024 9:21am
api-reference-v2 ⬜️ Ignored (Inspect) Visit Preview Aug 26, 2024 9:21am
docs-ui ⬜️ Ignored (Inspect) Visit Preview Aug 26, 2024 9:21am
docs-v2 ⬜️ Ignored (Inspect) Visit Preview Aug 26, 2024 9:21am
medusa-docs ⬜️ Ignored (Inspect) Visit Preview Aug 26, 2024 9:21am
resources-docs ⬜️ Ignored (Inspect) Visit Preview Aug 26, 2024 9:21am

Copy link
Contributor

@olivermrbl olivermrbl left a comment

Choose a reason for hiding this comment

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

Clean PR, LGTM

packages/core/utils/package.json Outdated Show resolved Hide resolved
packages/medusa/src/commands/db/create.ts Show resolved Hide resolved
packages/medusa/src/commands/db/create.ts Outdated Show resolved Hide resolved
packages/medusa/src/commands/db/create.ts Show resolved Hide resolved
packages/medusa/src/commands/db/create.ts Outdated Show resolved Hide resolved
packages/cli/medusa-cli/src/create-cli.ts Outdated Show resolved Hide resolved
packages/cli/medusa-cli/src/create-cli.ts Outdated Show resolved Hide resolved
@thetutlage thetutlage merged commit 7c2cfc1 into develop Aug 26, 2024
23 checks passed
@thetutlage thetutlage deleted the feat/add-db-create-command branch August 26, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants