s9n-devops-agent 2.0.13 → 2.0.18-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,373 @@
1
+ # Database Schema Contract
2
+
3
+ **Last Updated:** 2024-12-02
4
+ **Version:** 1.0.0
5
+ **Status:** Initial Template
6
+
7
+ ---
8
+
9
+ ## Purpose
10
+
11
+ This contract defines the complete database schema for the project. **All coding agents MUST check this file before making any database changes** to ensure no breaking changes are introduced.
12
+
13
+ ---
14
+
15
+ ## Change Log
16
+
17
+ | Date | Version | Agent/Author | Changes | Impact |
18
+ |------|---------|--------------|---------|--------|
19
+ | 2024-12-02 | 1.0.0 | DevOps Agent | Initial template creation | N/A - Template only |
20
+
21
+ ---
22
+
23
+ ## Schema Overview
24
+
25
+ ### Database Technology
26
+ - **Type:** [e.g., PostgreSQL, MySQL, MongoDB, etc.]
27
+ - **Version:** [e.g., PostgreSQL 15.x]
28
+ - **Connection Pool:** [e.g., Max 20 connections]
29
+
30
+ ### Database List
31
+ | Database Name | Purpose | Owner | Created Date |
32
+ |--------------|---------|-------|--------------|
33
+ | [db_name] | [Purpose description] | [Team/Service] | [YYYY-MM-DD] |
34
+
35
+ ---
36
+
37
+ ## Tables
38
+
39
+ ### Table: [table_name]
40
+
41
+ **Created:** [YYYY-MM-DD]
42
+ **Last Modified:** [YYYY-MM-DD]
43
+ **Purpose:** [Brief description of what this table stores]
44
+
45
+ #### Schema Definition
46
+
47
+ ```sql
48
+ CREATE TABLE [table_name] (
49
+ id SERIAL PRIMARY KEY,
50
+ column_name TYPE CONSTRAINTS,
51
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
52
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
53
+ );
54
+ ```
55
+
56
+ #### Columns
57
+
58
+ | Column Name | Data Type | Nullable | Default | Constraints | Description |
59
+ |-------------|-----------|----------|---------|-------------|-------------|
60
+ | id | SERIAL | NO | AUTO | PRIMARY KEY | Unique identifier |
61
+ | column_name | TYPE | YES/NO | value | CONSTRAINT | Description |
62
+ | created_at | TIMESTAMP | NO | CURRENT_TIMESTAMP | | Record creation time |
63
+ | updated_at | TIMESTAMP | NO | CURRENT_TIMESTAMP | | Last update time |
64
+
65
+ #### Indexes
66
+
67
+ | Index Name | Columns | Type | Purpose |
68
+ |------------|---------|------|---------|
69
+ | idx_[name] | [column1, column2] | BTREE/HASH | [Performance optimization for...] |
70
+
71
+ #### Foreign Keys
72
+
73
+ | FK Name | Column | References | On Delete | On Update |
74
+ |---------|--------|------------|-----------|-----------|
75
+ | fk_[name] | [column] | [table(column)] | CASCADE/RESTRICT | CASCADE/RESTRICT |
76
+
77
+ #### Dependencies
78
+
79
+ **Used By (Modules):**
80
+ - [Module Name] - [How it uses this table]
81
+ - [Service Name] - [Read/Write operations]
82
+
83
+ **Depends On (Tables):**
84
+ - [Related Table] - [Relationship description]
85
+
86
+ ---
87
+
88
+ ## Views
89
+
90
+ ### View: [view_name]
91
+
92
+ **Created:** [YYYY-MM-DD]
93
+ **Purpose:** [What this view provides]
94
+
95
+ ```sql
96
+ CREATE VIEW [view_name] AS
97
+ SELECT ...
98
+ FROM ...
99
+ WHERE ...;
100
+ ```
101
+
102
+ **Used By:**
103
+ - [Module/Service] - [Purpose]
104
+
105
+ ---
106
+
107
+ ## Stored Procedures & Functions
108
+
109
+ ### Procedure: [procedure_name]
110
+
111
+ **Created:** [YYYY-MM-DD]
112
+ **Purpose:** [What this procedure does]
113
+
114
+ ```sql
115
+ CREATE PROCEDURE [procedure_name](params)
116
+ BEGIN
117
+ -- Logic
118
+ END;
119
+ ```
120
+
121
+ **Parameters:**
122
+ | Name | Type | Direction | Description |
123
+ |------|------|-----------|-------------|
124
+ | param1 | TYPE | IN/OUT | Description |
125
+
126
+ **Called By:**
127
+ - [Module/Service] - [When/Why]
128
+
129
+ ---
130
+
131
+ ## Migration History
132
+
133
+ ### Migration: [migration_id]
134
+
135
+ **Date:** [YYYY-MM-DD]
136
+ **Type:** [Schema Change / Data Migration]
137
+ **Status:** [Applied / Pending / Rolled Back]
138
+
139
+ **Changes:**
140
+ - [Description of changes made]
141
+
142
+ **Rollback Plan:**
143
+ ```sql
144
+ -- Rollback SQL if needed
145
+ ```
146
+
147
+ **Impact Assessment:**
148
+ - **Breaking Change:** YES/NO
149
+ - **Affected Modules:** [List]
150
+ - **Downtime Required:** YES/NO
151
+
152
+ ---
153
+
154
+ ## Migration Policy
155
+
156
+ **All database changes must strictly adhere to these Continuous Delivery principles:**
157
+
158
+ ### 1. Idempotency
159
+ * **Requirement:** All migration scripts MUST be idempotent (runnable multiple times without error or side effects).
160
+ * **Implementation:**
161
+ * Use `IF NOT EXISTS` for creating tables/columns/indexes.
162
+ * Use `IF EXISTS` for dropping (though dropping is discouraged).
163
+ * Check for existing data before inserting.
164
+
165
+ ### 2. Separation of Concerns (DDL vs DML)
166
+ * **Requirement:** Schema changes (DDL) and Data changes (DML) MUST be in separate migration files.
167
+ * **Reasoning:** Mixing them causes locking issues and makes rollbacks complex.
168
+ * **DDL:** `CREATE`, `ALTER`, `DROP` (Schema definitions).
169
+ * **DML:** `INSERT`, `UPDATE`, `DELETE` (Data manipulation).
170
+
171
+ ### 3. Non-Destructive Changes
172
+ * **Requirement:** **NEVER drop columns or tables in a live environment.**
173
+ * **Deprecation Workflow:**
174
+ 1. Mark column as deprecated in this contract.
175
+ 2. Stop writing to it in the application.
176
+ 3. Stop reading from it.
177
+ 4. (Optional) Backfill/migrate data to new structure.
178
+ 5. Drop column only after N+1 releases when it is confirmed unused.
179
+
180
+ ### 4. Online-Safe Patterns
181
+ * **Avoid:** Long-running locks on busy tables.
182
+ * **Strategy:** For large tables, use techniques like:
183
+ * Add column nullable first, then backfill, then add NOT NULL constraint.
184
+ * Create index concurrently (`CREATE INDEX CONCURRENTLY`).
185
+
186
+ ## Breaking Change Protocol
187
+
188
+ ### Before Making Schema Changes:
189
+
190
+ 1. **Check Dependencies:** Review "Used By" sections for all affected tables
191
+ 2. **Impact Analysis:** Document all modules/services that will be affected
192
+ 3. **Migration Plan:** Create backward-compatible migration if possible
193
+ 4. **Communication:** Update this contract BEFORE applying changes
194
+ 5. **Version Bump:** Increment version number following semver
195
+ 6. **Testing:** Ensure all dependent modules pass tests after migration
196
+
197
+ ### Breaking Change Checklist:
198
+
199
+ - [ ] All dependent modules identified
200
+ - [ ] Migration script created and tested
201
+ - [ ] Rollback script prepared
202
+ - [ ] This contract updated with new schema
203
+ - [ ] SQL Contract updated with affected queries
204
+ - [ ] API Contract reviewed for endpoint impacts
205
+ - [ ] Team notified of breaking changes
206
+ - [ ] Documentation updated
207
+
208
+ ---
209
+
210
+ ## Data Retention Policies
211
+
212
+ | Table | Retention Period | Archive Strategy | Compliance Notes |
213
+ |-------|------------------|------------------|------------------|
214
+ | [table_name] | [e.g., 7 years] | [Archive to S3 after 1 year] | [GDPR, SOC2, etc.] |
215
+
216
+ ---
217
+
218
+ ## Security & Access Control
219
+
220
+ ### Role-Based Access
221
+
222
+ | Role | Tables | Permissions | Purpose |
223
+ |------|--------|-------------|---------|
224
+ | app_user | [tables] | SELECT, INSERT, UPDATE | Application runtime access |
225
+ | admin | ALL | ALL | Administrative tasks |
226
+ | readonly | ALL | SELECT | Reporting and analytics |
227
+
228
+ ### Sensitive Data
229
+
230
+ | Table | Column | Encryption | PII | Notes |
231
+ |-------|--------|------------|-----|-------|
232
+ | [table] | [column] | AES-256 | YES | [Handling requirements] |
233
+
234
+ ---
235
+
236
+ ## Performance Considerations
237
+
238
+ ### Query Optimization Guidelines
239
+
240
+ - **Indexes:** Always check existing indexes before adding new ones
241
+ - **N+1 Queries:** Use JOINs or batch loading where possible
242
+ - **Large Tables:** Tables with >1M rows require special consideration
243
+ - **Pagination:** Always use LIMIT/OFFSET or cursor-based pagination
244
+
245
+ ### Monitoring
246
+
247
+ - **Slow Query Threshold:** [e.g., 500ms]
248
+ - **Connection Pool Alerts:** [e.g., >80% utilization]
249
+ - **Table Size Alerts:** [e.g., >10GB requires review]
250
+
251
+ ---
252
+
253
+ ## Backup & Recovery
254
+
255
+ - **Backup Frequency:** [e.g., Daily at 2 AM UTC]
256
+ - **Backup Retention:** [e.g., 30 days]
257
+ - **Recovery Time Objective (RTO):** [e.g., 4 hours]
258
+ - **Recovery Point Objective (RPO):** [e.g., 24 hours]
259
+
260
+ ---
261
+
262
+ ## Notes for Coding Agents
263
+
264
+ ### CRITICAL RULES:
265
+
266
+ 1. **ALWAYS read this contract before creating or modifying database tables**
267
+ 2. **NEVER drop columns** - mark as deprecated and create migration plan
268
+ 3. **ALWAYS add new columns as nullable** or provide default values
269
+ 4. **UPDATE this contract immediately** after any schema change
270
+ 5. **INCREMENT version number** for any schema modification
271
+ 6. **CHECK SQL_CONTRACT.json** for queries that may be affected
272
+ 7. **VERIFY API_CONTRACT.md** for endpoints using affected tables
273
+ 8. **RUN all tests** after schema changes
274
+
275
+ ### Workflow:
276
+
277
+ ```
278
+ 1. Read DATABASE_SCHEMA_CONTRACT.md
279
+ 2. Identify affected tables and dependencies
280
+ 3. Check SQL_CONTRACT.json for related queries
281
+ 4. Plan backward-compatible changes
282
+ 5. Update this contract with new schema
283
+ 6. Create migration script
284
+ 7. Update SQL_CONTRACT.json if queries change
285
+ 8. Update API_CONTRACT.md if endpoints affected
286
+ 9. Run tests
287
+ 10. Commit with proper documentation
288
+ ```
289
+
290
+ ---
291
+
292
+ ## Template Instructions
293
+
294
+ **For DevOps Agent / Coding Agents:**
295
+
296
+ When populating this template for the first time:
297
+
298
+ 1. **Scan all source code** for database connection strings and ORM models
299
+ 2. **Identify all tables** by searching for CREATE TABLE, model definitions, or migration files
300
+ 3. **Extract schema information** from:
301
+ - Migration files (e.g., Alembic, Flyway, Liquibase)
302
+ - ORM models (e.g., SQLAlchemy, Sequelize, Prisma)
303
+ - Raw SQL files in the repository
304
+ 4. **Document dependencies** by tracing module imports and SQL query usage
305
+ 5. **List all modules** that interact with each table
306
+ 6. **Create this contract** before any new database changes are made
307
+
308
+ **Search Patterns:**
309
+ - `CREATE TABLE`, `ALTER TABLE`, `DROP TABLE`
310
+ - `@Entity`, `class.*Model`, `Schema.define`
311
+ - `db.query`, `SELECT`, `INSERT`, `UPDATE`, `DELETE`
312
+ - Migration file patterns: `**/migrations/**`, `**/alembic/**`
313
+
314
+ ---
315
+
316
+ ## Example Entry
317
+
318
+ <!-- ======================================================================= -->
319
+ <!-- NOTE: The following is an EXAMPLE ONLY. Do not treat as real schema. -->
320
+ <!-- ======================================================================= -->
321
+
322
+ ### Table: users
323
+
324
+ **Created:** 2024-01-15
325
+ **Last Modified:** 2024-11-20
326
+ **Purpose:** Stores user account information and authentication data
327
+
328
+ #### Schema Definition
329
+
330
+ ```sql
331
+ CREATE TABLE users (
332
+ id SERIAL PRIMARY KEY,
333
+ email VARCHAR(255) UNIQUE NOT NULL,
334
+ password_hash VARCHAR(255) NOT NULL,
335
+ username VARCHAR(100) UNIQUE NOT NULL,
336
+ is_active BOOLEAN DEFAULT true,
337
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
338
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
339
+ );
340
+ ```
341
+
342
+ #### Columns
343
+
344
+ | Column Name | Data Type | Nullable | Default | Constraints | Description |
345
+ |-------------|-----------|----------|---------|-------------|-------------|
346
+ | id | SERIAL | NO | AUTO | PRIMARY KEY | Unique user identifier |
347
+ | email | VARCHAR(255) | NO | - | UNIQUE, NOT NULL | User email address |
348
+ | password_hash | VARCHAR(255) | NO | - | NOT NULL | Bcrypt hashed password |
349
+ | username | VARCHAR(100) | NO | - | UNIQUE, NOT NULL | Display name |
350
+ | is_active | BOOLEAN | NO | true | - | Account active status |
351
+ | created_at | TIMESTAMP | NO | CURRENT_TIMESTAMP | - | Account creation time |
352
+ | updated_at | TIMESTAMP | NO | CURRENT_TIMESTAMP | - | Last profile update |
353
+
354
+ #### Indexes
355
+
356
+ | Index Name | Columns | Type | Purpose |
357
+ |------------|---------|------|---------|
358
+ | idx_users_email | email | BTREE | Fast email lookup for authentication |
359
+ | idx_users_username | username | BTREE | Fast username search |
360
+
361
+ #### Dependencies
362
+
363
+ **Used By (Modules):**
364
+ - `auth-service` - User authentication and session management
365
+ - `user-profile-api` - Profile CRUD operations
366
+ - `admin-dashboard` - User management interface
367
+
368
+ **Depends On (Tables):**
369
+ - None (root table)
370
+
371
+ ---
372
+
373
+ *This contract is a living document. Update it with every schema change.*