mirror of
https://github.com/BillyOutlast/posthog.git
synced 2026-02-04 03:01:23 +01:00
Product Folders
This is the (future) home for all PostHog products (RFC).
Dev guidelines
- Each high level product should have its own folder.
- Please keep the top level folders
under_scorecased, as dashes make it hard to import files in some languages (e.g. Python).
- Please keep the top level folders
- Each product has a few required files / folders:
manifest.tsx- describes the product's features. All manifest files are combined intofrontend/src/products.tsxon build.package.json- describes the frontend dependencies. Ideally they should all bepeerDependenciesof whatever is infrontend/package.json__init__.py- marks the directory as a python package, needed if you include the backend.frontend/- React frontend code. We run prettier/eslint only on files in thefrontendfolder on commit.backend/- Python backend code. It's treated as a separate django app.
Adding a new product
- Create a new folder
products/your_product_name, keep it underscore-cased. - Create a
manifest.tsxfile- Describe the product's frontend
scenes,routes,urls, file system types, and project tree (navbar) items. - All manifest files are combined into a single
frontend/src/products.tsxfile on build. - NOTE: we don't copy imports into
products.tsx. If you add new icons, update the imports manually infrontend/src/products.tsx. It only needs to be done once. - NOTE: if you want to add a link to the old pre-project-tree navbar, do so manually in
frontend/src/layout/navigation-3000/navigationLogic.tsx
- Describe the product's frontend
- Create a
package.jsonfile:- Keep the package name as
@posthog/products-your-product-name. Include@posthog/products-in the name. - Update the global
frontend/package.json: add your new npm package underdependencies. - If your scenes are linked up with the right paths, things should just work.
- Each scene can either export a React component as its default export, or define a
export const scene: SceneExport = { logic, component }object to export both a logic and a component. This way the logic stays mounted when you move away from the page. This is useful if you don't want to reload everything each time the scene is loaded.
- Keep the package name as
- Create a
__init__.pyfile if your product has python backend code.- This is needed to mark the directory as a Python package / Django app.
- Modify
posthog/settings/web.pyand add your new product underPRODUCTS_APPS. - Modify
tach.tomland add a new block for your product. We usetachto track cross-dependencies between python apps. - Modify
posthog/api/__init__.pyand add your API routes as you normally would (e.g.import products.early_access_features.backend.api as early_access_feature) - NOTE: we will automate some of these steps in the future, but for now, please do them manually.
Adding or moving backend models and migrations
- Create or move your backend models under the product's
backend/folder. - Import and export them under
posthog/models/__init__.py(seeEarlyAccessFeaturefor an example) - Create a
products/your_product_name/backend/migrationsfolder. - Run
python manage.py makemigrations your_product_name -n initial_migration - If this is a brand-new model, you're done.
- If you're moving a model from the old
posthog/models/folder, there are more things to do:- Make sure the model's
Metaclass hasdb_table = 'old_table_name'set along withmanaged = True. - Run
python manage.py makemigrations posthog -n remove_old_product_name - The generated migrations will want to
DROP TABLEyour old model, andCREATE TABLEthe new one. This is not what we want. - Instead, we want to run
migrations.SeparateDatabaseAndStatein both migrations. - Follow the example in
posthog/migrations/0548_migrate_early_access_features.pyandproducts/early_access_features/migrations/0001_initial_migration.py. - Move all operations into
state_operations = []and keep thedatabase_operations = []empty in both migrations. - Run and test this a few times before merging. Data loss is irreversible.
- Make sure the model's
TODO:
- A story for Python testing - run tests automatically, only test apps that changed, etc