Skip to content

Commit 7c9a7a9

Browse files
committed
Remove multiple migrations & fix log prefix handling
1 parent a7ec207 commit 7c9a7a9

11 files changed

+112
-65
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"contributes": {
4444
"commands": [
4545
{
46-
"command": "entityframework.removeMigration",
46+
"command": "entityframework.removeMigrations",
4747
"title": "Remove Migration",
4848
"icon": {
4949
"light": "icons/trash_light.svg",
@@ -130,7 +130,7 @@
130130
"when": "false"
131131
},
132132
{
133-
"command": "entityframework.removeMigration",
133+
"command": "entityframework.removeMigrations",
134134
"when": "false"
135135
},
136136
{
@@ -171,12 +171,12 @@
171171
],
172172
"view/item/context": [
173173
{
174-
"command": "entityframework.removeMigration",
174+
"command": "entityframework.removeMigrations",
175175
"when": "viewItem =~ /^migration-.*\\|?can-remove\\|?.*$/",
176176
"group": "inline@1"
177177
},
178178
{
179-
"command": "entityframework.removeMigration",
179+
"command": "entityframework.removeMigrations",
180180
"when": "viewItem =~ /^migration-.*\\|?can-remove\\|?.*$/",
181181
"group": "context@2"
182182
},
@@ -203,22 +203,22 @@
203203
{
204204
"command": "entityframework.addMigration",
205205
"when": "viewItem == dbContext",
206-
"group": "context@4"
206+
"group": "inline@4"
207207
},
208208
{
209209
"command": "entityframework.addMigration",
210210
"when": "viewItem == dbContext",
211-
"group": "inline@4"
211+
"group": "context@1"
212212
},
213213
{
214214
"command": "entityframework.generateScript",
215215
"when": "viewItem == dbContext",
216-
"group": "context@3"
216+
"group": "inline@3"
217217
},
218218
{
219219
"command": "entityframework.generateScript",
220220
"when": "viewItem == dbContext",
221-
"group": "inline@3"
221+
"group": "context@2"
222222
},
223223
{
224224
"command": "entityframework.generateERD",
@@ -228,7 +228,7 @@
228228
{
229229
"command": "entityframework.generateERD",
230230
"when": "viewItem == dbContext",
231-
"group": "context@2"
231+
"group": "context@3"
232232
},
233233
{
234234
"command": "entityframework.dbContextInfo",
@@ -238,7 +238,7 @@
238238
{
239239
"command": "entityframework.dbContextInfo",
240240
"when": "viewItem == dbContext",
241-
"group": "context@1"
241+
"group": "context@4"
242242
},
243243
{
244244
"command": "entityframework.scaffold",

src/actions/GenerateERDAction.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ export class GenerateERDAction extends TerminalAction {
161161
removeDataFromOutput: true,
162162
},
163163
);
164-
const output = CLI.getDataFromStdOut(await this.getOutput());
165-
166-
const result = JSON.parse(output) as ScaffoldResult;
164+
const output = await this.getOutput();
165+
const data = CLI.getDataFromStdOut(await this.getOutput());
166+
const result = JSON.parse(data) as ScaffoldResult;
167167

168168
const fileContents = fs.readFileSync(result.contextFile, 'utf-8');
169169

src/actions/GenerateScriptAction.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@ export class GenerateScriptAction extends TerminalAction {
3939
await this.start(undefined, {
4040
removeDataFromOutput: true,
4141
});
42-
const output = CLI.getDataFromStdOut(await this.getOutput());
43-
const uri = vscode.Uri.parse(
44-
`${TextDocumentProvider.scheme}:${output}`,
45-
);
42+
const output = await this.getOutput();
43+
const data = CLI.getDataFromStdOut(output);
44+
const uri = vscode.Uri.parse(`${TextDocumentProvider.scheme}:${data}`);
4645
const doc = await vscode.workspace.openTextDocument(uri);
4746
await vscode.languages.setTextDocumentLanguage(doc, 'sql');
4847
await vscode.window.showTextDocument(doc, { preview: false });
49-
return output;
48+
return data;
5049
},
5150
);
5251
}

src/actions/RemoveMigrationAction.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class RemoveMigrationAction extends TerminalAction {
1616
private readonly workspaceRoot: string,
1717
private readonly dbContext: string,
1818
private readonly project: string,
19+
private readonly refresh?: boolean,
1920
) {
2021
super(
2122
terminalProvider,
@@ -47,10 +48,13 @@ export class RemoveMigrationAction extends TerminalAction {
4748
this.dbContext,
4849
);
4950
dbContextsCache.clear(cacheId);
50-
await vscode.commands.executeCommand(
51-
CommandProvider.getCommandName(RefreshTreeCommand.commandName),
52-
false,
53-
);
51+
const refresh = this.refresh || this.refresh === undefined;
52+
if (refresh) {
53+
await vscode.commands.executeCommand(
54+
CommandProvider.getCommandName(RefreshTreeCommand.commandName),
55+
false,
56+
);
57+
}
5458
return output;
5559
},
5660
);

src/actions/RemoveMigrationsAction.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as vscode from 'vscode';
2+
import { CommandProvider } from '../commands/CommandProvider';
3+
import { RefreshTreeCommand } from '../commands/RefreshTreeCommand';
4+
import type { TerminalProvider } from '../terminal/TerminalProvider';
5+
import type { MigrationTreeItem } from '../treeView/MigrationTreeItem';
6+
import type { IAction } from './IAction';
7+
import { RemoveMigrationAction } from './RemoveMigrationAction';
8+
9+
export class RemoveMigrationsAction implements IAction {
10+
constructor(
11+
private readonly terminalProvider: TerminalProvider,
12+
private readonly workspaceRoot: string,
13+
private readonly dbContext: string,
14+
private readonly project: string,
15+
private readonly migrationsToRemove: MigrationTreeItem[],
16+
) {}
17+
18+
public async run() {
19+
for (let i = 0; i < this.migrationsToRemove.length; i++) {
20+
await new RemoveMigrationAction(
21+
this.terminalProvider,
22+
this.workspaceRoot,
23+
this.dbContext,
24+
this.project,
25+
false,
26+
).run();
27+
}
28+
if (this.migrationsToRemove.length > 0) {
29+
await vscode.commands.executeCommand(
30+
CommandProvider.getCommandName(RefreshTreeCommand.commandName),
31+
false,
32+
);
33+
}
34+
}
35+
}

src/cli/CLI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TerminalColors } from '../terminal/TerminalColors';
66
import type { Logger } from '../util/Logger';
77

88
const NEWLINE_SEPARATOR = /\r\n|\r|\n/;
9-
const OUTPUT_PREFIX = /^([a-z]+:\s+)/;
9+
const OUTPUT_PREFIX = /^([a-z]+:([\s]{4}|[\s]{3}))/;
1010

1111
export type ExecOpts = {
1212
cmdArgs: string[];

src/commands/CommandProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { GenerateERDCommand } from './GenerateERDCommand';
1414
import { GenerateScriptCommand } from './GenerateScriptCommand';
1515
import { OpenMigrationFileCommand } from './OpenMigrationFileCommand';
1616
import { RefreshTreeCommand } from './RefreshTreeCommand';
17-
import { RemoveMigrationCommand } from './RemoveMigrationCommand';
17+
import { RemoveMigrationsCommand } from './RemoveMigrationsCommand';
1818
import { RunMigrationCommand } from './RunMigrationCommand';
1919
import { ScaffoldCommand } from './ScaffoldCommand';
2020
import { UndoMigrationCommand } from './UndoMigrationCommand';
@@ -34,9 +34,9 @@ export class CommandProvider extends Disposable {
3434
new AddMigrationCommand(terminalProvider, item),
3535
);
3636
this.registerCommand(
37-
RemoveMigrationCommand.commandName,
37+
RemoveMigrationsCommand.commandName,
3838
(item?: MigrationTreeItem) => {
39-
return new RemoveMigrationCommand(terminalProvider, item);
39+
return new RemoveMigrationsCommand(terminalProvider, item);
4040
},
4141
);
4242
this.registerCommand(

src/commands/RemoveMigrationCommand.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { RemoveMigrationsAction } from '../actions/RemoveMigrationsAction';
2+
import type { TerminalProvider } from '../terminal/TerminalProvider';
3+
import {
4+
dbContextsCache,
5+
DbContextTreeItem,
6+
} from '../treeView/DbContextTreeItem';
7+
import { type MigrationTreeItem } from '../treeView/MigrationTreeItem';
8+
import { Command } from './Command';
9+
10+
export class RemoveMigrationsCommand extends Command {
11+
public static commandName = 'removeMigrations';
12+
13+
constructor(
14+
private readonly terminalProvider: TerminalProvider,
15+
private readonly item?: MigrationTreeItem,
16+
) {
17+
super();
18+
}
19+
20+
public async run() {
21+
if (!this.item) {
22+
return;
23+
}
24+
const migrations = dbContextsCache.get(
25+
DbContextTreeItem.getCacheId(
26+
this.item.workspaceRoot,
27+
this.item.projectFile.name,
28+
this.item.dbContext,
29+
),
30+
);
31+
const index = migrations?.indexOf(this.item) || -1;
32+
if (index === -1) {
33+
return;
34+
}
35+
const migrationsToRemove = migrations?.slice(index) || [];
36+
return new RemoveMigrationsAction(
37+
this.terminalProvider,
38+
this.item.workspaceRoot,
39+
this.item.dbContext,
40+
this.item.projectFile.name,
41+
migrationsToRemove,
42+
).run();
43+
}
44+
}

src/treeView/DbContextTreeItem.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,12 @@ export class DbContextTreeItem extends TreeItem {
6464
) as Migration[];
6565

6666
const children = migrations.map(
67-
(migration, index) =>
67+
migration =>
6868
new MigrationTreeItem(
6969
migration.name,
7070
this.label,
7171
this.projectFile,
7272
migration,
73-
index === migrations.length - 1,
7473
),
7574
);
7675
dbContextsCache.set(this.cacheId, children);

src/treeView/MigrationTreeItem.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ export class MigrationTreeItem extends TreeItem {
1212
public readonly dbContext: string,
1313
public readonly projectFile: ProjectFile,
1414
public readonly migration: Migration,
15-
isLast: boolean,
1615
) {
1716
super(
1817
label,
1918
projectFile.workspaceRoot,
2019
vscode.TreeItemCollapsibleState.None,
2120
);
2221
this.iconPath = getIconPath('file-code_light.svg', 'file-code_dark.svg');
23-
this.contextValue =
24-
'migration-' + getMigrationContextValue(migration, isLast);
22+
this.contextValue = 'migration-' + getMigrationContextValue(migration);
2523
this.resourceUri = migration.applied
2624
? vscode.Uri.parse(`${MigrationTreeItemScheme.Applied}:${label}`, true)
2725
: vscode.Uri.parse(
@@ -31,20 +29,15 @@ export class MigrationTreeItem extends TreeItem {
3129
}
3230
}
3331

34-
function getMigrationContextValue(
35-
migration: Migration,
36-
isLast: boolean,
37-
): string {
32+
function getMigrationContextValue(migration: Migration): string {
3833
const states: Array<
3934
'can-apply' | 'can-undo' | 'can-remove' | 'applied' | 'not-applied'
4035
> = [];
4136
if (migration.applied) {
4237
states.push('applied');
4338
states.push('can-undo');
4439
} else {
45-
if (isLast) {
46-
states.push('can-remove');
47-
}
40+
states.push('can-remove');
4841
states.push('can-apply');
4942
}
5043
return states.join('|');

0 commit comments

Comments
 (0)