Fix DB migration/enum handling.

This commit is contained in:
Jens Luedicke
2025-07-02 23:11:27 +02:00
parent 8e100f101a
commit 80d22e05c4
4 changed files with 11 additions and 11 deletions

View File

@@ -13,7 +13,7 @@ The freelancer migration adds support for independent users who can register wit
## Database Changes ## Database Changes
### User Table Changes ### User Table Changes
- `account_type` VARCHAR(20) DEFAULT 'Company User' - Type of account - `account_type` VARCHAR(20) DEFAULT 'COMPANY_USER' - Type of account
- `business_name` VARCHAR(100) - Optional business name for freelancers - `business_name` VARCHAR(100) - Optional business name for freelancers
- `company_id` INTEGER - Foreign key to company table (for multi-tenancy) - `company_id` INTEGER - Foreign key to company table (for multi-tenancy)
@@ -46,7 +46,7 @@ If you prefer manual control:
```sql ```sql
-- Add columns to user table -- Add columns to user table
ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'Company User'; ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'COMPANY_USER';
ALTER TABLE user ADD COLUMN business_name VARCHAR(100); ALTER TABLE user ADD COLUMN business_name VARCHAR(100);
ALTER TABLE user ADD COLUMN company_id INTEGER; ALTER TABLE user ADD COLUMN company_id INTEGER;
@@ -66,7 +66,7 @@ CREATE TABLE company (
ALTER TABLE company ADD COLUMN is_personal BOOLEAN DEFAULT 0; ALTER TABLE company ADD COLUMN is_personal BOOLEAN DEFAULT 0;
-- Update existing users -- Update existing users
UPDATE user SET account_type = 'Company User' WHERE account_type IS NULL; UPDATE user SET account_type = 'COMPANY_USER' WHERE account_type IS NULL;
``` ```
## Post-Migration Steps ## Post-Migration Steps

6
app.py
View File

@@ -160,7 +160,7 @@ def run_migrations():
('team_id', "ALTER TABLE user ADD COLUMN team_id INTEGER"), ('team_id', "ALTER TABLE user ADD COLUMN team_id INTEGER"),
('two_factor_enabled', "ALTER TABLE user ADD COLUMN two_factor_enabled BOOLEAN DEFAULT 0"), ('two_factor_enabled', "ALTER TABLE user ADD COLUMN two_factor_enabled BOOLEAN DEFAULT 0"),
('two_factor_secret', "ALTER TABLE user ADD COLUMN two_factor_secret VARCHAR(32)"), ('two_factor_secret', "ALTER TABLE user ADD COLUMN two_factor_secret VARCHAR(32)"),
('account_type', "ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'Company User'"), ('account_type', "ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'COMPANY_USER'"),
('business_name', "ALTER TABLE user ADD COLUMN business_name VARCHAR(100)") ('business_name', "ALTER TABLE user ADD COLUMN business_name VARCHAR(100)")
] ]
@@ -196,7 +196,7 @@ def run_migrations():
team_id INTEGER, team_id INTEGER,
two_factor_enabled BOOLEAN DEFAULT 0, two_factor_enabled BOOLEAN DEFAULT 0,
two_factor_secret VARCHAR(32), two_factor_secret VARCHAR(32),
account_type VARCHAR(20) DEFAULT 'Company User', account_type VARCHAR(20) DEFAULT 'COMPANY_USER',
business_name VARCHAR(100), business_name VARCHAR(100),
company_id INTEGER company_id INTEGER
) )
@@ -210,7 +210,7 @@ def run_migrations():
SELECT id, username, email, password_hash, created_at, is_verified, SELECT id, username, email, password_hash, created_at, is_verified,
verification_token, token_expiry, is_blocked, role, team_id, verification_token, token_expiry, is_blocked, role, team_id,
two_factor_enabled, two_factor_secret, two_factor_enabled, two_factor_secret,
COALESCE(account_type, 'Company User'), COALESCE(account_type, 'COMPANY_USER'),
business_name, business_name,
company_id company_id
FROM user FROM user

View File

@@ -120,7 +120,7 @@ def migrate_database():
# Add freelancer support columns to user table # Add freelancer support columns to user table
if 'account_type' not in user_columns: if 'account_type' not in user_columns:
print("Adding account_type column to user table...") print("Adding account_type column to user table...")
cursor.execute("ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'Company User'") cursor.execute("ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'COMPANY_USER'")
if 'business_name' not in user_columns: if 'business_name' not in user_columns:
print("Adding business_name column to user table...") print("Adding business_name column to user table...")

View File

@@ -52,8 +52,8 @@ def migrate_freelancer_support():
# Add account_type column to user table if it doesn't exist # Add account_type column to user table if it doesn't exist
if 'account_type' not in user_columns: if 'account_type' not in user_columns:
print("Adding account_type column to user table...") print("Adding account_type column to user table...")
# Default to 'Company User' for existing users # Default to 'COMPANY_USER' for existing users
cursor.execute("ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'Company User'") cursor.execute("ALTER TABLE user ADD COLUMN account_type VARCHAR(20) DEFAULT 'COMPANY_USER'")
# Add business_name column to user table if it doesn't exist # Add business_name column to user table if it doesn't exist
if 'business_name' not in user_columns: if 'business_name' not in user_columns:
@@ -65,8 +65,8 @@ def migrate_freelancer_support():
print("✓ Freelancer migration completed successfully!") print("✓ Freelancer migration completed successfully!")
# Update existing users to have explicit account_type # Update existing users to have explicit account_type
print("Updating existing users to Company User account type...") print("Updating existing users to COMPANY_USER account type...")
cursor.execute("UPDATE user SET account_type = 'Company User' WHERE account_type IS NULL OR account_type = ''") cursor.execute("UPDATE user SET account_type = 'COMPANY_USER' WHERE account_type IS NULL OR account_type = ''")
conn.commit() conn.commit()
return True return True