Repository: fal-ai/dbt-fal

Issue:
Add the dbt target.name as a property on the context object
**Context**
I am sending slack messages and to keep things simple I have opted for one channel. (for some given set of models, that's handled through meta anyway i.e. slack_channel_id). 

However, doing this - it's hard to tell what the given environment was if you use one channel for all dbt invocations.

**Describe the solution you'd like**
I would like to be able to use context to access  dbt properties such as the target.name e.g.

```
    context.target.name # dev, test, prod, etc.
```
**Describe alternatives you've considered**
This can be worked around by injecting the {{ target.name }} as a meta field to a model.yml e.g:

```
version: 2

models:
  - name: my_funky_model
    meta:
      SLACK_CHANNEL_ID: XXXXXXXXXX
      TARGET: "{{ target.name }}"
      fal:
        scripts:
          - fal_scripts/notify_slack.py
```

However this does seem redundant/laborious to add to all .yml definitions 


Discussion:

Resolving patch:
diff --git a/src/fal/fal_script.py b/src/fal/fal_script.py
--- a/src/fal/fal_script.py
+++ b/src/fal/fal_script.py
@@ -99,11 +99,21 @@ def __init__(self, config: RuntimeConfig):
             os.path.realpath(os.path.join(config.project_root, config.target_path))
         )
 
+@dataclass
+class ContextTarget:
+    def __init__(self, config: RuntimeConfig):
+        self.profile_name = config.profile_name
+        self.name = config.target_name
+        self.threads = config.threads
+        self.type = config.credentials.type
+        self.database = config.credentials.database
+        self.schema = config.credentials.schema
 
 @dataclass
 class Context:
     current_model: Union[CurrentModel, None]
     config: ContextConfig
+    target: ContextTarget
     _arguments: Optional[Dict[str, Any]] = field(repr=False, default=None)
 
     @property
@@ -233,9 +243,12 @@ def model_name(self):
         return "<GLOBAL>" if self.is_global else self.model.name  # type: ignore
 
     def _build_script_context(self) -> Context:
-        context_config = ContextConfig(self.faldbt._config)
+        config: RuntimeConfig = self.faldbt._config
+        context_config = ContextConfig(config)
+        target = ContextTarget(config)
+
         if self.is_global:
-            return Context(current_model=None, config=context_config)
+            return Context(current_model=None, target=target, config=context_config)
 
         model: DbtModel = self.model  # type: ignore
 
@@ -264,6 +277,7 @@ def _build_script_context(self) -> Context:
 
         return Context(
             current_model=current_model,
+            target=target,
             config=context_config,
             _arguments=self.hook_arguments,
         )


Test patch:
diff --git a/integration_tests/features/context_info.feature b/integration_tests/features/context_info.feature
--- a/integration_tests/features/context_info.feature
+++ b/integration_tests/features/context_info.feature
@@ -7,6 +7,18 @@ Feature: Context object information
       dbt run --profiles-dir $profilesDir --project-dir $baseDir
       """
 
+  Scenario: Get target info in post hook
+    When the following command is invoked:
+      """
+      fal flow run --profiles-dir $profilesDir --project-dir $baseDir --select some_model
+      """
+    Then the following models are calculated:
+      | some_model |
+    Then the following scripts are ran:
+      | some_model.lists.py | some_model.context.py |
+    And the script some_model.context.py output file has the lines:
+      | target profile: fal_test |
+
   Scenario: Get rows affected in post hook for fal flow run
     When the following command is invoked:
       """
diff --git a/integration_tests/projects/005_functions_and_variables/fal_scripts/context.py b/integration_tests/projects/005_functions_and_variables/fal_scripts/context.py
--- a/integration_tests/projects/005_functions_and_variables/fal_scripts/context.py
+++ b/integration_tests/projects/005_functions_and_variables/fal_scripts/context.py
@@ -6,6 +6,10 @@
 extra = ""
 extra += f"context: {context}\n"
 extra += f"model: {context.current_model}\n"
+extra += f"target: {context.target}\n"
+extra += f"target name: {context.target.name}\n"
+extra += f"target profile: {context.target.profile_name}\n"
+extra += f"target database: {context.target.database}\n"
 
 response = context.current_model.adapter_response
 assert response