Skip to content

Commit

Permalink
feat(templates): programmatic migration gen (#9238)
Browse files Browse the repository at this point in the history
Programmatically generate lockfiles and postgres migrations
  • Loading branch information
denolfe authored Nov 16, 2024
1 parent ff8e7bb commit 1393d84
Show file tree
Hide file tree
Showing 28 changed files with 16,194 additions and 5,780 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ jobs:
tests-e2e:
runs-on: ubuntu-latest
needs: build
name: ${{ matrix.suite }}
name: e2e-${{ matrix.suite }}
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -414,6 +414,10 @@ jobs:
- template: with-vercel-postgres
database: postgres

# Re-enable once PG conncection is figured out
# - template: with-vercel-website
# database: postgres

name: ${{ matrix.template }}-${{ matrix.database }}

env:
Expand Down
5 changes: 4 additions & 1 deletion scripts/build-template-with-local-pkgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ async function main() {
await fs.writeFile(
path.resolve(templatePath, '.env'),
// Populate POSTGRES_URL just in case it's needed
`PAYLOAD_SECRET=secret\nDATABASE_URI=${databaseConnection}\nPOSTGRES_URL=${databaseConnection}`,
`PAYLOAD_SECRET=secret
DATABASE_URI=${databaseConnection}
POSTGRES_URL=${databaseConnection}
BLOB_READ_WRITE_TOKEN=vercel_blob_rw_TEST_asdf`,
)
execSync('pnpm run build', execOpts)

Expand Down
62 changes: 37 additions & 25 deletions scripts/generate-template-variations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function main() {
dirname: 'with-vercel-website',
db: 'vercel-postgres',
storage: 'vercelBlobStorage',
sharp: false,
sharp: true,
vercelDeployButtonLink:
`https://vercel.com/new/clone?repository-url=` +
encodeURI(
Expand Down Expand Up @@ -202,35 +202,27 @@ async function main() {
packageJson.scripts.ci = 'payload migrate && pnpm build'
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2))

// Handle copying migrations
const migrationSrcDir = path.join(templatesDir, '_data/migrations')
const migrationDestDir = path.join(destDir, 'src/migrations')

// Make directory if it doesn't exist
if ((await fs.stat(migrationDestDir).catch(() => null)) === null) {
await fs.mkdir(migrationDestDir, { recursive: true })
}
log(`Copying migrations from ${migrationSrcDir} to ${migrationDestDir}`)
copyRecursiveSync(migrationSrcDir, migrationDestDir)

// Change all '@payloadcms/db-postgres' import to be '@payloadcms/db-vercel-postgres'
if (db === 'vercel-postgres') {
const migrationFiles = await fs.readdir(migrationDestDir)
for (const migrationFile of migrationFiles) {
const migrationFilePath = path.join(migrationDestDir, migrationFile)
const migrationFileContents = await fs.readFile(migrationFilePath, 'utf8')
const updatedFileContents = migrationFileContents.replaceAll(
'@payloadcms/db-postgres',
'@payloadcms/db-vercel-postgres',
)
await fs.writeFile(migrationFilePath, updatedFileContents)
}
}
// Delete and recreate migrations directory
await fs.rm(migrationDestDir, { recursive: true, force: true })
await fs.mkdir(migrationDestDir, { recursive: true })

log(`Generating initial migrations in ${migrationDestDir}`)

execSyncSafe(`pnpm run payload migrate:create initial`, {
cwd: destDir,
env: {
...process.env,
BLOB_READ_WRITE_TOKEN: 'vercel_blob_rw_TEST_asdf',
DATABASE_URI: 'postgres://localhost:5432/payloadtests',
},
})
}

if (generateLockfile) {
log('Generating pnpm-lock.yaml')
execSync(`pnpm install --ignore-workspace`, { cwd: destDir })
execSyncSafe(`pnpm install --ignore-workspace`, { cwd: destDir })
}

// TODO: Email?
Expand All @@ -241,7 +233,7 @@ async function main() {
}
// TODO: Run prettier manually on the generated files, husky blows up
log('Running prettier on generated files...')
execSync(`pnpm prettier --write templates "*.{js,jsx,ts,tsx}"`)
execSyncSafe(`pnpm prettier --write templates "*.{js,jsx,ts,tsx}"`)

log('Template generation complete!')
}
Expand Down Expand Up @@ -307,3 +299,23 @@ function header(message: string) {
function log(message: string) {
console.log(chalk.dim(message))
}
function execSyncSafe(command: string, options?: Parameters<typeof execSync>[1]) {
try {
execSync(command, options)
} catch (error) {
if (error instanceof Error) {
const stderr = (error as any).stderr?.toString()
const stdout = (error as any).stdout?.toString()

if (stderr && stderr.trim()) {
console.error('Standard Error:', stderr)
} else if (stdout && stdout.trim()) {
console.error('Standard Output (likely contains error details):', stdout)
} else {
console.error('An unknown error occurred with no output.')
}
} else {
console.error('An unexpected error occurred:', error)
}
}
}
Loading

0 comments on commit 1393d84

Please sign in to comment.