Skip to content

Commit 6d5c103

Browse files
committed
sqitch refactor
1 parent 3fe18ec commit 6d5c103

File tree

5 files changed

+181
-97
lines changed

5 files changed

+181
-97
lines changed

packages/core/src/migrate/migration.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export interface MigrationOptions {
2424
fast?: boolean;
2525
usePlan?: boolean;
2626
cache?: boolean;
27+
/**
28+
* The plan file to use for sqitch operations
29+
* Defaults to 'launchql.plan'
30+
*/
31+
planFile?: string;
2732
}
2833

2934
/**
@@ -56,13 +61,16 @@ export async function deployModules(options: MigrationOptions): Promise<void> {
5661
useTransaction: options.useTransaction,
5762
fast: options.fast,
5863
usePlan: options.usePlan,
59-
cache: options.cache
64+
cache: options.cache,
65+
planFile: options.planFile
6066
}
6167
);
6268
} else {
6369
// Direct execution on current directory
6470
if (options.useSqitch) {
65-
await runSqitch('deploy', options.database, options.cwd);
71+
await runSqitch('deploy', options.database, options.cwd, getPgEnvOptions(), {
72+
planFile: options.planFile || 'launchql.plan'
73+
});
6674
} else {
6775
await deployModule(
6876
getPgEnvOptions(),
@@ -103,13 +111,17 @@ export async function revertModules(options: MigrationOptions): Promise<void> {
103111
options.cwd,
104112
{
105113
useSqitch: options.useSqitch,
106-
useTransaction: options.useTransaction
114+
useTransaction: options.useTransaction,
115+
planFile: options.planFile
107116
}
108117
);
109118
} else {
110119
// Direct execution on current directory
111120
if (options.useSqitch) {
112-
await runSqitch('revert', options.database, options.cwd);
121+
await runSqitch('revert', options.database, options.cwd, getPgEnvOptions(), {
122+
planFile: options.planFile || 'launchql.plan',
123+
confirm: true
124+
});
113125
} else {
114126
await revertModule(
115127
getPgEnvOptions(),
@@ -148,12 +160,17 @@ export async function verifyModules(options: MigrationOptions): Promise<void> {
148160
options.projectName,
149161
options.database,
150162
options.cwd,
151-
{ useSqitch: options.useSqitch }
163+
{
164+
useSqitch: options.useSqitch,
165+
planFile: options.planFile
166+
}
152167
);
153168
} else {
154169
// Direct execution on current directory
155170
if (options.useSqitch) {
156-
await runSqitch('verify', options.database, options.cwd);
171+
await runSqitch('verify', options.database, options.cwd, getPgEnvOptions(), {
172+
planFile: options.planFile || 'launchql.plan'
173+
});
157174
} else {
158175
await verifyModule(
159176
getPgEnvOptions(),

packages/core/src/projects/deploy-project.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { resolve } from 'path';
2-
import { spawn } from 'child_process';
32

43
import { errors, LaunchQLOptions } from '@launchql/types';
5-
import { getSpawnEnvWithPg, PgConfig } from 'pg-env';
4+
import { PgConfig } from 'pg-env';
65
import { Logger } from '@launchql/logger';
76
import { getPgPool } from 'pg-cache';
87
import { deployModule } from '../migrate/deploy-module';
98
import { LaunchQLProject } from '../class/launchql';
109
import { packageModule } from '../package';
10+
import { runSqitch } from '../utils/sqitch-wrapper';
1111

1212
interface Extensions {
1313
resolved: string[];
@@ -50,6 +50,11 @@ export const deployProject = async (
5050
* if fast is true, you can choose to cache the packaged module
5151
*/
5252
cache?: boolean;
53+
/**
54+
* The plan file to use for sqitch operations
55+
* Defaults to 'launchql.plan'
56+
*/
57+
planFile?: string;
5358
}
5459
): Promise<Extensions> => {
5560
const mod = new LaunchQLProject(dir);
@@ -118,35 +123,19 @@ export const deployProject = async (
118123
}
119124
} else if (options?.useSqitch) {
120125
// Use legacy sqitch
121-
log.debug(`→ Command: sqitch deploy --plan-file launchql.plan db:pg:${database}`);
126+
const planFile = options.planFile || 'launchql.plan';
127+
log.debug(`→ Command: sqitch deploy --plan-file ${planFile} db:pg:${database}`);
122128

123-
const child = spawn('sqitch', ['deploy', '--plan-file', 'launchql.plan', `db:pg:${database}`], {
124-
cwd: modulePath,
125-
env: getSpawnEnvWithPg(opts.pg)
126-
});
127-
128-
const exitCode: number = await new Promise((resolve, reject) => {
129-
child.stdout.setEncoding('utf-8');
130-
child.stderr.setEncoding('utf-8');
131-
132-
child.stderr.on('data', (chunk: Buffer | string) => {
133-
const text = chunk.toString();
134-
if (/error/i.test(text)) {
135-
log.error(text);
136-
} else if (/warning/i.test(text)) {
137-
log.warn(text);
138-
} else {
139-
log.error(text); // non-warning stderr
140-
}
129+
try {
130+
const exitCode = await runSqitch('deploy', database, modulePath, opts.pg as PgConfig, {
131+
planFile
141132
});
142-
143-
child.stdout.pipe(process.stdout);
144-
145-
child.on('close', resolve);
146-
child.on('error', reject);
147-
});
148-
149-
if (exitCode !== 0) {
133+
134+
if (exitCode !== 0) {
135+
log.error(`❌ Deployment failed for module ${extension}`);
136+
throw errors.DEPLOYMENT_FAILED({ type: 'Deployment', module: extension });
137+
}
138+
} catch (err) {
150139
log.error(`❌ Deployment failed for module ${extension}`);
151140
throw errors.DEPLOYMENT_FAILED({ type: 'Deployment', module: extension });
152141
}

packages/core/src/projects/revert-project.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { resolve } from 'path';
2-
import { spawn } from 'child_process';
32

43
import { LaunchQLProject } from '../class/launchql';
54
import { errors, LaunchQLOptions } from '@launchql/types';
6-
import { getSpawnEnvWithPg } from 'pg-env';
5+
import { PgConfig } from 'pg-env';
76
import { Logger } from '@launchql/logger';
87
import { getPgPool } from 'pg-cache';
98
import { revertModule } from '../migrate/revert-module';
9+
import { runSqitch } from '../utils/sqitch-wrapper';
1010

1111
interface Extensions {
1212
resolved: string[];
@@ -20,7 +20,15 @@ export const revertProject = async (
2020
name: string,
2121
database: string,
2222
dir: string,
23-
options?: { useSqitch?: boolean; useTransaction?: boolean }
23+
options?: {
24+
useSqitch?: boolean;
25+
useTransaction?: boolean;
26+
/**
27+
* The plan file to use for sqitch operations
28+
* Defaults to 'launchql.plan'
29+
*/
30+
planFile?: string;
31+
}
2432
): Promise<Extensions> => {
2533
const mod = new LaunchQLProject(dir);
2634

@@ -58,34 +66,20 @@ export const revertProject = async (
5866

5967
if (options?.useSqitch) {
6068
// Use legacy sqitch
61-
log.debug(`→ Command: sqitch revert --plan-file launchql.plan db:pg:${database} -y`);
62-
63-
const child = spawn('sqitch', ['revert', '--plan-file', 'launchql.plan', `db:pg:${database}`, '-y'], {
64-
cwd: modulePath,
65-
env: getSpawnEnvWithPg(opts.pg),
66-
});
67-
68-
const exitCode: number = await new Promise((resolve, reject) => {
69-
child.stdout.setEncoding('utf-8');
70-
child.stderr.setEncoding('utf-8');
69+
const planFile = options.planFile || 'launchql.plan';
70+
log.debug(`→ Command: sqitch revert --plan-file ${planFile} db:pg:${database} -y`);
7171

72-
child.stderr.on('data', (chunk: Buffer | string) => {
73-
const text = chunk.toString();
74-
if (/error/i.test(text)) {
75-
log.error(text);
76-
} else if (/warning/i.test(text)) {
77-
log.warn(text);
78-
} else {
79-
log.error(text); // non-warning stderr output
80-
}
72+
try {
73+
const exitCode = await runSqitch('revert', database, modulePath, opts.pg as PgConfig, {
74+
planFile,
75+
confirm: true
8176
});
82-
83-
child.stdout.pipe(process.stdout);
84-
child.on('close', resolve);
85-
child.on('error', reject);
86-
});
87-
88-
if (exitCode !== 0) {
77+
78+
if (exitCode !== 0) {
79+
log.error(`❌ Revert failed for module ${extension}`);
80+
throw errors.DEPLOYMENT_FAILED({ type: 'Revert', module: extension });
81+
}
82+
} catch (err) {
8983
log.error(`❌ Revert failed for module ${extension}`);
9084
throw errors.DEPLOYMENT_FAILED({ type: 'Revert', module: extension });
9185
}

packages/core/src/projects/verify-project.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { resolve } from 'path';
2-
import { spawn } from 'child_process';
32

43
import { errors, LaunchQLOptions } from '@launchql/types';
5-
import { getSpawnEnvWithPg } from 'pg-env';
4+
import { PgConfig } from 'pg-env';
65
import { LaunchQLProject } from '../class/launchql';
76
import { Logger } from '@launchql/logger';
87
import { getPgPool } from 'pg-cache';
98
import { verifyModule } from '../migrate/verify-module';
9+
import { runSqitch } from '../utils/sqitch-wrapper';
1010

1111
interface Extensions {
1212
resolved: string[];
@@ -20,7 +20,14 @@ export const verifyProject = async (
2020
name: string,
2121
database: string,
2222
dir: string,
23-
options?: { useSqitch?: boolean }
23+
options?: {
24+
useSqitch?: boolean;
25+
/**
26+
* The plan file to use for sqitch operations
27+
* Defaults to 'launchql.plan'
28+
*/
29+
planFile?: string;
30+
}
2431
): Promise<Extensions> => {
2532
const mod = new LaunchQLProject(dir);
2633

@@ -58,29 +65,20 @@ export const verifyProject = async (
5865
try {
5966
if (options?.useSqitch) {
6067
// Use legacy sqitch
61-
const env = getSpawnEnvWithPg(opts.pg);
62-
await new Promise<void>((resolve, reject) => {
63-
const child = spawn('sqitch', ['verify', '--plan-file', 'launchql.plan', `db:pg:${database}`], {
64-
cwd: modulePath,
65-
env
66-
});
67-
68-
child.stdout.on('data', (data) => {
69-
log.debug(data.toString().trim());
68+
const planFile = options.planFile || 'launchql.plan';
69+
log.debug(`→ Command: sqitch verify --plan-file ${planFile} db:pg:${database}`);
70+
71+
try {
72+
const exitCode = await runSqitch('verify', database, modulePath, opts.pg as PgConfig, {
73+
planFile
7074
});
7175

72-
child.stderr.on('data', (data) => {
73-
log.error(data.toString().trim());
74-
});
75-
76-
child.on('close', (code) => {
77-
if (code === 0) {
78-
resolve();
79-
} else {
80-
reject(new Error(`sqitch verify exited with code ${code}`));
81-
}
82-
});
83-
});
76+
if (exitCode !== 0) {
77+
throw new Error(`sqitch verify exited with code ${exitCode}`);
78+
}
79+
} catch (err) {
80+
throw new Error(`Verification failed: ${err instanceof Error ? err.message : String(err)}`);
81+
}
8482
} else {
8583
// Use new migration system
8684
await verifyModule(opts.pg, database, modulePath);

0 commit comments

Comments
 (0)