Skip to content

Resolve "Age update automatically every year and DOB(Year mandatory) should be taken"

Merge Request

Overview

This MR updates the user data model by replacing the age field with date_of_birth (DOB).

The goal is to eliminate storing derived data (age) and instead store the source of truth (DOB). Age will now be dynamically calculated when required.


What does this MR do and why?

Previously, the system stored age directly in the database. This approach caused:

  • Data inconsistency over time (age becomes outdated)
  • Redundant data storage
  • Manual updates required to maintain accuracy

Solution

  • Removed age column from the database
  • Introduced date_of_birth column
  • Updated backend logic to calculate age dynamically from DOB

Benefits

  • Ensures accurate and up-to-date age calculation
  • Follows best practice (store source, not derived data)
  • Reduces data inconsistency issues

Changes Made

Database

  • Removed: age column from users table
  • Added: date_of_birth (DATE type)

Backend

  • Updated User model/schema
  • Added utility function to calculate age from DOB
  • Updated API responses to return computed age

API Changes

  • Request:

    • date_of_birth added
    • age removed
  • Response:

    • age is dynamically calculated and returned

Files Modified

  • models/user.py
  • schemas/user_schema.py
  • services/user_service.py
  • migrations/xxxx_add_dob_remove_age.py

Technical Details

Root Cause

Storing age directly leads to stale data since it changes over time.

Fix

  • Store date_of_birth instead
  • Calculate age dynamically using current date

Example Logic

from datetime import date

def calculate_age(dob: date) -> int:
    today = date.today()
    return today.year - dob.year - (
        (today.month, today.day) < (dob.month, dob.day)
    )

Type of Change

  • New feature
  • 📝 Documentation update
  • 🧪 Test update
  • 🔧 Configuration change

Related Issues / References


Screenshots

signup get all users create patient

How to Validate Locally

  1. Run migrations:
alembic upgrade head
  1. Create a new user:
  • Provide date_of_birth
  • Ensure age is NOT accepted
  1. Fetch user details:
  • Verify age is calculated correctly

Expected Behavior

Before After
Age stored in DB DOB stored in DB
Age static Age dynamic
Manual updates needed Auto-calculated

Testing Done

  • API endpoint tests passing

Test Cases Covered

Scenario Expected Result Status
Create user with DOB User created successfully
Fetch user Age calculated correctly
Missing DOB Validation error

Code Quality Checklist

Code Standards

  • Code follows project conventions
  • No unused code
  • Type hints added

API Design

  • Proper validation for DOB
  • Correct HTTP responses
  • API docs updated

Database & Migrations

  • Migration created
  • Downgrade supported
  • Schema updated correctly

Security

  • No sensitive data exposed
  • Input validation implemented

Documentation

  • API docs updated
  • .env.example unchanged

Known Limitations / Technical Debt

  • Existing users with age need migration handling (default DOB or manual update)

Additional Notes

  • This change may impact frontend forms — ensure DOB field is used instead of age input

MR Acceptance Checklist

Quality & Correctness

  • Works as expected
  • No regressions

Maintainability

  • Clean and readable code
  • Follows best practices
Edited by POORNA CHANDRA SAI TEJA KUMPATLA

Merge request reports

Loading