Skip to content

Commit

Permalink
ps script fra egen laptop
Browse files Browse the repository at this point in the history
  • Loading branch information
espenhoh committed May 7, 2024
1 parent 839bdf5 commit fe1096c
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions start_vscode_dbt_laptop.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Path to wher you would like to place the python virtual env
$dbtEnv_path = ".dbtenv"




#Function to set up a virtual env for dbt
function Add-dbtenv {
# Create environment (https://docs.python.org/3/library/venv.html)
python -m venv $dbtEnv_path
# And activate
iex $dbtEnv_path"/Scripts/activate.ps1"
# Install these packages to forsce python to use Windows Certificate Store.
python -m pip install setuptools-scm pip-system-certs --trusted-host pypi.org --trusted-host files.pythonhosted.org
# Latest pip
python -m pip install --upgrade pip
# Install the latest dbt
python -m pip install -r https://raw.githubusercontent.com/navikt/dbt-i-nav/main/requirements.txt
}

#Check if environment exists
$dbtEnv_exists = Test-Path -Path $dbtEnv_path
if (-Not $dbtEnv_exists) {
Add-dbtenv
}
Else{
if ((Read-Host -Prompt "Update $dbtEnv_path`? (y/n)") -eq "y") {
Add-dbtenv
}
Else {
iex $dbtEnv_path"/Scripts/activate.ps1"
}
}

#path to dbt folder
if ($args[0]) {
$dbtPath = $args[0]
Write-Host "Starting dbt environment. Please wait ..."
} else {
Write-Host "Missing dbt project path. Add c:\path\to\project\ as argument to this script"
exit
}

#Database schema
if ($args[1]) {
# Database schema
$schema = $args[1].ToUpper()
Write-Host "Setting schema to: $schema"
} else {
Write-Host "Missing schema. Pass schema as second argument this script."
exit
}


Try {
Write-Host "Checking if path $dbtPath exists ..."
Set-Location $dbtPath -ErrorAction Stop
Write-Host "Path $dbtPath found, continuing ..."
}
Catch [System.Management.Automation.ItemNotFoundException]{
Write-Host "Unable to find path:" $dbtPath"."
exit
}




$dbt_project_file = Test-Path -Path $dbtPath"\dbt_project.yml"

if ($dbt_project_file) {
Write-Host "$dbtPath is a valid dbt project. Welcome!"
Write-Host "Please enter environment details!"
} else {
Write-Host "Invalid dbt project: "$dbtPath" (missing dbt_project.yml)"
exit
}


$env:DBT_PROFILES_DIR = $dbtPath




#Targest database in profiles.yml
$target = Read-Host -Prompt "Target db"
$target = $target.ToUpper()

#Database creds
$creds = Get-Credential
$username = $creds.Username
$username += "[$schema]"

$env:DBT_DB_SCHEMA = $schema
$env:DBT_DB_TARGET = $target
$env:DBT_ENV_SECRET_USER = $username
$env:DBT_ENV_SECRET_PASS = $creds.GetNetworkCredential().password



dbt deps


$git_repo_detected = Test-Path -Path $dbtPath"\.git"

if ($git_repo_detected) {
code .
} else {
code ..
}

0 comments on commit fe1096c

Please sign in to comment.