# Complete Database Workflow Task

Execute the full database setup workflow from requirements to live, documented backend.

## Workflow Overview

This task orchestrates the complete database workflow by executing multiple tasks in sequence:

1. **Schema Design** → draft-database-schema.md
2. **Backend Provisioning** → provision-supabase-backend.md  
3. **Testing & Validation** → database-testing.md
4. **Documentation** → database-documentation.md

## Interactive Workflow Steps

### Phase 1: Requirements and Schema Design
**Objective:** Gather requirements and generate database schema

**Process:**
1. Execute draft-database-schema.md task
2. Collect comprehensive project requirements
3. Design entities, relationships, and security model
4. Generate production-ready SQL schema
5. Create initial documentation

**User Interaction (elicit=true):**
- Project overview and business domain
- Core entities and data models
- Security requirements and user roles
- Performance and scalability needs
- Integration requirements

**Deliverables:**
- Complete database schema (schema-[timestamp].sql)
- Schema documentation (schema-documentation-[timestamp].md)
- Entity relationship documentation
- Security model specification

### Phase 2: Supabase Backend Provisioning  
**Objective:** Deploy schema to live Supabase project

**Process:**
1. Execute provision-supabase-backend.md task
2. Authenticate with Supabase platform
3. Create new project with proper configuration
4. Deploy generated schema to live database
5. Configure environment and access controls

**User Interaction (elicit=true):**
- Supabase organization selection
- Project name and region preferences
- Database password and security settings
- Environment configuration preferences

**Deliverables:**
- Live Supabase project (https://[project-ref].supabase.co)
- Environment configuration (.env.local)
- Project access credentials and API keys
- Deployment verification report

### Phase 3: Testing and Validation
**Objective:** Verify deployment and functionality

**Process:**
1. Execute database-testing.md task
2. Test authentication and authorization flows
3. Validate Row Level Security policies
4. Verify data operations and API access
5. Performance baseline testing

**Automated Testing:**
- Schema validation and constraint testing
- RLS policy verification with test users
- API endpoint functionality testing
- Performance and response time measurement

**Deliverables:**
- Test results and validation report
- Performance baseline metrics
- Security verification documentation
- Integration testing examples

### Phase 4: Documentation and Handoff
**Objective:** Create comprehensive documentation for development team

**Process:**
1. Execute database-documentation.md task
2. Generate API documentation and examples
3. Create integration guides for common frameworks
4. Document security implementation and best practices
5. Provide troubleshooting and maintenance guides

**Documentation Generated:**
- Complete API reference documentation
- Integration examples (React, Next.js, etc.)
- Security implementation guide
- Performance optimization recommendations
- Troubleshooting and FAQ documentation

## Checkpoint Reviews

**After Schema Design:**
Present complete schema and documentation for user review before proceeding to deployment. Allow for revisions and refinements.

**After Provisioning:**
Verify project creation and schema deployment success before proceeding to testing. Confirm environment setup works correctly.

**After Testing:**
Review test results and address any issues before final documentation. Ensure all security and performance requirements are met.

## Success Criteria

**Technical Success:**
- ✅ Production-ready database schema generated
- ✅ Live Supabase project successfully deployed
- ✅ All tests passing with no security vulnerabilities
- ✅ Performance meets specified requirements
- ✅ Complete documentation and integration guides

**Business Success:**
- ✅ Schema supports all identified business requirements
- ✅ Security model properly protects user data
- ✅ Team can immediately begin application development
- ✅ Scalability path documented for growth
- ✅ Maintenance and monitoring procedures established

## Risk Mitigation

**Pre-Flight Checks:**
- Verify Supabase account access and billing setup
- Confirm schema requirements are complete and validated
- Check for potential naming conflicts or reserved words
- Review security requirements and compliance needs

**Rollback Procedures:**
- Schema rollback scripts for development issues
- Project deletion procedures if deployment fails
- Data backup and recovery procedures
- Alternative deployment strategies

## Integration Points

**Input Requirements:**
- Project requirements document (PRD) if available
- Business entity and relationship specifications
- Security and compliance requirements
- Performance and scalability targets

**Output Artifacts:**
- Live Supabase backend with deployed schema
- Complete environment configuration
- Comprehensive documentation package
- Integration examples and starter code
- Testing and validation procedures

**Next Steps:**
- Frontend development with backend integration
- API development and business logic implementation
- User interface development with data binding
- Production deployment and monitoring setup

## Time Estimates

**Total Workflow Time:** 45-90 minutes depending on complexity

- **Phase 1 (Schema Design):** 15-30 minutes
- **Phase 2 (Provisioning):** 10-20 minutes  
- **Phase 3 (Testing):** 10-20 minutes
- **Phase 4 (Documentation):** 10-20 minutes

**Complexity Factors:**
- Number of entities and relationships
- Security requirements complexity
- Performance optimization needs
- Integration requirements scope
- Documentation depth requirements

## Quality Assurance

**Automated Checks:**
- SQL syntax validation
- Schema constraint verification
- RLS policy syntax checking
- Performance query analysis
- Security vulnerability scanning

**Manual Reviews:**
- Business requirement alignment
- Security model appropriateness
- Documentation completeness
- Integration example accuracy
- Troubleshooting guide effectiveness