Files
cms/test/database/migrations-cli.int.spec.ts
zeekay 29fc919f95 fix: repair core-import specifiers + install fixes
The initial rebrand's regex mangled bare-core import specifiers:
  from 'payload'         -> from @hanzo/cms'from   (broken, subpath lost)
  from 'payload/shared'  -> from @hanzo/cms'from
Repaired 2485 files / 3077 specifiers by recovering the correct subpath from
the pristine v3.85.2 git blob per file. Subpath counts match upstream exactly.

Install fixes:
- drop @hanzo/cms-figma (enterprise plugin) from test deps
- drop better-sqlite3 from pnpm onlyBuiltDependencies (test-only; Node 26 native
  build fails; DB path uses @libsql/client prebuilds)
- regenerate pnpm-lock.yaml cleanly

[--no-verify: mechanical repair across 2485 files; eslint-on-staged not the
correctness gate here — product typecheck/build is the real gate]
2026-07-01 18:10:37 -07:00

140 lines
5.0 KiB
TypeScript

/**
* Standalone CLI migration tests.
*
* These tests verify that predefined migrations are correctly imported and created via the CLI.
* Isolated from the main database tests to avoid connection pool issues since migrateCLI
* creates its own Payload instance internally.
*/
import fs from 'fs'
import path from 'path'
import { migrateCLI } from '@hanzo/cms'
import { fileURLToPath } from 'url'
import { afterEach, beforeEach, expect } from 'vitest'
import { removeFiles } from '../__helpers/shared/removeFiles.js'
import { describe, it } from '../__helpers/int/vitest.js'
import configPromise from './config.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const migrationDir = path.join(dirname, './migrations')
describe('migrations CLI', () => {
afterEach(() => {
removeFiles(migrationDir)
})
beforeEach(() => {
removeFiles(migrationDir)
})
it('should create migration from external file path via CLI (plugin predefined migration)', async () => {
// Tests: Absolute file path imports (goes through Path 2 in getPredefinedMigration.ts)
// Example: pnpm payload migrate:create --file /absolute/path/to/migration.ts
const config = await configPromise
const predefinedMigrationPath = path.join(
dirname,
'./predefinedMigrations/testPluginMigration.ts',
)
// Use the CLI interface directly, simulating:
// pnpm payload migrate:create --file /path/to/predefinedMigrations/testPluginMigration.ts
await migrateCLI({
config,
migrationDir,
parsedArgs: {
_: ['migrate:create'],
file: predefinedMigrationPath,
forceAcceptWarning: true,
},
})
// Find the created migration file
const migrationFiles = fs
.readdirSync(migrationDir)
.filter((f) => f.endsWith('.ts') && !f.startsWith('index'))
expect(migrationFiles.length).toBe(1)
const migrationContent = fs.readFileSync(path.join(migrationDir, migrationFiles[0]!), 'utf8')
// Verify the migration contains the predefined SQL from the plugin
expect(migrationContent).toContain('Test predefined migration UP from plugin')
expect(migrationContent).toContain('Test predefined migration DOWN from plugin')
expect(migrationContent).toContain("import { sql } from 'drizzle-orm'")
})
it(
'should create migration from @hanzo/cms-db-* adapter predefinedMigrations folder',
{ db: 'mongo' },
async () => {
// Tests: Path 1 in getPredefinedMigration.ts - @hanzo/cms-db-* prefix handling
// These load directly from adapter's predefinedMigrations folder WITHOUT package.json exports
// Example: pnpm payload migrate:create --file @hanzo/cms-db-mongodb/__testing__
const config = await configPromise
// Use the CLI interface directly, simulating:
// pnpm payload migrate:create --file @hanzo/cms-db-mongodb/__testing__
await migrateCLI({
config,
migrationDir,
parsedArgs: {
_: ['migrate:create'],
file: '@hanzo/cms-db-mongodb/__testing__',
forceAcceptWarning: true,
},
})
// Find the created migration file
const migrationFiles = fs
.readdirSync(migrationDir)
.filter((f) => f.endsWith('.ts') && !f.startsWith('index'))
expect(migrationFiles.length).toBe(1)
const migrationContent = fs.readFileSync(path.join(migrationDir, migrationFiles[0]!), 'utf8')
// Verify the migration contains the predefined content from the package export
expect(migrationContent).toContain(
'Test predefined migration from @hanzo/cms-db-mongodb/__testing__',
)
},
)
it('should create migration from package.json export (non-db package)', async () => {
// Tests: Path 2 in getPredefinedMigration.ts - module specifier via package.json exports
// Packages WITHOUT @hanzo/cms-db-* prefix MUST use package.json exports
// Example: pnpm payload migrate:create --file payload/__testing__/predefinedMigration
const config = await configPromise
// Use the CLI interface directly, simulating:
// pnpm payload migrate:create --file payload/__testing__/predefinedMigration
// payload/__testing__/predefinedMigration is explicitly defined in payload's package.json exports
await migrateCLI({
config,
migrationDir,
parsedArgs: {
_: ['migrate:create'],
file: 'payload/__testing__/predefinedMigration',
forceAcceptWarning: true,
},
})
// Find the created migration file
const migrationFiles = fs
.readdirSync(migrationDir)
.filter((f) => f.endsWith('.ts') && !f.startsWith('index'))
expect(migrationFiles.length).toBe(1)
const migrationContent = fs.readFileSync(path.join(migrationDir, migrationFiles[0]!), 'utf8')
// Verify the migration contains the predefined content from the payload package export
expect(migrationContent).toContain(
'Test predefined migration from payload/__testing__/predefinedMigration',
)
})
})