Skip to content

Commit 249d6fd

Browse files
authored
Support target selector (#1424)
1 parent 4a563f6 commit 249d6fd

File tree

10 files changed

+513
-254
lines changed

10 files changed

+513
-254
lines changed

dbt_core_integration.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ class DbtProject:
344344

345345
def __init__(
346346
self,
347-
target: Optional[str] = None,
347+
target_name: Optional[str] = None,
348348
profiles_dir: Optional[str] = None,
349349
project_dir: Optional[str] = None,
350350
threads: Optional[int] = 1,
@@ -357,7 +357,7 @@ def __init__(
357357
):
358358
self.args = ConfigInterface(
359359
threads=threads,
360-
target=target,
360+
target=target_name,
361361
profiles_dir=profiles_dir,
362362
project_dir=project_dir,
363363
profile=profile,
@@ -398,7 +398,9 @@ def init_config(self):
398398
set_invocation_context(os.environ)
399399
set_from_args(self.args, None)
400400
# Copy over global_flags
401-
self.args.__dict__.update(get_flags().__dict__)
401+
for key, value in get_flags().__dict__.items():
402+
if key not in self.args.__dict__:
403+
self.args.__dict__[key] = value
402404
else:
403405
set_from_args(self.args, self.args)
404406
self.config = RuntimeConfig.from_args(self.args)
@@ -882,3 +884,15 @@ def validate_sql_dry_run(self, compiled_sql: str):
882884
return self.adapter.validate_sql(compiled_sql)
883885
except Exception as e:
884886
raise Exception(str(e))
887+
888+
def get_target_names(self):
889+
from dbt.config.profile import read_profile
890+
profile = read_profile(self.args.profiles_dir)
891+
project = profile[self.project_name]
892+
if "outputs" in project:
893+
outputs = project["outputs"]
894+
return outputs.keys()
895+
return []
896+
897+
def set_selected_target(self, target: str):
898+
self.args.target = target

src/commands/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ProgressLocation,
1616
TextEditorDecorationType,
1717
DecorationRangeBehavior,
18+
QuickPickItem,
1819
} from "vscode";
1920
import { SqlPreviewContentProvider } from "../content_provider/sqlPreviewContentProvider";
2021
import { RunModelType } from "../domain";
@@ -568,6 +569,44 @@ export class VSCodeCommands implements Disposable {
568569
this.notebookController.createNotebook(args);
569570
},
570571
),
572+
commands.registerCommand(
573+
"dbtPowerUser.openTargetSelector",
574+
async (targets, project: DBTProject, statusBar) => {
575+
try {
576+
if (!targets) {
577+
return;
578+
}
579+
this.dbtTerminal.debug(
580+
"OpenTargetSelector",
581+
"Showing following targets",
582+
targets,
583+
);
584+
const target = await window.showQuickPick(targets, {
585+
title: "Select your target",
586+
canPickMany: false,
587+
});
588+
if (target) {
589+
await project.setSelectedTarget(target);
590+
await statusBar.updateStatusBar();
591+
this.dbtTerminal.info(
592+
"OpenTargetSelector",
593+
"Selecting target",
594+
true,
595+
target,
596+
);
597+
}
598+
} catch (error) {
599+
this.dbtTerminal.error(
600+
"OpenTargetSelector",
601+
"An error occurred while changing target",
602+
error,
603+
);
604+
window.showErrorMessage(
605+
"An error occurred while changing target: " + error,
606+
);
607+
}
608+
},
609+
),
571610
commands.registerCommand(
572611
"dbtPowerUser.createSqlFile",
573612
async (args: { code?: string; fileName?: string } | undefined) => {

src/dbt_client/dbtCloudIntegration.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,18 @@ export class DBTCloudProjectIntegration
336336
this.dbtPath = getDBTPath(this.pythonEnvironment, this.terminal);
337337
}
338338

339+
async setSelectedTarget(targetName: string): Promise<void> {
340+
throw new Error("Method not implemented.");
341+
}
342+
343+
async getTargetNames(): Promise<Array<string>> {
344+
throw new Error("Method not implemented.");
345+
}
346+
347+
getSelectedTarget(): string | undefined {
348+
throw new Error("Method not implemented.");
349+
}
350+
339351
getTargetPath(): string | undefined {
340352
return this.targetPath;
341353
}

src/dbt_client/dbtCoreIntegration.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export class DBTCoreProjectIntegration
241241
private profilesDir?: string;
242242
private targetPath?: string;
243243
private adapterType?: string;
244+
private targetName?: string;
244245
private version?: number[];
245246
private packagesInstallPath?: string;
246247
private modelPaths?: string[];
@@ -370,6 +371,7 @@ export class DBTCoreProjectIntegration
370371
async refreshProjectConfig(): Promise<void> {
371372
await this.createPythonDbtProject(this.python);
372373
await this.python.ex`project.init_project()`;
374+
this.targetName = await this.findSelectedTarget();
373375
this.targetPath = await this.findTargetPath();
374376
this.modelPaths = await this.findModelPaths();
375377
this.seedPaths = await this.findSeedPaths();
@@ -379,6 +381,25 @@ export class DBTCoreProjectIntegration
379381
this.adapterType = await this.findAdapterType();
380382
}
381383

384+
async findSelectedTarget(): Promise<string> {
385+
return await this.python.lock(
386+
(python) => python`to_dict(project.config.target_name)`,
387+
);
388+
}
389+
390+
async setSelectedTarget(targetName: string): Promise<void> {
391+
await this.python.lock(
392+
(python) => python`project.set_selected_target(${targetName})`,
393+
);
394+
await this.refreshProjectConfig();
395+
}
396+
397+
async getTargetNames(): Promise<Array<string>> {
398+
return await this.python.lock(
399+
(python) => python`to_dict(project.get_target_names())`,
400+
);
401+
}
402+
382403
async executeSQL(
383404
query: string,
384405
limit: number,
@@ -507,6 +528,10 @@ export class DBTCoreProjectIntegration
507528
}
508529
}
509530

531+
getSelectedTarget() {
532+
return this.targetName;
533+
}
534+
510535
getTargetPath(): string | undefined {
511536
return this.targetPath;
512537
}
@@ -798,6 +823,10 @@ export class DBTCoreProjectIntegration
798823
command.addArgument("--profiles-dir");
799824
command.addArgument(this.profilesDir);
800825
}
826+
if (this.targetName) {
827+
command.addArgument("--target");
828+
command.addArgument(this.targetName);
829+
}
801830
command.setExecutionStrategy(this.pythonDBTCommandExecutionStrategy);
802831
return command;
803832
}

src/dbt_client/dbtIntegration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ export interface DBTProjectIntegration extends Disposable {
325325
initializeProject(): Promise<void>;
326326
// called when project configuration is changed
327327
refreshProjectConfig(): Promise<void>;
328+
// Change target
329+
setSelectedTarget(targetName: string): Promise<void>;
330+
getTargetNames(): Promise<Array<string>>;
328331
// retrieve dbt configs
329332
getTargetPath(): string | undefined;
330333
getModelPaths(): string[] | undefined;
@@ -333,6 +336,7 @@ export interface DBTProjectIntegration extends Disposable {
333336
getPackageInstallPath(): string | undefined;
334337
getAdapterType(): string | undefined;
335338
getVersion(): number[] | undefined;
339+
getSelectedTarget(): string | undefined;
336340
// parse manifest
337341
rebuildManifest(): Promise<void>;
338342
// execute queries

0 commit comments

Comments
 (0)