|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { UndoMigrationCommand } from '../commands/UndoMigrationCommand'; |
| 3 | +import type { TerminalProvider } from '../terminal/TerminalProvider'; |
| 4 | +import { |
| 5 | + dbContextsCache, |
| 6 | + DbContextTreeItem, |
| 7 | +} from '../treeView/DbContextTreeItem'; |
| 8 | +import type { MigrationTreeItem } from '../treeView/MigrationTreeItem'; |
| 9 | +import { AddMigrationAction } from './AddMigrationAction'; |
| 10 | +import type { IAction } from './IAction'; |
| 11 | +import { RemoveMigrationAction } from './RemoveMigrationAction'; |
| 12 | + |
| 13 | +export class ResetMigrationsAction implements IAction { |
| 14 | + constructor( |
| 15 | + private readonly terminalProvider: TerminalProvider, |
| 16 | + private readonly workspaceRoot: string, |
| 17 | + private readonly dbContext: string, |
| 18 | + private readonly project: string, |
| 19 | + private readonly item: MigrationTreeItem, |
| 20 | + ) {} |
| 21 | + |
| 22 | + public async run() { |
| 23 | + const migrations = dbContextsCache.get( |
| 24 | + DbContextTreeItem.getCacheId( |
| 25 | + this.workspaceRoot, |
| 26 | + this.project, |
| 27 | + this.dbContext, |
| 28 | + ), |
| 29 | + ); |
| 30 | + const index = (migrations || []).indexOf(this.item); |
| 31 | + if (index === -1) { |
| 32 | + return; |
| 33 | + } |
| 34 | + const migrationsToReset = migrations?.slice(index) || []; |
| 35 | + if (!migrationsToReset.length) { |
| 36 | + return; |
| 37 | + } |
| 38 | + const options = ['Yes', 'Cancel'] as const; |
| 39 | + const answer = await vscode.window.showInformationMessage( |
| 40 | + 'Any manual migration modifications will be lost! Are you sure you want to do this?', |
| 41 | + ...options, |
| 42 | + ); |
| 43 | + if (answer === 'Cancel') { |
| 44 | + return; |
| 45 | + } |
| 46 | + const migrationName = await vscode.window.showInputBox({ |
| 47 | + title: 'Enter Migration Name', |
| 48 | + prompt: 'EG: MigrationName', |
| 49 | + ignoreFocusOut: true, |
| 50 | + }); |
| 51 | + if (!migrationName) { |
| 52 | + return ''; |
| 53 | + } |
| 54 | + await new UndoMigrationCommand( |
| 55 | + this.terminalProvider, |
| 56 | + this.item, |
| 57 | + false, |
| 58 | + ).run(); |
| 59 | + for (let i = 0; i < migrationsToReset.length; i++) { |
| 60 | + await new RemoveMigrationAction( |
| 61 | + this.terminalProvider, |
| 62 | + this.workspaceRoot, |
| 63 | + this.dbContext, |
| 64 | + this.project, |
| 65 | + false, |
| 66 | + ).run(); |
| 67 | + } |
| 68 | + await new AddMigrationAction( |
| 69 | + this.terminalProvider, |
| 70 | + this.workspaceRoot, |
| 71 | + this.dbContext, |
| 72 | + this.project, |
| 73 | + migrationName, |
| 74 | + ).run(); |
| 75 | + } |
| 76 | +} |
0 commit comments