Skip to content

Commit 01ed7f1

Browse files
committed
Undo migrations
1 parent a61163b commit 01ed7f1

14 files changed

+83
-126
lines changed

icons/replace-all_dark.svg

+3
Loading

icons/replace-all_light.svg

+3
Loading

icons/replace_dark.svg

+3
Loading

icons/replace_light.svg

+3
Loading

package.json

+15-15
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@
4848
}
4949
},
5050
{
51-
"command": "entityframework.generateScript",
52-
"title": "Generate Script",
51+
"command": "entityframework.undoMigration",
52+
"title": "Undo Migration",
5353
"icon": {
54-
"light": "icons/file-code_light.svg",
55-
"dark": "icons/file-code_dark.svg"
54+
"light": "icons/reply_light.svg",
55+
"dark": "icons/reply_dark.svg"
5656
}
5757
},
5858
{
59-
"command": "entityframework.runMigrations",
60-
"title": "Run migrations to here",
59+
"command": "entityframework.generateScript",
60+
"title": "Generate Script",
6161
"icon": {
62-
"light": "icons/run-all_light.svg",
63-
"dark": "icons/run-all_dark.svg"
62+
"light": "icons/file-code_light.svg",
63+
"dark": "icons/file-code_dark.svg"
6464
}
6565
}
6666
],
@@ -91,7 +91,7 @@
9191
"when": "false"
9292
},
9393
{
94-
"command": "entityframework.runMigrations",
94+
"command": "entityframework.undoMigration",
9595
"when": "false"
9696
},
9797
{
@@ -119,22 +119,22 @@
119119
},
120120
{
121121
"command": "entityframework.runMigration",
122-
"when": "viewItem =~ /^migration-.*\\|?can-apply-single\\|?.*$/",
122+
"when": "viewItem =~ /^migration-.*\\|?can-apply\\|?.*$/",
123123
"group": "inline@2"
124124
},
125125
{
126126
"command": "entityframework.runMigration",
127-
"when": "viewItem =~ /^migration-.*\\|?can-apply-single\\|?.*$/",
127+
"when": "viewItem =~ /^migration-.*\\|?can-apply\\|?.*$/",
128128
"group": "context@1"
129129
},
130130
{
131-
"command": "entityframework.runMigrations",
132-
"when": "viewItem =~ /^migration-.*\\|?can-apply-to-here\\|?.*$/",
131+
"command": "entityframework.undoMigration",
132+
"when": "viewItem =~ /^migration-.*\\|?can-undo\\|?.*$/",
133133
"group": "inline@2"
134134
},
135135
{
136-
"command": "entityframework.runMigrations",
137-
"when": "viewItem =~ /^migration-.*\\|?can-apply-to-here\\|?.*$/",
136+
"command": "entityframework.undoMigration",
137+
"when": "viewItem =~ /^migration-.*\\|?can-undo\\|?.*$/",
138138
"group": "context@1"
139139
},
140140
{

src/commands/CommandProvider.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { GenerateScriptCommand } from './GenerateScriptCommand';
1111
import { RefreshTreeCommand } from './RefreshTreeCommand';
1212
import { RemoveMigrationCommand } from './RemoveMigrationCommand';
1313
import { RunMigrationCommand } from './RunMigrationCommand';
14-
import { RunMigrationsCommand } from './RunMigrationsCommand';
14+
import { UndoMigrationCommand } from './UndoMigrationCommand';
1515

1616
export class CommandProvider extends Disposable {
1717
constructor(
18-
private readonly treeDataProvider: TreeDataProvider,
19-
private readonly terminalProvider: TerminalProvider,
18+
treeDataProvider: TreeDataProvider,
19+
terminalProvider: TerminalProvider,
2020
) {
2121
super();
2222
this.registerCommand(
@@ -36,9 +36,9 @@ export class CommandProvider extends Disposable {
3636
new RunMigrationCommand(terminalProvider, item),
3737
);
3838
this.registerCommand(
39-
RunMigrationsCommand.commandName,
39+
UndoMigrationCommand.commandName,
4040
(item?: MigrationTreeItem) =>
41-
new RunMigrationsCommand(terminalProvider, item),
41+
new UndoMigrationCommand(terminalProvider, item),
4242
);
4343
this.registerCommand(
4444
GenerateScriptCommand.commandName,
@@ -48,7 +48,7 @@ export class CommandProvider extends Disposable {
4848
this.registerCommand(
4949
RefreshTreeCommand.commandName,
5050
(clearCache: boolean) =>
51-
new RefreshTreeCommand(this.treeDataProvider, clearCache),
51+
new RefreshTreeCommand(treeDataProvider, clearCache),
5252
);
5353
}
5454

src/commands/RunMigrationsCommand.ts

-28
This file was deleted.

src/commands/UndoMigrationCommand.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { RunMigrationAction } from '../actions/RunMigrationAction';
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 UndoMigrationCommand extends Command {
11+
public static commandName = 'undoMigration';
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 cacheId = DbContextTreeItem.getCacheId(
25+
this.item.solutionFile.workspaceRoot,
26+
this.item.project,
27+
this.item.dbcontext,
28+
);
29+
const migrations = dbContextsCache.get(cacheId);
30+
if (migrations) {
31+
const index = migrations.indexOf(this.item);
32+
const migrationId =
33+
index === 0 ? '0' : migrations[index - 1].migration.id;
34+
return new RunMigrationAction(
35+
this.terminalProvider,
36+
this.item.solutionFile.workspaceRoot,
37+
this.item.dbcontext,
38+
this.item.project,
39+
migrationId,
40+
).run();
41+
}
42+
}
43+
}

src/extension.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,18 @@ const subscriptions: vscode.Disposable[] = [];
1212

1313
export async function activate(_context: vscode.ExtensionContext) {
1414
const solutionFiles = await SolutionFinder.getSolutionFiles();
15-
const terminal = new Terminal();
16-
// const terminal = vscode.window.createTerminal({
17-
// name: `EF Migrations`,
18-
// pty,
19-
// });
20-
const terminalProvider = new TerminalProvider(terminal);
2115
const dbContextModelContextWatcher = new DbContextModelSnapshotWatcher(
2216
solutionFiles,
2317
);
2418
const migrationTreeItemDecorationProvider =
2519
new MigrationTreeItemDecorationProvider();
2620
const treeDataProvider = new TreeDataProvider(solutionFiles);
21+
const terminalProvider = new TerminalProvider(new Terminal());
2722
const commandProvider = new CommandProvider(
2823
treeDataProvider,
2924
terminalProvider,
3025
);
3126

32-
// terminal.show();
33-
34-
// pty.setAction(async terminal => {
35-
// terminal.write('NEW Type and press enter to echo the text\r\n\r\n');
36-
// await new Promise<undefined>(res => setTimeout(() => res(undefined), 2000));
37-
// terminal.write('Done\n');
38-
// });
39-
40-
// pty.onDidOpen(async () => {
41-
// await pty.exec();
42-
// });
43-
4427
subscriptions.push(
4528
dbContextModelContextWatcher,
4629
migrationTreeItemDecorationProvider,

src/terminal/Terminal.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as vscode from 'vscode';
2-
import type { ChildProcessWithoutNullStreams } from 'node:child_process';
3-
import { spawn } from 'node:child_process';
2+
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
43

5-
import { EventWaiter } from './EventWaiter';
4+
import { EventWaiter } from '../util/EventWaiter';
65
import { getEnvConfig } from '../config/config';
76

87
const NL = '\n';

src/terminal/TerminalOld.ts

-30
This file was deleted.

src/treeView/DbContextTreeItem.ts

-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ export class DbContextTreeItem extends TreeItem {
6262
this.project,
6363
migration,
6464
index === migrations.length - 1,
65-
index === 0,
66-
index > 0 ? migrations[index - 1] : undefined,
6765
),
6866
);
6967
dbContextsCache.set(this.cacheId, children);

0 commit comments

Comments
 (0)