Title: seed.py: Redundant seeding logic and potential issues in test environments
Body:
The seed.py file contains two separate seeding functions: seed_data and seed_database. This is confusing and redundant. Moreover, seed_data includes db.drop_all() before creating tables, which is unnecessary and potentially harmful, especially if the migrations are not in sync with the models. Further, it calls another separate seeding function to seed the database in a testing environment. It does not make any sense to have one seed file calling another separate seed function, particularly for something such as testing.
Moreover, the try/except block with the subsequent time.sleep(2) is indicative of underlying connection issues that should be addressed at the connection level, rather than with arbitrary delays.
Consolidate the seeding logic into a single, well-structured function. Remove db.drop_all() from the seed_data function and rely on the migrations to manage the database schema. Remove the nested retry attempt to re-seed the database, as this is not a production worthy solution and exposes the database to corruption.
def seed_database(testing=False): # add testing parameter to control environment specific logic
"""Seed the database with initial data"""
try:
# Create test user
unique_id = str(uuid.uuid4())[:8]
test_user = User(
first_name="Test",
last_name="User",
email=f"test_{unique_id}@example.com"
)
test_user.set_password("TestPass123!")
db.session.add(test_user)
db.session.commit()
# Create some test properties
properties = []
for i in range(5):
# Generate realistic property values following the 70% Rule
arv = random.randint(200000, 500000) # After Repair Value
max_purchase = arv * 0.7 # 70% of ARV
purchase_cost = max_purchase - random.randint(10000, 30000) # Slightly below max for profit
total_rehab_cost = (max_purchase - purchase_cost) * random.uniform(0.8, 1.2) # Variation in rehab costs
# Create property with required init parameters
property = Property(
address=f"123 Test St #{i+1}",
owner_id=test_user.id,
purchase_price=purchase_cost,
current_phase='ACQUISITION'
)
# Set additional attributes after initialization
property.propertyName = f"Test Property {i+1}"
property.city = "Test City"
property.state = "TS"
property.zipCode = "12345"
property.purchaseCost = purchase_cost
property.totalRehabCost = total_rehab_cost
property.arvSalePrice = arv
properties.append(property)
db.session.add(property)
db.session.commit()
# Create phases for each property
for property in properties:
phases = create_property_phases(property.id, property.purchase_price, property.totalRehabCost)
for phase in phases:
db.session.add(phase)
db.session.commit()
print("Seed data inserted successfully.")
except Exception as e:
print(f"Error seeding database: {e}")
These issues represent areas where the code could be improved for security, reliability, and maintainability.
Title: seed.py: Redundant seeding logic and potential issues in test environments
Body:
The
seed.pyfile contains two separate seeding functions:seed_dataandseed_database. This is confusing and redundant. Moreover,seed_dataincludesdb.drop_all()before creating tables, which is unnecessary and potentially harmful, especially if the migrations are not in sync with the models. Further, it calls another separate seeding function to seed the database in a testing environment. It does not make any sense to have one seed file calling another separate seed function, particularly for something such as testing.Moreover, the try/except block with the subsequent time.sleep(2) is indicative of underlying connection issues that should be addressed at the connection level, rather than with arbitrary delays.
Consolidate the seeding logic into a single, well-structured function. Remove
db.drop_all()from theseed_datafunction and rely on the migrations to manage the database schema. Remove the nested retry attempt to re-seed the database, as this is not a production worthy solution and exposes the database to corruption.These issues represent areas where the code could be improved for security, reliability, and maintainability.