Skip to content

Commit 09f55ac

Browse files
committed
plans
1 parent 1a577d3 commit 09f55ac

File tree

16 files changed

+268
-179
lines changed

16 files changed

+268
-179
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"@launchql/migrate": "^2.3.0",
4646
"@launchql/server-utils": "^2.1.15",
47+
"@launchql/sqitch-parser": "^2.0.0",
4748
"@launchql/templatizer": "^2.1.6",
4849
"@launchql/types": "^2.1.13",
4950
"@pgsql/types": "^17.6.1",

packages/core/src/extensions.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFileSync, writeFileSync } from 'fs';
2+
import { parsePlanFileSimple } from '@launchql/sqitch-parser';
23
import { ModuleMap } from './modules';
34

45
export interface ExtensionInfo {
@@ -48,16 +49,14 @@ export const getAvailableExtensions = (
4849
* Parse the sqitch.plan file to get the extension name.
4950
*/
5051
export const getExtensionName = (packageDir: string): string => {
51-
const plan = readFileSync(`${packageDir}/sqitch.plan`, 'utf-8')
52-
.split('\n')
53-
.map((line) => line.trim())
54-
.filter((line) => /^%project=/.test(line));
55-
56-
if (!plan.length) {
52+
const planPath = `${packageDir}/sqitch.plan`;
53+
const plan = parsePlanFileSimple(planPath);
54+
55+
if (!plan.project) {
5756
throw new Error('No project name found in sqitch.plan!');
5857
}
5958

60-
return plan[0].split('=')[1].trim();
59+
return plan.project;
6160
};
6261

6362
/**

packages/core/src/modules.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { readFileSync } from 'fs';
22
import { sync as glob } from 'glob';
33
import { basename, dirname, relative } from 'path';
4+
import { getLatestChange } from '@launchql/sqitch-parser';
45

56
export interface Module {
67
path: string;
@@ -65,12 +66,8 @@ export const latestChange = (
6566
throw new Error(`latestChange() ${sqlmodule} NOT FOUND!`);
6667
}
6768

68-
const plan = readFileSync(`${basePath}/${module.path}/sqitch.plan`, 'utf-8')
69-
.split('\n')
70-
.map((line) => line.trim())
71-
.filter(Boolean);
72-
73-
return plan[plan.length - 1].split(' ')[0];
69+
const planPath = `${basePath}/${module.path}/sqitch.plan`;
70+
return getLatestChange(planPath);
7471
};
7572

7673
/**
@@ -86,12 +83,8 @@ export const latestChangeAndVersion = (
8683
throw new Error(`latestChangeAndVersion() ${sqlmodule} NOT FOUND!`);
8784
}
8885

89-
const plan = readFileSync(`${basePath}/${module.path}/sqitch.plan`, 'utf-8')
90-
.split('\n')
91-
.map((line) => line.trim())
92-
.filter(Boolean);
93-
94-
const change = plan[plan.length - 1].split(' ')[0];
86+
const planPath = `${basePath}/${module.path}/sqitch.plan`;
87+
const change = getLatestChange(planPath);
9588
const pkg = require(`${basePath}/${module.path}/package.json`);
9689

9790
return { change, version: pkg.version };

packages/core/src/resolve.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFileSync } from 'fs';
2+
import { getChanges } from '@launchql/sqitch-parser';
23

34
import { getDeps } from './deps';
45
import { getExtensionName } from './extensions';
@@ -42,17 +43,9 @@ export const resolveWithPlan = (
4243
scriptType: 'deploy' | 'revert' = 'deploy'
4344
): string => {
4445
const sqlfile: string[] = [];
45-
const plan = readFileSync(`${pkgDir}/sqitch.plan`, 'utf-8');
46-
47-
let resolved = plan
48-
.split('\n')
49-
.filter(
50-
(line) =>
51-
line.trim().length > 0 && // Exclude empty lines
52-
!line.trim().startsWith('%') && // Exclude initial project settings
53-
!line.trim().startsWith('@') // Exclude tags
54-
)
55-
.map((line) => line.split(' ')[0]);
46+
const planPath = `${pkgDir}/sqitch.plan`;
47+
48+
let resolved = getChanges(planPath);
5649

5750
if (scriptType === 'revert') {
5851
resolved = resolved.reverse();

packages/migrate/__tests__/plan-parser.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'path';
22
import {
3-
parsePlanFileWithValidation,
3+
parsePlanFile,
44
resolveReference,
55
ExtendedPlanFile
66
} from '../src/parser/plan-parser';
@@ -11,7 +11,7 @@ describe('Plan Parser with Validation', () => {
1111
describe('Valid plan files', () => {
1212
it('should parse valid change names', () => {
1313
const planPath = join(fixturesDir, 'plan-valid', 'valid-change-names.plan');
14-
const result = parsePlanFileWithValidation(planPath);
14+
const result = parsePlanFile(planPath);
1515

1616
expect(result.errors).toHaveLength(0);
1717
expect(result.plan).toBeDefined();
@@ -38,7 +38,7 @@ describe('Plan Parser with Validation', () => {
3838

3939
it('should parse valid tag names', () => {
4040
const planPath = join(fixturesDir, 'plan-valid', 'valid-tag-names.plan');
41-
const result = parsePlanFileWithValidation(planPath);
41+
const result = parsePlanFile(planPath);
4242

4343
expect(result.errors).toHaveLength(0);
4444
expect(result.plan).toBeDefined();
@@ -69,7 +69,7 @@ describe('Plan Parser with Validation', () => {
6969

7070
it('should parse symbolic references', () => {
7171
const planPath = join(fixturesDir, 'plan-valid', 'symbolic-head-root.plan');
72-
const result = parsePlanFileWithValidation(planPath);
72+
const result = parsePlanFile(planPath);
7373

7474
expect(result.errors).toHaveLength(0);
7575
expect(result.plan).toBeDefined();
@@ -87,7 +87,7 @@ describe('Plan Parser with Validation', () => {
8787

8888
it('should parse relative references', () => {
8989
const planPath = join(fixturesDir, 'plan-valid', 'relative-head-root.plan');
90-
const result = parsePlanFileWithValidation(planPath);
90+
const result = parsePlanFile(planPath);
9191

9292
expect(result.errors).toHaveLength(0);
9393
expect(result.plan).toBeDefined();
@@ -114,7 +114,7 @@ describe('Plan Parser with Validation', () => {
114114

115115
it('should parse SHA1 references', () => {
116116
const planPath = join(fixturesDir, 'plan-valid', 'sha1-refs.plan');
117-
const result = parsePlanFileWithValidation(planPath);
117+
const result = parsePlanFile(planPath);
118118

119119
expect(result.errors).toHaveLength(0);
120120
expect(result.plan).toBeDefined();
@@ -132,7 +132,7 @@ describe('Plan Parser with Validation', () => {
132132
describe('Invalid plan files', () => {
133133
it('should report errors for invalid change names', () => {
134134
const planPath = join(fixturesDir, 'plan-invalid', 'invalid-change-names.plan');
135-
const result = parsePlanFileWithValidation(planPath);
135+
const result = parsePlanFile(planPath);
136136

137137
expect(result.errors.length).toBeGreaterThan(0);
138138

@@ -146,7 +146,7 @@ describe('Plan Parser with Validation', () => {
146146

147147
it('should report errors for invalid tag names', () => {
148148
const planPath = join(fixturesDir, 'plan-invalid', 'invalid-tag-names.plan');
149-
const result = parsePlanFileWithValidation(planPath);
149+
const result = parsePlanFile(planPath);
150150

151151
expect(result.errors.length).toBeGreaterThan(0);
152152

@@ -158,7 +158,7 @@ describe('Plan Parser with Validation', () => {
158158

159159
it('should report errors for bad symbolic references', () => {
160160
const planPath = join(fixturesDir, 'plan-invalid', 'bad-symbolic-refs.plan');
161-
const result = parsePlanFileWithValidation(planPath);
161+
const result = parsePlanFile(planPath);
162162

163163
expect(result.errors.length).toBeGreaterThan(0);
164164

packages/migrate/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"@launchql/logger": "^1.0.0",
3737
"@launchql/server-utils": "^2.1.15",
38+
"@launchql/sqitch-parser": "^2.0.0",
3839
"@launchql/types": "^2.1.13",
3940
"@pgsql/types": "^17.6.1",
4041
"pg": "^8.11.3",

packages/migrate/src/client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ import {
1414
VerifyResult,
1515
StatusResult
1616
} from './types';
17-
import { parsePlanFile, getChangesInOrder } from './parser/plan';
17+
import { parsePlanFileSimple as parsePlanFile, Change } from '@launchql/sqitch-parser';
1818
import { hashFile } from './utils/hash';
1919
import { readScript, scriptExists } from './utils/fs';
2020
import { cleanSql } from './clean';
2121
import { withTransaction, executeQuery, TransactionContext } from './utils/transaction';
2222

23+
// Helper function to get changes in order
24+
function getChangesInOrder(planPath: string, reverse: boolean = false): Change[] {
25+
const plan = parsePlanFile(planPath);
26+
return reverse ? [...plan.changes].reverse() : plan.changes;
27+
}
28+
2329
const log = new Logger('migrate');
2430

2531
export class LaunchQLMigrate {

packages/migrate/src/index.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
export { LaunchQLMigrate } from './client';
22
export * from './types';
3-
export { parsePlanFile, getChangeNamesFromPlan, getChangesInOrder } from './parser/plan';
43
export { hashFile, hashString } from './utils/hash';
54
export { readScript, scriptExists } from './utils/fs';
65
export { withTransaction, TransactionContext, TransactionOptions } from './utils/transaction';
76

8-
// Export new validation and parsing functionality
9-
export {
10-
isValidChangeName,
11-
isValidTagName,
12-
parseReference,
13-
isValidDependency,
14-
ParsedReference
15-
} from './parser/validators';
16-
17-
export {
18-
parsePlanFileWithValidation,
19-
resolveReference,
20-
ExtendedPlanFile,
21-
Tag,
22-
ParseError,
23-
ParseResult
24-
} from './parser/plan-parser';
7+
// Re-export parser functionality from sqitch-parser
8+
export * from '@launchql/sqitch-parser';

packages/migrate/src/parser/plan.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './parser';

0 commit comments

Comments
 (0)