|
| 1 | +--- |
| 2 | +title: File Organization |
| 3 | +--- |
| 4 | + |
| 5 | +Proper file organization is crucial for maintainable GitOps workflows. This guide covers repository structures and directory layouts that scale with your team. |
| 6 | + |
| 7 | +## Repository Structure |
| 8 | + |
| 9 | +### Monorepo Approach |
| 10 | + |
| 11 | +Database migrations alongside application code: |
| 12 | + |
| 13 | +``` |
| 14 | +my-app/ |
| 15 | +├── src/ # Application code |
| 16 | +├── migrations/ # Database migrations |
| 17 | +│ ├── versioned/ |
| 18 | +│ │ ├── 001__init.sql |
| 19 | +│ │ └── 002__add_users.sql |
| 20 | +│ └── schema/ |
| 21 | +│ └── public.sql # SDL if using declarative |
| 22 | +├── .github/workflows/ |
| 23 | +│ └── bytebase-gitops.yml # CI/CD integration |
| 24 | +└── README.md |
| 25 | +``` |
| 26 | + |
| 27 | +**Benefits:** |
| 28 | +- Migrations versioned with application code |
| 29 | +- Atomic commits for schema + code changes |
| 30 | +- Single source of truth |
| 31 | + |
| 32 | +**Best for:** |
| 33 | +- Small to medium teams |
| 34 | +- Tight schema-code coupling |
| 35 | +- Monolithic or modular monolith architectures |
| 36 | + |
| 37 | +### Separate Repository |
| 38 | + |
| 39 | +Dedicated database repository: |
| 40 | + |
| 41 | +``` |
| 42 | +database-schemas/ |
| 43 | +├── app-db/ |
| 44 | +│ ├── migrations/ |
| 45 | +│ └── schema/ |
| 46 | +├── analytics-db/ |
| 47 | +│ └── migrations/ |
| 48 | +└── .gitlab-ci.yml |
| 49 | +``` |
| 50 | + |
| 51 | +**Benefits:** |
| 52 | +- Separation of concerns |
| 53 | +- Independent deployment cycles |
| 54 | +- Multiple teams/databases |
| 55 | + |
| 56 | +**Best for:** |
| 57 | +- Larger organizations |
| 58 | +- Dedicated database teams |
| 59 | +- Microservices with shared databases |
| 60 | + |
| 61 | +<Tip> |
| 62 | + Choose based on your team structure. Co-located migrations work well for small teams with tight schema-code coupling. Separate repos fit larger organizations with dedicated database teams. |
| 63 | +</Tip> |
| 64 | + |
| 65 | +## Directory Layout |
| 66 | + |
| 67 | +### For Migration-Based Workflow |
| 68 | + |
| 69 | +**Option 1: Organized by Category** |
| 70 | + |
| 71 | +``` |
| 72 | +migrations/ |
| 73 | +├── baseline/ |
| 74 | +│ └── 000__initial_schema.sql # Initial baseline |
| 75 | +├── features/ |
| 76 | +│ ├── 001__users.sql |
| 77 | +│ ├── 002__products.sql |
| 78 | +│ └── 003__orders.sql |
| 79 | +└── hotfixes/ |
| 80 | + └── 004__fix_index.sql |
| 81 | +``` |
| 82 | + |
| 83 | +**Benefits:** |
| 84 | +- Clear organization by purpose |
| 85 | +- Easy to navigate |
| 86 | +- Separates routine changes from emergencies |
| 87 | + |
| 88 | +**Option 2: Flat Structure** |
| 89 | + |
| 90 | +``` |
| 91 | +migrations/ |
| 92 | +├── 001__initial_schema.sql |
| 93 | +├── 002__add_users.sql |
| 94 | +├── 003__add_products_dml.sql |
| 95 | +└── 004__add_indexes.sql |
| 96 | +``` |
| 97 | + |
| 98 | +**Benefits:** |
| 99 | +- Simple and straightforward |
| 100 | +- Chronological ordering |
| 101 | +- Easy to understand |
| 102 | + |
| 103 | +### For State-Based Workflow (SDL) |
| 104 | + |
| 105 | +**Option 1: Organized by Schema** |
| 106 | + |
| 107 | +``` |
| 108 | +schema/ |
| 109 | +├── public.sql # Main schema |
| 110 | +├── analytics.sql # Analytics schema |
| 111 | +└── internal.sql # Internal schema |
| 112 | +``` |
| 113 | + |
| 114 | +**Benefits:** |
| 115 | +- Clear schema separation |
| 116 | +- Matches database structure |
| 117 | +- Easy to find objects |
| 118 | + |
| 119 | +**Option 2: Split by Object Type** |
| 120 | + |
| 121 | +``` |
| 122 | +schema/ |
| 123 | +├── 01_tables.sql |
| 124 | +├── 02_indexes.sql |
| 125 | +├── 03_views.sql |
| 126 | +├── 04_functions.sql |
| 127 | +└── 05_sequences.sql |
| 128 | +``` |
| 129 | + |
| 130 | +**Benefits:** |
| 131 | +- Organized by DDL type |
| 132 | +- Predictable file structure |
| 133 | +- Clear dependencies (tables before indexes) |
| 134 | + |
| 135 | +### Hybrid Approach |
| 136 | + |
| 137 | +Combine both workflows for different purposes: |
| 138 | + |
| 139 | +``` |
| 140 | +database/ |
| 141 | +├── schema/ # SDL for structure (DDL) |
| 142 | +│ ├── public.sql |
| 143 | +│ └── analytics.sql |
| 144 | +├── migrations/ # Migrations for data (DML) |
| 145 | +│ ├── 001__seed_roles_dml.sql |
| 146 | +│ └── 002__migrate_users_dml.sql |
| 147 | +└── .github/workflows/ |
| 148 | + ├── schema-cicd.yml # SDL pipeline |
| 149 | + └── data-cicd.yml # Migration pipeline |
| 150 | +``` |
| 151 | + |
| 152 | +**Best for:** |
| 153 | +- Teams wanting declarative schema management |
| 154 | +- Projects requiring data migrations |
| 155 | +- Gradual migration from versioned to SDL |
| 156 | + |
| 157 | +## Documentation Structure |
| 158 | + |
| 159 | +Maintain supporting documentation alongside migrations: |
| 160 | + |
| 161 | +``` |
| 162 | +database/ |
| 163 | +├── migrations/ |
| 164 | +├── schema/ |
| 165 | +├── docs/ |
| 166 | +│ ├── CHANGELOG.md # Schema changelog |
| 167 | +│ ├── DEPENDENCIES.md # Schema-app dependencies |
| 168 | +│ └── ROLLBACK_PLAN.md # Rollback procedures |
| 169 | +└── test-data/ |
| 170 | + └── seed.sql # Test data for development |
| 171 | +``` |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## Next Steps |
| 176 | + |
| 177 | +<CardGroup cols={2}> |
| 178 | + <Card title="Migration Guidelines" icon="file-code" href="/gitops/best-practices/migration-guidelines"> |
| 179 | + Learn version numbering and file best practices |
| 180 | + </Card> |
| 181 | + |
| 182 | + <Card title="Git and CI/CD" icon="git-branch" href="/gitops/best-practices/git-and-cicd"> |
| 183 | + Set up branching strategies and pipelines |
| 184 | + </Card> |
| 185 | +</CardGroup> |
0 commit comments