> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sdf.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Deprecating a model

## Overview

In the previous tutorial, we discovered `app_installs` was replaced by `app_installs_v2`.

This tutorial will walk through the deprecation of `app_installs` and demonstrate
how SDF can help overcome the two big hurdles in deprecating a model:

1. Preventing prod from breaking by tracking and resolving downstream dependencies
2. Setting up guardrails to prevent future mistakes

## Prerequisites

Completion of the [previous tutorial](/tutorials/debugging).

## Setup

### Reference to Metadata Files

SDF's semantic understanding is based on metadata files, defining your custom business
logic, and smart propagation of values. To get started, we need to reference the metadata
directory by simply including the relevant paths in our workspace configuration file, `workspace.sdf.yml`.

Open the file `workspace.sdf.yml` and uncomment the following:

```yml workspace.sdf.yml theme={null}
...
# Uncomment here to begin the "Deprecating a Model" tutorial >>>>>>
- path: classifications  # Classifiers
    type: metadata
- path: reports  # Reports based on SDF s information schema
    type: report
- path: checks  # Checks against SDF's information schema
    type: check
# <<<<<<<
```

<Note>
  Note

  * The classifications folder contains one YAML file for table classifiers, and one for column classifiers.
  * The metadata folder is structured based on our models folder.
  * The reports folder is currently empty.

  We will talk about all of these a bit later.
</Note>

Now, the paths are included in our project.

## Guide

<Steps>
  <Step title="Before Deprecation">
    Before deprecating a model, we can run an impact analysis on the its
    downstream dependencies using SDF's lineage command. We simply run:

    ```shell theme={null}
    sdf lineage staging.app_installs --forward --show result
    ```

    <div className="bg-[#0F1117] dark:bg-codeblock rounded-xl dark:ring-1 dark:ring-gray-800/50 relative">
      <pre style={{ fontFamily: 'monospace', backgroundColor: 'transparent' }} className="language-shell">
        <code className="language-shell">
          Table: moms\_flower\_shop.staging.app\_installs

          moms\_flower\_shop.staging.app\_installs.campaign\_id

          moms\_flower\_shop.staging.app\_installs.campaign\_name
          │
          │ copy
          └──────┐
                 moms\_flower\_shop.analytics.agg\_installs\_and\_campaigns.campaign\_name

          moms\_flower\_shop.staging.app\_installs.campaign\_type

          moms\_flower\_shop.staging.app\_installs.customer\_id
          │
          │ mod
          └──────┐
                 moms\_flower\_shop.analytics.agg\_installs\_and\_campaigns.distinct\_installs

          moms\_flower\_shop.staging.app\_installs.event\_id

          moms\_flower\_shop.staging.app\_installs.install\_time
          │
          │ mod
          └──────┐
                 moms\_flower\_shop.analytics.agg\_installs\_and\_campaigns.install\_date

          moms\_flower\_shop.staging.app\_installs.platform
          │
          │ copy
          └──────┐
                 moms\_flower\_shop.analytics.agg\_installs\_and\_campaigns.platform
        </code>
      </pre>
    </div>

    <Tip>
      **Wow!** The model `agg_installs_and_campaigns` is also using the
      old version table!
    </Tip>
  </Step>

  <Step title="Classify the Model as Deprecated">
    The file `classifications/table_classifiers.sdf.yml` holds definitions of
    a table level classifier called `TABLE_STATUS`. You can see that one of the
    table status options is `deprecated`.

    To add the classifier to our table, navigate to the table's metadata file
    `metadata/staging/app_installs.sdf.yml`. Remove the commented
    section assigning the `TABLE_STATUS.deprecated` classifier to the table:

    ```yml metadata/staging/app_installs.sdf.yml theme={null}
    # Uncomment here to add a "deprecated" classifier to the table
    classifiers:
      - TABLE_STATUS.deprecated
    ```

    Let's compile and see how it looks:

    ```shell theme={null}
    sdf compile staging.app_installs --show result
    ```

    <div className="bg-[#0F1117] dark:bg-codeblock rounded-xl dark:ring-1 dark:ring-gray-800/50 relative">
      <pre style={{ fontFamily: 'monospace', backgroundColor: 'transparent' }} className="language-error">
        <code className="language-error">
          Schema moms\_flower\_shop.staging.app\_installs
                 This table is a staging table which adds campaign information to app install events
                 \[TABLE\_STATUS.deprecated]
          ┌───────────────┬───────────┬────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
          │ column\_name   ┆ data\_type ┆ classifier ┆ description                                                                                                                                  │
          ╞═══════════════╪═══════════╪════════════╪══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╡
          │ event\_id      ┆ bigint    ┆            ┆                                                                                                                                              │
          │ customer\_id   ┆ bigint    ┆            ┆ The identifier of the customer that performed the event                                                                                      │
          │ install\_time  ┆ timestamp ┆            ┆                                                                                                                                              │
          │ platform      ┆ varchar   ┆            ┆ iOS or Android                                                                                                                               │
          │ campaign\_id   ┆ bigint    ┆            ┆ The identifier of the campaign that is associated with the event                                                                             │
          │ campaign\_name ┆ varchar   ┆            ┆ The campaign name associated with the campaign\_id                                                                                            │
          │ campaign\_type ┆ varchar   ┆            ┆ A substring of the campaign name contain the campaign type.  Supported types - instagram\_ads, friends\_referrals, facebook\_ads, google\_search │
          │               ┆           ┆            ┆                                                                                                                                              │
          └───────────────┴───────────┴────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
        </code>
      </pre>
    </div>

    <Tip>
      Read more about classifiers in the [Classifiers Guide](/guide/basics/classifiers).
    </Tip>
  </Step>

  <Step title="Create a Report of References to Deprecated Tables">
    We can create an [SDF report](/guide/data-quality/reports) to receive an instant snapshot of
    all models referencing deprecated tables throughout the **entire data warehouse**.

    Create a new SQL file called `deprecated_table_reference.sql` in the
    `reports/` directory. Copy the following query to your new file:

    ```sql reports/deprecated_table_reference.sql theme={null}
    WITH
    deprecated_tables AS (
        SELECT
            table_id
        FROM sdf.information_schema.tables 
        WHERE 
            CONTAINS(classifiers, 'TABLE_STATUS.deprecated')
    )

    SELECT 
        to_table_id AS table_id, 
        from_table_id AS upstream_deprecated_table_id
    FROM sdf.information_schema.table_lineage
    WHERE from_table_id IN (SELECT table_id FROM deprecated_tables)
        AND to_table_id IS NOT NULL

    ```

    <Tip>
      To learn more about SDF's rich information schema and how you can
      leverage it to write your custom own reports, read our [information schema
      reference page](/reference/sdf-information-schema).
    </Tip>

    Thanks to SDF's lineage tracking capabilities, we already know that `agg_installs_and_campaigns`
    is referencing the old table. It's a good test to check -
    we expect it to fail.

    Let's check it out (pun intended):

    ```shell theme={null}
    sdf report deprecated_table_reference
    ```

    <div className="bg-[#0F1117] dark:bg-codeblock rounded-xl dark:ring-1 dark:ring-gray-800/50 relative">
      <pre style={{ fontFamily: 'monospace', backgroundColor: 'transparent' }} className="language-error">
        <code className="language-error">
          Report moms\_flower\_shop.pub.deprecated\_table\_reference
          ┌───────────────────────────────────────────────────────┬───────────────────────────────────────┐
          │ table\_id                                              ┆ upstream\_deprecated\_table\_id          │
          ╞═══════════════════════════════════════════════════════╪═══════════════════════════════════════╡
          │ moms\_flower\_shop.analytics.agg\_installs\_and\_campaigns ┆ moms\_flower\_shop.staging.app\_installs │
          └───────────────────────────────────────────────────────┴───────────────────────────────────────┘
          1 rows.
        </code>
      </pre>
    </div>

    It works! Now, we should fix `agg_installs_and_campaigns`.

    <Tip>
      For more examples on getting health reports of your data warehouse and aggregate custom logic and static checks,
      read our full [Reports Guide](/guide/data-quality/reports).
    </Tip>
  </Step>

  <Step title="Resolve Downstream Dependencies">
    To make sure we won't break prod if we delete our table, we
    can choose to resolve the dependency in one of two ways:

    * If the model is not being used, we can just delete it altogether.
    * Otherwise, we can update the `FROM` statement of `models/analytics/agg_installs_and_campaigns.sql`
      like we did for our previous model:

    ```sql models/analytics/agg_installs_and_campaigns.sql theme={null}
    ...
    FROM staging.app_installs_v2  -- Change from app_installs to app_installs_v2
    ...
    ```

    Let's run our report again:

    ```
    sdf report deprecated_table_reference
    ```

    All clear!

    <div className="bg-[#0F1117] dark:bg-codeblock rounded-xl dark:ring-1 dark:ring-gray-800/50 relative">
      <pre style={{ fontFamily: 'monospace', backgroundColor: 'transparent' }} className="language-error">
        <code className="language-error">
          Report moms\_flower\_shop.pub.deprecated\_table\_reference
          ┌──────────┬──────────────────────────────┐
          │ table\_id ┆ upstream\_deprecated\_table\_id │
          ╞══════════╪══════════════════════════════╡
          └──────────┴──────────────────────────────┘
          0 rows.
        </code>
      </pre>
    </div>
  </Step>
</Steps>

That was really cool, ha? Let's explore what other cool things we can
do with SDF's information schema and semantic understanding in our
next tutorial - [enriching your warehouse](/tutorials/enriching-your-warehouse)

## Related Topics

<CardGroup cols={3}>
  <Card title="Workspace Configuration" href="/guide/setup/workspaces">
    Learn all you need to know about workspaces
  </Card>

  <Card title="Classifiers" href="/guide/basics/classifiers">
    Learn all the ways classifiers can help enhance your workspace
  </Card>

  <Card title="Reports" href="/guide/data-quality/reports">
    Learn more about reports to get immediate insights about your data Warehouse
  </Card>
</CardGroup>
