fix(create-medusa-app): fix inconsistency in checking errors in migrations (#5189)
This commit is contained in:
1 parent
1e7db5a5cb
commit
18a05dee86
6 files changed
+42
-6
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"create-medusa-app": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(create-medusa-app): fix inconsistency in checking errors in migrations
|
||||||
@@ -103,7 +103,7 @@ export default async ({
|
|||||||
!skipDb && migrations ? await askForAdminEmail(seed, boilerplate) : ""
|
!skipDb && migrations ? await askForAdminEmail(seed, boilerplate) : ""
|
||||||
const installNextjs = withNextjsStarter || (await askForNextjsStarter())
|
const installNextjs = withNextjsStarter || (await askForNextjsStarter())
|
||||||
|
|
||||||
const { client, dbConnectionString } = !skipDb
|
let { client, dbConnectionString } = !skipDb
|
||||||
? await getDbClientAndCredentials({
|
? await getDbClientAndCredentials({
|
||||||
dbName,
|
dbName,
|
||||||
dbUrl,
|
dbUrl,
|
||||||
@@ -153,7 +153,7 @@ export default async ({
|
|||||||
...factBoxOptions,
|
...factBoxOptions,
|
||||||
title: "Creating database...",
|
title: "Creating database...",
|
||||||
})
|
})
|
||||||
await runCreateDb({ client, dbName, spinner })
|
client = await runCreateDb({ client, dbName, spinner })
|
||||||
|
|
||||||
factBoxOptions.interval = displayFactBox({
|
factBoxOptions.interval = displayFactBox({
|
||||||
...factBoxOptions,
|
...factBoxOptions,
|
||||||
@@ -179,6 +179,7 @@ export default async ({
|
|||||||
migrations,
|
migrations,
|
||||||
onboardingType: installNextjs ? "nextjs" : "default",
|
onboardingType: installNextjs ? "nextjs" : "default",
|
||||||
nextjsDirectory,
|
nextjsDirectory,
|
||||||
|
client,
|
||||||
})
|
})
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
if (isAbortError(e)) {
|
if (isAbortError(e)) {
|
||||||
|
|||||||
@@ -24,13 +24,23 @@ export async function runCreateDb({
|
|||||||
client: pg.Client
|
client: pg.Client
|
||||||
dbName: string
|
dbName: string
|
||||||
spinner: Ora
|
spinner: Ora
|
||||||
}) {
|
}): Promise<pg.Client> {
|
||||||
// create postgres database
|
let newClient = client
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// create postgres database
|
||||||
await createDb({
|
await createDb({
|
||||||
client,
|
client,
|
||||||
db: dbName,
|
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) {
|
} catch (e) {
|
||||||
spinner.stop()
|
spinner.stop()
|
||||||
logMessage({
|
logMessage({
|
||||||
@@ -38,6 +48,8 @@ export async function runCreateDb({
|
|||||||
type: "error",
|
type: "error",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return newClient
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getForDbName(dbName: string): Promise<{
|
async function getForDbName(dbName: string): Promise<{
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ type PostgresConnection = {
|
|||||||
user?: string
|
user?: string
|
||||||
password?: string
|
password?: string
|
||||||
connectionString?: string
|
connectionString?: string
|
||||||
|
database?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (connect: PostgresConnection) => {
|
export default async (connect: PostgresConnection) => {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { EOL } from "os"
|
|||||||
import { displayFactBox, FactBoxOptions } from "./facts.js"
|
import { displayFactBox, FactBoxOptions } from "./facts.js"
|
||||||
import ProcessManager from "./process-manager.js"
|
import ProcessManager from "./process-manager.js"
|
||||||
import { clearProject } from "./clear-project.js"
|
import { clearProject } from "./clear-project.js"
|
||||||
|
import type { Client } from "pg"
|
||||||
|
|
||||||
type PrepareOptions = {
|
type PrepareOptions = {
|
||||||
directory: string
|
directory: string
|
||||||
@@ -23,6 +24,7 @@ type PrepareOptions = {
|
|||||||
migrations?: boolean
|
migrations?: boolean
|
||||||
onboardingType?: "default" | "nextjs"
|
onboardingType?: "default" | "nextjs"
|
||||||
nextjsDirectory?: string
|
nextjsDirectory?: string
|
||||||
|
client: Client | null
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async ({
|
export default async ({
|
||||||
@@ -38,6 +40,7 @@ export default async ({
|
|||||||
migrations,
|
migrations,
|
||||||
onboardingType = "default",
|
onboardingType = "default",
|
||||||
nextjsDirectory = "",
|
nextjsDirectory = "",
|
||||||
|
client,
|
||||||
}: PrepareOptions) => {
|
}: PrepareOptions) => {
|
||||||
// initialize execution options
|
// initialize execution options
|
||||||
const execOptions = {
|
const execOptions = {
|
||||||
@@ -145,14 +148,28 @@ export default async ({
|
|||||||
npxOptions
|
npxOptions
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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
|
// ensure that migrations actually ran in case of an uncaught error
|
||||||
if (!proc.stdout.includes("Migrations completed")) {
|
if (errorOccurred) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`An error occurred while running migrations: ${
|
`An error occurred while running migrations: ${
|
||||||
proc.stderr || proc.stdout
|
proc.stderr || proc.stdout
|
||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ESNext",
|
"target": "ESNext",
|
||||||
"module": "ESNext",
|
"module": "Node16",
|
||||||
"moduleResolution": "node16",
|
"moduleResolution": "node16",
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
|
|||||||
Reference in new issue
Block a user