Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/core/__tests__/migrate/local-tracking-guard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { LaunchQLMigrate } from '../../src/migrate/client';
import { MigrateTestFixture, teardownAllPools, TestDatabase } from '../../test-utils';

describe('local tracking guard for deployed/skipped', () => {
let fixture: MigrateTestFixture;
let db: TestDatabase;

beforeEach(async () => {
fixture = new MigrateTestFixture();
db = await fixture.setupTestDatabase();
});

afterEach(async () => {
await fixture.cleanup();
});

afterAll(async () => {
await teardownAllPools();
});

it('normalizes same-package qualified names to unqualified in deployed', async () => {
const tempDir = fixture.createPlanFile('test-local-tracking', [
{ name: 'change1' }
]);

fixture.createScript(tempDir, 'deploy', 'change1', 'SELECT 1;');

const client = new LaunchQLMigrate(db.config);

const result = await client.deploy({
modulePath: tempDir,
logOnly: true,
});

expect(result.deployed).toContain('change1');
expect(result.deployed.every((n: string) => !n.includes(':'))).toBe(true);
});

it('throws error on cross-package qualified names', async () => {
const tempDir = fixture.createPlanFile('test-local-tracking', [
{ name: 'change1' }
]);

fixture.createScript(tempDir, 'deploy', 'change1', 'SELECT 1;');

const client = new LaunchQLMigrate(db.config);

expect(() => {
(client as any).toUnqualifiedLocal('pkgA', 'pkgB:change1');
}).toThrow('Cross-package change encountered in local tracking: pkgB:change1 (current package: pkgA)');
});
});
19 changes: 13 additions & 6 deletions packages/core/src/migrate/client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Logger } from '@launchql/logger';
import { errors } from '@launchql/types';
import { readFileSync } from 'fs';
import { dirname,join } from 'path';
import { Pool } from 'pg';
import { getPgPool } from 'pg-cache';
import { PgConfig } from 'pg-env';
import { errors } from '@launchql/types';

import { LaunchQLPackage } from '../core/class/launchql';
import { Change, parsePlanFile, parsePlanFileSimple, readScript } from '../files';
import { DependencyResult, resolveDependencies } from '../resolution/deps';
import { resolveDependencies } from '../resolution/deps';
import { resolveTagToChangeName } from '../resolution/resolve';
import { cleanSql } from './clean';
import {
Expand Down Expand Up @@ -49,6 +48,13 @@ export class LaunchQLMigrate {
private eventLogger: EventLogger;
private initialized: boolean = false;

private toUnqualifiedLocal(pkg: string, nm: string) {
if (!nm.includes(':')) return nm;
const [p, local] = nm.split(':', 2);
if (p === pkg) return local;
throw new Error(`Cross-package change encountered in local tracking: ${nm} (current package: ${pkg})`);
}

constructor(config: PgConfig, options: LaunchQLMigrateOptions = {}) {
this.pgConfig = config;
// Use environment variable DEPLOYMENT_HASH_METHOD if available, otherwise use options or default to 'content'
Expand Down Expand Up @@ -156,7 +162,7 @@ export class LaunchQLMigrate {
const isDeployed = await this.isDeployed(plan.package, change.name);
if (isDeployed) {
log.info(`Skipping already deployed change: ${change.name}`);
const unqualified = change.name.includes(':') ? change.name.split(':')[1] : change.name;
const unqualified = this.toUnqualifiedLocal(plan.package, change.name);
skipped.push(unqualified);
continue;
}
Expand Down Expand Up @@ -196,7 +202,7 @@ export class LaunchQLMigrate {
]
);

const unqualified = change.name.includes(':') ? change.name.split(':')[1] : change.name;
const unqualified = this.toUnqualifiedLocal(plan.package, change.name);
deployed.push(unqualified);
log.success(`Successfully ${logOnly ? 'logged' : 'deployed'}: ${change.name}`);
} catch (error: any) {
Expand Down Expand Up @@ -302,7 +308,8 @@ export class LaunchQLMigrate {
const isDeployed = await this.isDeployed(plan.package, change.name);
if (!isDeployed) {
log.info(`Skipping not deployed change: ${change.name}`);
skipped.push(change.name);
const unqualified = this.toUnqualifiedLocal(plan.package, change.name);
skipped.push(unqualified);
continue;
}

Expand Down