Skip to content

Commit 2280638

Browse files
authored
fix(cli): add publish behaviour on run and rollback (#348)
1 parent 895e11e commit 2280638

File tree

6 files changed

+388
-14
lines changed

6 files changed

+388
-14
lines changed

packages/cli/src/commands/migrations/rollback/actions.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface RollbackDataStory {
88
storyId: number;
99
name: string;
1010
content: StoryContent;
11+
published?: boolean;
12+
unpublished_changes?: boolean;
1113
}
1214

1315
export interface RollbackData {
@@ -20,8 +22,13 @@ export interface RollbackData {
2022
* @param options.space - The space ID
2123
* @param options.path - Base path for saving rollback data
2224
* @param options.story - Story with their original content
25+
* @param options.story.id - Story ID
26+
* @param options.story.name - Story name
27+
* @param options.story.content - Story content
28+
* @param options.story.published - Story publication status
29+
* @param options.story.unpublished_changes - Story unpublished changes status
2330
* @param options.migrationTimestamp - The timestamp when the migration started
24-
* @param options.migrationFile - Name of the migration file being applied
31+
* @param options.migrationNames - Names of the migration files being applied
2532
*/
2633
export async function saveRollbackData({
2734
space,
@@ -32,14 +39,16 @@ export async function saveRollbackData({
3239
}: {
3340
space: string;
3441
path: string;
35-
story: { id: number; name: string; content: StoryContent };
42+
story: { id: number; name: string; content: StoryContent; published?: boolean; unpublished_changes?: boolean };
3643
migrationTimestamp: number;
3744
migrationNames: string[];
3845
}): Promise<void> {
3946
const rollbackData: RollbackDataStory = {
4047
storyId: story.id,
4148
name: story.name,
4249
content: story.content,
50+
published: story.published,
51+
unpublished_changes: story.unpublished_changes,
4352
};
4453

4554
// Resolve the path for rollbacks

packages/cli/src/commands/migrations/rollback/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,26 @@ migrationsCommand.command('rollback [migrationFile]')
5353
for (const story of rollbackData.stories) {
5454
const spinner = new Spinner({ verbose }).start(`Restoring story ${chalk.hex(colorPalette.PRIMARY)(story.name || story.storyId)}...`);
5555
try {
56-
await updateStory(space, story.storyId, {
56+
const payload: any = {
5757
story: {
5858
content: story.content,
5959
id: story.storyId,
6060
name: story.name,
6161
},
6262
force_update: '1',
63-
});
63+
};
64+
65+
// Only restore publication status if it was saved in the rollback data
66+
// For backwards compatibility, we check if the published status exists in the rollback data
67+
if (story.published !== undefined && story.unpublished_changes !== undefined) {
68+
// If the story was originally published without changes, publish it
69+
if (story.published && !story.unpublished_changes) {
70+
payload.publish = 1;
71+
}
72+
// Otherwise, don't publish (let it remain as draft or with unpublished changes)
73+
}
74+
75+
await updateStory(space, story.storyId, payload);
6476
spinner.succeed(`Restored story ${chalk.hex(colorPalette.PRIMARY)(story.name || story.storyId)}`);
6577
}
6678
catch (error) {

packages/cli/src/commands/migrations/run/streams/migrations-transform.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class MigrationStream extends Transform {
8383
return migrationFunction;
8484
}
8585

86-
private async processStory(story: Story): Promise<Array<{ storyId: number; name: string | undefined; content: StoryContent }>> {
86+
private async processStory(story: Story): Promise<Array<{ storyId: number; name: string | undefined; content: StoryContent; published?: boolean; unpublished_changes?: boolean }>> {
8787
// Filter migrations based on component name if provided
8888
const relevantMigrations = this.options.componentName
8989
? this.options.migrationFiles.filter((file) => {
@@ -101,7 +101,7 @@ export class MigrationStream extends Transform {
101101
return [];
102102
}
103103

104-
const successfulResults: Array<{ storyId: number; name: string | undefined; content: StoryContent }> = [];
104+
const successfulResults: Array<{ storyId: number; name: string | undefined; content: StoryContent; published?: boolean; unpublished_changes?: boolean }> = [];
105105

106106
// Process each relevant migration
107107
const result = await this.applyMigrationsToStory(story, relevantMigrations);
@@ -112,7 +112,7 @@ export class MigrationStream extends Transform {
112112
return successfulResults;
113113
}
114114

115-
private async applyMigrationsToStory(story: Story, migrationFiles: MigrationFile[]): Promise<{ storyId: number; name: string | undefined; content: StoryContent } | null> {
115+
private async applyMigrationsToStory(story: Story, migrationFiles: MigrationFile[]): Promise<{ storyId: number; name: string | undefined; content: StoryContent; published?: boolean; unpublished_changes?: boolean } | null> {
116116
const migrationNames = migrationFiles.map(f => f.name);
117117

118118
try {
@@ -144,7 +144,13 @@ export class MigrationStream extends Transform {
144144
await saveRollbackData({
145145
space: this.options.space,
146146
path: this.options.path,
147-
story: { id: story.id, name: story.name || '', content: story.content as StoryContent },
147+
story: {
148+
id: story.id,
149+
name: story.name || '',
150+
content: story.content as StoryContent,
151+
published: story.published,
152+
unpublished_changes: story.unpublished_changes,
153+
},
148154
migrationTimestamp: this.timestamp,
149155
migrationNames,
150156
});
@@ -160,6 +166,8 @@ export class MigrationStream extends Transform {
160166
storyId: story.id,
161167
name: story.name,
162168
content: storyContent,
169+
published: story.published,
170+
unpublished_changes: story.unpublished_changes,
163171
};
164172
}
165173
else if (processed && !contentChanged) {

0 commit comments

Comments
 (0)