# Database Column Service Test Controller

This controller provides a web interface for manually testing the Database Column Service functionality.

## Access

The test interface is available at: `/customization/test/database-columns`

## Features

### 1. Database Connection Check
- Tests the tenant database connection
- Shows database name, driver, and connection status

### 2. Quick Test
- Creates a sample form field and column automatically
- Useful for quick validation of the service

### 3. Create Column
- Select an existing form field and record type
- Creates a new database column based on the form field configuration
- Shows the result including column name, type, and table

### 4. List Columns
- Select a record type to see all columns in its associated table
- Shows column names, types, and nullable status

### 5. Update Column
- Update an existing column with new form field configuration
- Requires the old column name for identification

### 6. Delete Column
- Remove a column from a table
- Requires the column name to delete

## API Endpoints

### GET `/customization/test/database-columns`
- Main test interface

### GET `/customization/test/database-columns/connection`
- Check database connection status

### GET `/customization/test/database-columns/sample`
- Create a sample column for testing

### POST `/customization/test/database-columns/create`
- Create a new column
- Parameters: `form_field_id`, `record_type_id`

### POST `/customization/test/database-columns/update`
- Update an existing column
- Parameters: `form_field_id`, `record_type_id`, `old_column_name`

### POST `/customization/test/database-columns/delete`
- Delete a column
- Parameters: `record_type_id`, `column_name`

### POST `/customization/test/database-columns/list`
- List all columns in a table
- Parameters: `record_type_id`

## Command Line Testing

You can also test the service using the Artisan command:

```bash
# Test with sample data
php artisan test:database-columns --action=sample

# Create a column
php artisan test:database-columns --action=create --form-field-id=1 --record-type-id=1

# List columns in a table
php artisan test:database-columns --action=list --record-type-id=1

# Update a column
php artisan test:database-columns --action=update --form-field-id=2 --record-type-id=1 --old-column-name=old_column

# Delete a column
php artisan test:database-columns --action=delete --record-type-id=1 --column-name=column_to_delete
```

### Command Options

- `--action`: Action to perform (create, update, delete, list, sample)
- `--form-field-id`: Form field ID for create/update actions
- `--record-type-id`: Record type ID
- `--column-name`: Column name for delete action
- `--old-column-name`: Old column name for update action

## Usage Examples

### Testing Column Creation
1. Navigate to the test interface
2. Select a form field from the dropdown
3. Select a record type
4. Click "Create Column"
5. Check the result for success/error messages

### Testing Column Updates
1. First create a column using the create function
2. Note the column name from the result
3. Select a different form field
4. Enter the old column name
5. Click "Update Column"

### Testing Column Deletion
1. List columns to see available columns
2. Enter the column name to delete
3. Select the record type
4. Click "Delete Column"

## Prerequisites

- Must be authenticated
- Must have access to tenant subdomain
- Database connection must be configured
- Form fields and record types must exist in the database

## Troubleshooting

### Common Issues

1. **"Table does not exist"**
   - Ensure the record type has a valid `recordType` value
   - Check that the table name generation is working correctly

2. **"Column already exists"**
   - The column name is already taken
   - Try using a different form field or modify the scriptid

3. **"Invalid column name"**
   - Column name contains reserved SQL keywords
   - Column name is too long (>64 characters)
   - Column name contains invalid characters

4. **Database connection errors**
   - Check tenant database configuration
   - Verify the `tenant_connection` is properly configured

### Debug Information

The controller provides detailed error messages and success responses that include:
- Column details (name, type, table, constraints)
- Database connection information
- Form field and record type details
- Operation status and messages

## Security Notes

- This controller is for testing purposes only
- Should be disabled in production environments
- All operations are logged for audit purposes
- Uses proper validation and error handling
