fix(create-medusa-app): fix inconsistency in checking errors in migrations (#5189)

This commit is contained in:
Shahed Nasser
2023-09-29 15:26:24 +03:00
committed by GitHub
parent 1e7db5a5cb
commit 18a05dee86
6 changed files with 48 additions and 12 deletions

View File

@@ -0,0 +1,5 @@
---
"create-medusa-app": patch
---
fix(create-medusa-app): fix inconsistency in checking errors in migrations

View File

@@ -103,7 +103,7 @@ export default async ({
!skipDb && migrations ? await askForAdminEmail(seed, boilerplate) : ""
const installNextjs = withNextjsStarter || (await askForNextjsStarter())
const { client, dbConnectionString } = !skipDb
let { client, dbConnectionString } = !skipDb
? await getDbClientAndCredentials({
dbName,
dbUrl,
@@ -153,7 +153,7 @@ export default async ({
...factBoxOptions,
title: "Creating database...",
})
await runCreateDb({ client, dbName, spinner })
client = await runCreateDb({ client, dbName, spinner })
factBoxOptions.interval = displayFactBox({
...factBoxOptions,
@@ -179,6 +179,7 @@ export default async ({
migrations,
onboardingType: installNextjs ? "nextjs" : "default",
nextjsDirectory,
client,
})
} catch (e: any) {
if (isAbortError(e)) {

View File

@@ -24,13 +24,23 @@ export async function runCreateDb({
client: pg.Client
dbName: string
spinner: Ora
}) {
// create postgres database
}): Promise<pg.Client> {
let newClient = client
try {
// create postgres database
await createDb({
client,
db: dbName,
})
// create a new connection with database selected
await client.end()
newClient = await postgresClient({
user: client.user,
password: client.password,
database: dbName,
})
} catch (e) {
spinner.stop()
logMessage({
@@ -38,6 +48,8 @@ export async function runCreateDb({
type: "error",
})
}
return newClient
}
async function getForDbName(dbName: string): Promise<{

View File

@@ -5,6 +5,7 @@ type PostgresConnection = {
user?: string
password?: string
connectionString?: string
database?: string
}
export default async (connect: PostgresConnection) => {

View File

@@ -7,6 +7,7 @@ import { EOL } from "os"
import { displayFactBox, FactBoxOptions } from "./facts.js"
import ProcessManager from "./process-manager.js"
import { clearProject } from "./clear-project.js"
import type { Client } from "pg"
type PrepareOptions = {
directory: string
@@ -23,6 +24,7 @@ type PrepareOptions = {
migrations?: boolean
onboardingType?: "default" | "nextjs"
nextjsDirectory?: string
client: Client | null
}
export default async ({
@@ -38,6 +40,7 @@ export default async ({
migrations,
onboardingType = "default",
nextjsDirectory = "",
client,
}: PrepareOptions) => {
// initialize execution options
const execOptions = {
@@ -145,13 +148,27 @@ export default async ({
npxOptions
)
// ensure that migrations actually ran in case of an uncaught error
if (!proc.stdout.includes("Migrations completed")) {
throw new Error(
`An error occurred while running migrations: ${
proc.stderr || proc.stdout
}`
)
if (client) {
// check the migrations table is in the database
// to ensure that migrations ran
let errorOccurred = false
try {
const migrations = await client.query(`SELECT * FROM "migrations"`)
errorOccurred = migrations.rowCount == 0
} catch (e) {
// avoid error thrown if the migrations table
// doesn't exist
errorOccurred = true
}
// ensure that migrations actually ran in case of an uncaught error
if (errorOccurred) {
throw new Error(
`An error occurred while running migrations: ${
proc.stderr || proc.stdout
}`
)
}
}
},
})

View File

@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"module": "Node16",
"moduleResolution": "node16",
"outDir": "./dist",
"esModuleInterop": true,