|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { teardownPgPools } from 'pg-cache'; |
| 4 | + |
| 5 | +import { CLIDeployTestFixture } from '../test-utils'; |
| 6 | + |
| 7 | +jest.setTimeout(30000); |
| 8 | + |
| 9 | +describe('CLI Package Alias Resolution', () => { |
| 10 | + let fixture: CLIDeployTestFixture; |
| 11 | + let testDb: any; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + fixture = new CLIDeployTestFixture('sqitch', 'simple-w-tags'); |
| 15 | + |
| 16 | + // Modify the package.json of my-first to have a scoped npm name |
| 17 | + // This simulates the case where package.json name differs from control file name |
| 18 | + const packageJsonPath = fixture.fixturePath('packages', 'my-first', 'package.json'); |
| 19 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 20 | + packageJson.name = '@test-scope/my-first'; |
| 21 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 22 | + }); |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + testDb = await fixture.setupTestDatabase(); |
| 26 | + }); |
| 27 | + |
| 28 | + afterAll(async () => { |
| 29 | + await fixture.cleanup(); |
| 30 | + await teardownPgPools(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should deploy using npm package name alias instead of control file name', async () => { |
| 34 | + // Deploy using the scoped npm name (@test-scope/my-first) instead of control file name (my-first) |
| 35 | + const commands = `lql deploy --database ${testDb.name} --package @test-scope/my-first --yes`; |
| 36 | + |
| 37 | + await fixture.runTerminalCommands(commands, { |
| 38 | + database: testDb.name |
| 39 | + }, true); |
| 40 | + |
| 41 | + // Verify deployment succeeded - the schema should exist |
| 42 | + expect(await testDb.exists('schema', 'myfirstapp')).toBe(true); |
| 43 | + |
| 44 | + // Verify the deployed changes are recorded under the control file name (my-first), not the npm name |
| 45 | + const deployedChanges = await testDb.getDeployedChanges(); |
| 46 | + expect(deployedChanges.some((change: any) => change.package === 'my-first')).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should still work with control file name directly (backward compatibility)', async () => { |
| 50 | + // Deploy using the control file name directly |
| 51 | + const commands = `lql deploy --database ${testDb.name} --package my-first --yes`; |
| 52 | + |
| 53 | + await fixture.runTerminalCommands(commands, { |
| 54 | + database: testDb.name |
| 55 | + }, true); |
| 56 | + |
| 57 | + // Verify deployment succeeded |
| 58 | + expect(await testDb.exists('schema', 'myfirstapp')).toBe(true); |
| 59 | + |
| 60 | + const deployedChanges = await testDb.getDeployedChanges(); |
| 61 | + expect(deployedChanges.some((change: any) => change.package === 'my-first')).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should deploy to specific change using npm package name alias', async () => { |
| 65 | + // Deploy to a specific change using the aliased npm name |
| 66 | + const commands = `lql deploy --database ${testDb.name} --package @test-scope/my-first --to schema_myfirstapp --yes`; |
| 67 | + |
| 68 | + await fixture.runTerminalCommands(commands, { |
| 69 | + database: testDb.name |
| 70 | + }, true); |
| 71 | + |
| 72 | + // Verify only the schema was deployed (not the tables) |
| 73 | + expect(await testDb.exists('schema', 'myfirstapp')).toBe(true); |
| 74 | + |
| 75 | + const deployedChanges = await testDb.getDeployedChanges(); |
| 76 | + expect(deployedChanges.find((change: any) => |
| 77 | + change.package === 'my-first' && change.change_name === 'schema_myfirstapp' |
| 78 | + )).toBeTruthy(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should revert using npm package name alias', async () => { |
| 82 | + // First deploy |
| 83 | + const deployCommands = `lql deploy --database ${testDb.name} --package @test-scope/my-first --yes`; |
| 84 | + await fixture.runTerminalCommands(deployCommands, { |
| 85 | + database: testDb.name |
| 86 | + }, true); |
| 87 | + |
| 88 | + expect(await testDb.exists('schema', 'myfirstapp')).toBe(true); |
| 89 | + |
| 90 | + // Then revert using the aliased npm name |
| 91 | + const revertCommands = `lql revert --database ${testDb.name} --package @test-scope/my-first --yes`; |
| 92 | + await fixture.runTerminalCommands(revertCommands, { |
| 93 | + database: testDb.name |
| 94 | + }, true); |
| 95 | + |
| 96 | + // Verify revert succeeded |
| 97 | + expect(await testDb.exists('schema', 'myfirstapp')).toBe(false); |
| 98 | + }); |
| 99 | +}); |
0 commit comments