Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documents different approach for initial population of data #6691

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Kircheneer
Copy link
Contributor

Closes #6671

What's Changed

See attached issue.

TODO

  • I would appreciate some more input as I don't feel like my overview is comprehensive yet

Copy link
Contributor

@glennmatthews glennmatthews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a stab at this!


In many cases, an app may wish to make use of Nautobot's various extensibility features, such as [custom fields](../../../../user-guide/platform-functionality/customfield.md) or [relationships](../../../../user-guide/platform-functionality/relationship.md). It can be useful for an app to automatically create a custom field definition or relationship definition as a consequence of being installed and activated, so that everyday usage of the app can rely upon these definitions to be present.

To make this possible, Nautobot provides a custom [signal](https://docs.djangoproject.com/en/stable/topics/signals/), `nautobot_database_ready`, that apps can register to listen for. This signal is triggered when `nautobot-server migrate` or `nautobot-server post_upgrade` is run after installing an app, and provides an opportunity for the app to make any desired additions to the database at this time.
Furthermore, sometimes apps might want to create a baseline of available data. This could, for example, be a baseline of circuit providers for an SSoT app that synchronizes circuit data.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to define SSoT here as it's the first time it's referenced in the doc?

A couple other examples would probably be helpful to get developers thinking; the one I've seen the most is creating default Status and/or Role records (or just adding to the content_types of existing records) to use with an app's new data models.


**Con**:

- Prevent Nautobot from starting when there are errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Re-runs every time nautobot-server migrate or nautobot-server post_upgrade is run
    • Must be idempotent (doesn't introduce duplicate records or error out if run repeatedly)
    • May recreate or revert records that a user intentionally deleted or altered
  • Nautobot startup/upgrade performance?
  • The possibility of later user modifications to data must be accounted for and handled in code.

**Pro**:

- Execution: Automatic
- Provide a clean way to delete any associated objects when uninstalling the app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Runs exactly once (on nautobot-server migrate or nautobot-server post_upgrade), so idempotence is not a concern, and will not later revert or recreate records that a user intentionally deleted or altered.
  • Users can later modify the created data like any other record, if a need arises.


- Implementation: Medium
- Prevent Nautobot from starting when there are errors
- Immutable—if you add new objects, you need to add a new migration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Immutableif you add new objects, you need to add a new migration
- Immutableif you later need to add new objects or alter existing one, you need to add a new migration

**Pro**:

- Execution: Automatic
- Implementation: Easy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can be rerun as needed (opposite of "Con" on data migrations below)
  • Can be updated "in place" in later App releases (opposite of "Con" on data migrations below)
  • Users can later modify the created data like any other record, if a need arises.

**Con**:

- Data is not available in the DB/API/GUI until the process that uses it runs
- Some care has to be taken to not duplicate information if multiple things depend on this data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Later modifications to the object definitions may be error-prone if a given record/set of attributes is created/referenced in many locations
  • User modifications to the data may result in side effects (renaming a Status may result in a new Status with the original name being recreated next time the code runs, etc.)


- Execution: Manual
- Implementation: Medium
- Creating/updating them is not straight-forward, especially for big data sets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The possibility of later user modifications to data must be accounted for and handled in code.

**Pro**:

- Have good support for usage in unit tests
- Have good performance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Users can later modify the created data like any other record, if a need arises.


**Con**:

- Execution: Manual
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The possibility of later user modifications to data must be accounted for and handled in code.

Comment on lines +71 to +76
- Signal Handlers on `nautobot_database_ready` (see above)
- [Django Onboard Tooling](https://docs.djangoproject.com/en/4.2/howto/initial-data/)
- [Django Data Migrations](https://docs.djangoproject.com/en/4.2/topics/migrations/#data-migrations)
- [Django Fixtures](https://docs.djangoproject.com/en/4.2/topics/db/fixtures/#fixtures)
- [Design Builder Jobs](https://docs.nautobot.com/projects/design-builder/en/latest/)
- Creating data in-place where needed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the repetition of pros/cons below, I wonder if it'd be possible and reasonable to create a table here of approaches versus their use cases/pros/cons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Documentation on population of initial data
2 participants