This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code lens for running migrations
Adds code lens to run migrations to specific versions in the terminal. This is convenient because it allows developers to quickly rollback or fast-forward to specific schema versions with a click.
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as vscode from "vscode"; | ||
|
||
import { Telemetry } from "./telemetry"; | ||
import { Workspace } from "./workspace"; | ||
|
||
export class MigrationController { | ||
private terminal: vscode.Terminal | undefined; | ||
private readonly telemetry: Telemetry; | ||
private readonly currentWorkspace: () => Workspace | undefined; | ||
|
||
constructor( | ||
_context: vscode.ExtensionContext, | ||
telemetry: Telemetry, | ||
currentWorkspace: () => Workspace | undefined, | ||
) { | ||
this.telemetry = telemetry; | ||
this.currentWorkspace = currentWorkspace; | ||
|
||
vscode.window.onDidCloseTerminal((terminal: vscode.Terminal): void => { | ||
if (terminal === this.terminal) this.terminal = undefined; | ||
}); | ||
} | ||
|
||
async runMigrationInTerminal(command?: string) { | ||
// eslint-disable-next-line no-param-reassign | ||
command ??= "bin/rails db:migrate"; | ||
|
||
if (this.terminal === undefined) { | ||
this.terminal = this.getTerminal(); | ||
} | ||
|
||
this.terminal.show(); | ||
this.terminal.sendText(command); | ||
|
||
const workspace = this.currentWorkspace(); | ||
|
||
if (workspace?.lspClient?.serverVersion) { | ||
await this.telemetry.sendCodeLensEvent( | ||
"migrate_in_terminal", | ||
workspace.lspClient.serverVersion, | ||
); | ||
} | ||
} | ||
|
||
// Get an existing terminal or create a new one. For multiple workspaces, it's important to create a new terminal for | ||
// each workspace because they might be using different Ruby versions. If there's no workspace, we fallback to a | ||
// generic name | ||
private getTerminal() { | ||
const workspace = this.currentWorkspace(); | ||
const name = workspace | ||
? `${workspace.workspaceFolder.name}: migration` | ||
: "Ruby LSP: migration"; | ||
|
||
const previousTerminal = vscode.window.terminals.find( | ||
(terminal) => terminal.name === name, | ||
); | ||
|
||
return previousTerminal | ||
? previousTerminal | ||
: vscode.window.createTerminal({ | ||
name, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters