proagents 1.6.19 → 1.6.21

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.
Files changed (143) hide show
  1. package/.claude/settings.local.json +13 -1
  2. package/.proagents/.cursorrules +25 -10
  3. package/.proagents/.windsurfrules +25 -10
  4. package/.proagents/AGENTS.md +30 -11
  5. package/.proagents/AI_INSTRUCTIONS.md +86 -30
  6. package/.proagents/BOLT.md +25 -10
  7. package/.proagents/CLAUDE.md +25 -10
  8. package/.proagents/GEMINI.md +25 -10
  9. package/.proagents/KIRO.md +25 -10
  10. package/.proagents/LOVABLE.md +25 -10
  11. package/.proagents/PROAGENTS.md +96 -343
  12. package/.proagents/REPLIT.md +25 -10
  13. package/.proagents/activity.log +1 -0
  14. package/.proagents/custom-commands.yaml +0 -1
  15. package/.proagents/docs/command-details.md +1 -2
  16. package/.proagents/getting-started/ai-training-setup.md +0 -1
  17. package/.proagents/performance/README.md +59 -0
  18. package/.proagents/performance/bundle-analysis.md +375 -0
  19. package/.proagents/performance/load-testing.md +563 -0
  20. package/.proagents/performance/runtime-metrics.md +489 -0
  21. package/.proagents/performance/web-vitals.md +425 -0
  22. package/.proagents/platforms.yaml +66 -0
  23. package/.proagents/proagents.config.yaml +0 -1
  24. package/.proagents/prompts/ai-add.md +80 -0
  25. package/.proagents/prompts/ai-list.md +41 -0
  26. package/.proagents/prompts/ai-remove.md +112 -0
  27. package/.proagents/prompts/ai-sync.md +96 -0
  28. package/.proagents/workflow-modes/entry-modes.md +1 -6
  29. package/lib/commands/ai.js +100 -48
  30. package/lib/commands/init.js +89 -22
  31. package/package.json +1 -1
  32. package/.proagents/ANTIGRAVITY.md +0 -61
  33. package/.proagents/CHATGPT.md +0 -57
  34. package/.proagents/GROQ.md +0 -57
  35. package/.proagents/api-versioning/README.md +0 -257
  36. package/.proagents/api-versioning/changelog-template.md +0 -225
  37. package/.proagents/api-versioning/deprecation-workflow.md +0 -470
  38. package/.proagents/api-versioning/versioning-strategy.md +0 -291
  39. package/.proagents/automation/README.md +0 -38
  40. package/.proagents/automation/ai-behavior-rules.md +0 -339
  41. package/.proagents/automation/ai-prompt-injection.md +0 -331
  42. package/.proagents/automation/auto-decisions.md +0 -535
  43. package/.proagents/automation/decision-defaults.yaml +0 -317
  44. package/.proagents/cache/README.md +0 -110
  45. package/.proagents/cache/analysis-metadata.json +0 -76
  46. package/.proagents/cache/conventions.json +0 -125
  47. package/.proagents/cache/dependencies.json +0 -85
  48. package/.proagents/cache/features.json +0 -115
  49. package/.proagents/cache/patterns.json +0 -105
  50. package/.proagents/cache/schemas/conventions-schema.json +0 -138
  51. package/.proagents/cache/schemas/dependencies-schema.json +0 -95
  52. package/.proagents/cache/schemas/features-schema.json +0 -104
  53. package/.proagents/cache/schemas/metadata-schema.json +0 -83
  54. package/.proagents/cache/schemas/patterns-schema.json +0 -136
  55. package/.proagents/cache/schemas/structure-schema.json +0 -72
  56. package/.proagents/cache/structure.json +0 -109
  57. package/.proagents/checklists/README.md +0 -261
  58. package/.proagents/checklists/code-quality.md +0 -137
  59. package/.proagents/checklists/code-review.md +0 -148
  60. package/.proagents/checklists/pr-checklist.md +0 -78
  61. package/.proagents/checklists/pre-deployment.md +0 -132
  62. package/.proagents/checklists/pre-implementation.md +0 -80
  63. package/.proagents/checklists/testing.md +0 -120
  64. package/.proagents/checkpoints.json +0 -13
  65. package/.proagents/cicd/README.md +0 -338
  66. package/.proagents/cicd/azure-devops.md +0 -267
  67. package/.proagents/cicd/github-actions.md +0 -375
  68. package/.proagents/cicd/gitlab-ci.md +0 -278
  69. package/.proagents/cicd/jenkins.md +0 -317
  70. package/.proagents/collaboration/README.md +0 -143
  71. package/.proagents/collaboration/roles.md +0 -248
  72. package/.proagents/collaboration/sessions.md +0 -390
  73. package/.proagents/collaboration/sync.md +0 -358
  74. package/.proagents/cost/README.md +0 -48
  75. package/.proagents/cost/cost-template.md +0 -283
  76. package/.proagents/cost/estimation-framework.md +0 -287
  77. package/.proagents/database/README.md +0 -72
  78. package/.proagents/database/examples/001-create-users.sql +0 -129
  79. package/.proagents/database/examples/002-add-preferences.sql +0 -94
  80. package/.proagents/database/examples/003-add-index.sql +0 -105
  81. package/.proagents/database/examples/004-rename-column.sql +0 -122
  82. package/.proagents/database/examples/005-add-foreign-key.sql +0 -142
  83. package/.proagents/database/examples/006-data-migration.sql +0 -196
  84. package/.proagents/database/examples/007-drop-column.sql +0 -163
  85. package/.proagents/database/examples/README.md +0 -89
  86. package/.proagents/database/migration-workflow.md +0 -478
  87. package/.proagents/database/rollback-scripts.md +0 -487
  88. package/.proagents/database/safety-checks.md +0 -447
  89. package/.proagents/git/README.md +0 -68
  90. package/.proagents/git/branch-strategy.md +0 -164
  91. package/.proagents/git/commit-conventions.md +0 -241
  92. package/.proagents/git/pr-workflow.md +0 -286
  93. package/.proagents/git/rollback-procedures.md +0 -416
  94. package/.proagents/ide-integration/README.md +0 -124
  95. package/.proagents/ide-integration/cline-config.md +0 -429
  96. package/.proagents/ide-integration/continue-config.md +0 -380
  97. package/.proagents/ide-integration/cursor-rules.md +0 -280
  98. package/.proagents/ide-integration/github-copilot.md +0 -384
  99. package/.proagents/ide-integration/windsurf-rules.md +0 -314
  100. package/.proagents/integrations/README.md +0 -97
  101. package/.proagents/integrations/pm/README.md +0 -344
  102. package/.proagents/learning/README.md +0 -136
  103. package/.proagents/learning/adaptation.md +0 -305
  104. package/.proagents/learning/data-collection.md +0 -283
  105. package/.proagents/learning/implementation-guide.md +0 -865
  106. package/.proagents/learning/reports.md +0 -306
  107. package/.proagents/mcp/README.md +0 -133
  108. package/.proagents/mcp/context-providers.md +0 -442
  109. package/.proagents/mcp/server-config.md +0 -306
  110. package/.proagents/mcp/tools-definition.md +0 -513
  111. package/.proagents/pm-integration/README.md +0 -151
  112. package/.proagents/pm-integration/asana.md +0 -346
  113. package/.proagents/pm-integration/github-issues.md +0 -308
  114. package/.proagents/pm-integration/gitlab-issues.md +0 -482
  115. package/.proagents/pm-integration/jira.md +0 -364
  116. package/.proagents/pm-integration/linear.md +0 -409
  117. package/.proagents/pm-integration/notion.md +0 -275
  118. package/.proagents/pm-integration/sync-config.md +0 -533
  119. package/.proagents/pm-integration/trello.md +0 -159
  120. package/.proagents/rules/README.md +0 -179
  121. package/.proagents/rules/custom-rules-template.yaml +0 -286
  122. package/.proagents/rules/custom-rules.md +0 -754
  123. package/.proagents/rules/validation-rules-template.yaml +0 -517
  124. package/.proagents/runbooks/README.md +0 -219
  125. package/.proagents/runbooks/dependency-vulnerability.md +0 -505
  126. package/.proagents/runbooks/incident-response.md +0 -451
  127. package/.proagents/runbooks/performance-degradation.md +0 -584
  128. package/.proagents/runbooks/production-debugging.md +0 -489
  129. package/.proagents/sprints/README.md +0 -58
  130. package/.proagents/team/README.md +0 -256
  131. package/.proagents/team/code-ownership.md +0 -306
  132. package/.proagents/team/communication-templates.md +0 -441
  133. package/.proagents/team/handoff-protocol.md +0 -380
  134. package/.proagents/team/ide-setup/README.md +0 -103
  135. package/.proagents/team/ide-setup/cursor.md +0 -276
  136. package/.proagents/team/ide-setup/jetbrains.md +0 -330
  137. package/.proagents/team/ide-setup/neovim.md +0 -640
  138. package/.proagents/team/ide-setup/vscode.md +0 -348
  139. package/.proagents/team/onboarding.md +0 -278
  140. package/.proagents/time-tracking.json +0 -19
  141. package/.proagents/troubleshooting/README.md +0 -730
  142. package/.proagents/troubleshooting/ai-issues.md +0 -601
  143. package/.proagents/troubleshooting/workflow-issues.md +0 -571
@@ -1,122 +0,0 @@
1
- -- Migration: Rename column from 'name' to 'display_name'
2
- -- Author: ProAgents
3
- -- Date: 2024-01-18
4
- -- Risk Level: Medium
5
- -- Requires Approval: Team Lead
6
- -- Estimated Duration: < 1 second (metadata only)
7
-
8
- -- Description:
9
- -- Renames the 'name' column to 'display_name' for clarity.
10
- -- Column rename is a metadata-only operation in PostgreSQL,
11
- -- but requires application code updates.
12
- --
13
- -- IMPORTANT: Coordinate this migration with application deployment.
14
- -- Consider using the expand-contract pattern for zero-downtime.
15
-
16
- -- ============================================
17
- -- EXPAND-CONTRACT PATTERN (Zero Downtime)
18
- -- ============================================
19
-
20
- -- Phase 1: EXPAND - Add new column (this migration)
21
- -- Phase 2: MIGRATE - Update application to write to both columns
22
- -- Phase 3: BACKFILL - Copy data from old to new column
23
- -- Phase 4: CONTRACT - Update application to read from new column only
24
- -- Phase 5: CLEANUP - Drop old column (separate migration)
25
-
26
- -- ============================================
27
- -- UP MIGRATION (Simple approach - requires brief downtime)
28
- -- ============================================
29
-
30
- -- Option A: Direct rename (brief lock, simple)
31
- ALTER TABLE users
32
- RENAME COLUMN name TO display_name;
33
-
34
- -- Update any views that reference this column
35
- -- (Example if you have views)
36
- -- CREATE OR REPLACE VIEW user_profiles AS
37
- -- SELECT id, display_name, email FROM users;
38
-
39
- -- ============================================
40
- -- UP MIGRATION (Zero-downtime approach)
41
- -- ============================================
42
-
43
- -- -- Step 1: Add new column
44
- -- ALTER TABLE users ADD COLUMN display_name VARCHAR(100);
45
- --
46
- -- -- Step 2: Create trigger to sync columns during transition
47
- -- CREATE OR REPLACE FUNCTION sync_name_columns()
48
- -- RETURNS TRIGGER AS $$
49
- -- BEGIN
50
- -- IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
51
- -- IF NEW.display_name IS NULL AND NEW.name IS NOT NULL THEN
52
- -- NEW.display_name = NEW.name;
53
- -- ELSIF NEW.name IS NULL AND NEW.display_name IS NOT NULL THEN
54
- -- NEW.name = NEW.display_name;
55
- -- END IF;
56
- -- END IF;
57
- -- RETURN NEW;
58
- -- END;
59
- -- $$ LANGUAGE plpgsql;
60
- --
61
- -- CREATE TRIGGER trigger_sync_name_columns
62
- -- BEFORE INSERT OR UPDATE ON users
63
- -- FOR EACH ROW
64
- -- EXECUTE FUNCTION sync_name_columns();
65
- --
66
- -- -- Step 3: Backfill existing data
67
- -- UPDATE users SET display_name = name WHERE display_name IS NULL;
68
- --
69
- -- -- Step 4: After application updated, drop trigger and old column
70
- -- DROP TRIGGER IF EXISTS trigger_sync_name_columns ON users;
71
- -- DROP FUNCTION IF EXISTS sync_name_columns();
72
- -- ALTER TABLE users DROP COLUMN name;
73
-
74
- -- ============================================
75
- -- DOWN MIGRATION (Rollback)
76
- -- ============================================
77
-
78
- -- Option A: Simple rollback
79
- ALTER TABLE users
80
- RENAME COLUMN display_name TO name;
81
-
82
- -- Option B: Zero-downtime rollback (reverse the expand)
83
- -- ALTER TABLE users DROP COLUMN display_name;
84
-
85
- -- ============================================
86
- -- VERIFICATION
87
- -- ============================================
88
-
89
- -- Check column was renamed
90
- SELECT column_name
91
- FROM information_schema.columns
92
- WHERE table_name = 'users'
93
- AND column_name IN ('name', 'display_name');
94
-
95
- -- Verify no broken views
96
- SELECT viewname
97
- FROM pg_views
98
- WHERE definition LIKE '%users.name%';
99
-
100
- -- Check for broken functions/procedures
101
- SELECT proname
102
- FROM pg_proc
103
- WHERE prosrc LIKE '%users.name%';
104
-
105
- -- ============================================
106
- -- APPLICATION CODE UPDATES REQUIRED
107
- -- ============================================
108
-
109
- -- Before migration:
110
- -- const user = await db.user.findUnique({ where: { id } });
111
- -- console.log(user.name);
112
-
113
- -- After migration:
114
- -- const user = await db.user.findUnique({ where: { id } });
115
- -- console.log(user.display_name); // Updated field name
116
-
117
- -- ORM model update (Prisma example):
118
- -- model User {
119
- -- id String @id @default(uuid())
120
- -- display_name String? @map("display_name") // Updated from "name"
121
- -- email String @unique
122
- -- }
@@ -1,142 +0,0 @@
1
- -- Migration: Add foreign key relationship (users -> teams)
2
- -- Author: ProAgents
3
- -- Date: 2024-01-19
4
- -- Risk Level: Medium
5
- -- Requires Approval: Team Lead
6
- -- Estimated Duration: Depends on table size
7
-
8
- -- Description:
9
- -- Adds a team_id foreign key to the users table, establishing a
10
- -- many-to-one relationship between users and teams.
11
- --
12
- -- IMPORTANT: Existing users will have NULL team_id unless backfilled.
13
-
14
- -- ============================================
15
- -- PRE-FLIGHT CHECKS
16
- -- ============================================
17
-
18
- -- Ensure teams table exists
19
- SELECT EXISTS (
20
- SELECT FROM information_schema.tables
21
- WHERE table_name = 'teams'
22
- );
23
-
24
- -- Check for orphan data that would violate constraint
25
- -- (Run this BEFORE adding constraint)
26
- SELECT COUNT(*) as orphan_count
27
- FROM users u
28
- WHERE u.team_id IS NOT NULL
29
- AND NOT EXISTS (SELECT 1 FROM teams t WHERE t.id = u.team_id);
30
-
31
- -- ============================================
32
- -- UP MIGRATION
33
- -- ============================================
34
-
35
- -- Step 1: Add the column (nullable first for safety)
36
- ALTER TABLE users
37
- ADD COLUMN team_id UUID;
38
-
39
- -- Step 2: Add index on foreign key column (important for JOIN performance)
40
- CREATE INDEX CONCURRENTLY idx_users_team_id
41
- ON users (team_id)
42
- WHERE team_id IS NOT NULL;
43
-
44
- -- Step 3: Add foreign key constraint
45
- -- Using NOT VALID first to avoid full table scan
46
- ALTER TABLE users
47
- ADD CONSTRAINT fk_users_team
48
- FOREIGN KEY (team_id)
49
- REFERENCES teams(id)
50
- ON DELETE SET NULL -- When team deleted, set user's team_id to NULL
51
- ON UPDATE CASCADE -- When team id changes, update users
52
- NOT VALID; -- Don't validate existing rows yet
53
-
54
- -- Step 4: Validate the constraint (can be done later, non-blocking)
55
- ALTER TABLE users
56
- VALIDATE CONSTRAINT fk_users_team;
57
-
58
- -- Add column comment
59
- COMMENT ON COLUMN users.team_id IS 'Reference to the team this user belongs to';
60
-
61
- -- ============================================
62
- -- ALTERNATIVE: Strict relationship (required team)
63
- -- ============================================
64
-
65
- -- If team_id should be required:
66
- --
67
- -- Step 1: Create default team
68
- -- INSERT INTO teams (id, name) VALUES ('00000000-0000-0000-0000-000000000000', 'Default');
69
- --
70
- -- Step 2: Add column with default
71
- -- ALTER TABLE users
72
- -- ADD COLUMN team_id UUID NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000';
73
- --
74
- -- Step 3: Add foreign key with RESTRICT
75
- -- ALTER TABLE users
76
- -- ADD CONSTRAINT fk_users_team
77
- -- FOREIGN KEY (team_id)
78
- -- REFERENCES teams(id)
79
- -- ON DELETE RESTRICT -- Prevent team deletion if users exist
80
- -- ON UPDATE CASCADE;
81
-
82
- -- ============================================
83
- -- DOWN MIGRATION (Rollback)
84
- -- ============================================
85
-
86
- -- Drop constraint first
87
- ALTER TABLE users
88
- DROP CONSTRAINT IF EXISTS fk_users_team;
89
-
90
- -- Drop index
91
- DROP INDEX CONCURRENTLY IF EXISTS idx_users_team_id;
92
-
93
- -- Drop column
94
- ALTER TABLE users
95
- DROP COLUMN IF EXISTS team_id;
96
-
97
- -- ============================================
98
- -- VERIFICATION
99
- -- ============================================
100
-
101
- -- Check constraint exists
102
- SELECT constraint_name, constraint_type
103
- FROM information_schema.table_constraints
104
- WHERE table_name = 'users'
105
- AND constraint_name = 'fk_users_team';
106
-
107
- -- Check foreign key details
108
- SELECT
109
- kcu.column_name,
110
- ccu.table_name AS foreign_table_name,
111
- ccu.column_name AS foreign_column_name,
112
- rc.delete_rule,
113
- rc.update_rule
114
- FROM information_schema.key_column_usage kcu
115
- JOIN information_schema.constraint_column_usage ccu
116
- ON kcu.constraint_name = ccu.constraint_name
117
- JOIN information_schema.referential_constraints rc
118
- ON kcu.constraint_name = rc.constraint_name
119
- WHERE kcu.table_name = 'users'
120
- AND kcu.constraint_name = 'fk_users_team';
121
-
122
- -- Test constraint enforcement
123
- -- This should fail:
124
- -- INSERT INTO users (email, password_hash, team_id)
125
- -- VALUES ('test@example.com', 'hash', '00000000-0000-0000-0000-000000000001');
126
- -- Error: insert or update on table "users" violates foreign key constraint
127
-
128
- -- ============================================
129
- -- BACKFILL EXISTING USERS
130
- -- ============================================
131
-
132
- -- Option 1: Assign all existing users to a default team
133
- -- UPDATE users
134
- -- SET team_id = (SELECT id FROM teams WHERE name = 'Default' LIMIT 1)
135
- -- WHERE team_id IS NULL;
136
-
137
- -- Option 2: Assign users based on email domain
138
- -- UPDATE users
139
- -- SET team_id = t.id
140
- -- FROM teams t
141
- -- WHERE users.email LIKE '%@' || t.domain
142
- -- AND users.team_id IS NULL;
@@ -1,196 +0,0 @@
1
- -- Migration: Migrate user roles from string to normalized table
2
- -- Author: ProAgents
3
- -- Date: 2024-01-20
4
- -- Risk Level: High
5
- -- Requires Approval: Manager + DBA
6
- -- Estimated Duration: 5-30 minutes depending on data volume
7
-
8
- -- Description:
9
- -- Transforms the denormalized 'role' enum column into a normalized
10
- -- many-to-many relationship with a roles table. This allows users
11
- -- to have multiple roles.
12
- --
13
- -- CRITICAL: This is a data migration. Test thoroughly on staging first.
14
- -- Create backup before running in production.
15
-
16
- -- ============================================
17
- -- PRE-FLIGHT CHECKS
18
- -- ============================================
19
-
20
- -- Create backup table
21
- CREATE TABLE users_backup_20240120 AS SELECT * FROM users;
22
-
23
- -- Verify backup
24
- SELECT COUNT(*) FROM users_backup_20240120;
25
-
26
- -- Check current role distribution
27
- SELECT role, COUNT(*) as count
28
- FROM users
29
- GROUP BY role
30
- ORDER BY count DESC;
31
-
32
- -- ============================================
33
- -- UP MIGRATION
34
- -- ============================================
35
-
36
- -- Step 1: Create roles table
37
- CREATE TABLE roles (
38
- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
39
- name VARCHAR(50) NOT NULL UNIQUE,
40
- description TEXT,
41
- permissions JSONB NOT NULL DEFAULT '[]'::jsonb,
42
- created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
43
- );
44
-
45
- -- Step 2: Create junction table for many-to-many
46
- CREATE TABLE user_roles (
47
- user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
48
- role_id UUID NOT NULL REFERENCES roles(id) ON DELETE CASCADE,
49
- granted_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
50
- granted_by UUID REFERENCES users(id),
51
- PRIMARY KEY (user_id, role_id)
52
- );
53
-
54
- -- Create indexes
55
- CREATE INDEX idx_user_roles_user_id ON user_roles(user_id);
56
- CREATE INDEX idx_user_roles_role_id ON user_roles(role_id);
57
-
58
- -- Step 3: Populate roles table from existing enum values
59
- INSERT INTO roles (name, description, permissions) VALUES
60
- ('admin', 'Full system access', '["read", "write", "delete", "admin"]'::jsonb),
61
- ('developer', 'Development access', '["read", "write", "deploy"]'::jsonb),
62
- ('viewer', 'Read-only access', '["read"]'::jsonb),
63
- ('guest', 'Limited guest access', '["read_public"]'::jsonb);
64
-
65
- -- Step 4: Migrate existing user roles to junction table
66
- INSERT INTO user_roles (user_id, role_id, granted_at)
67
- SELECT
68
- u.id as user_id,
69
- r.id as role_id,
70
- u.created_at as granted_at
71
- FROM users u
72
- JOIN roles r ON r.name = u.role::text
73
- WHERE u.deleted_at IS NULL;
74
-
75
- -- Step 5: Verify migration counts match
76
- DO $$
77
- DECLARE
78
- original_count INTEGER;
79
- migrated_count INTEGER;
80
- BEGIN
81
- SELECT COUNT(*) INTO original_count
82
- FROM users WHERE deleted_at IS NULL;
83
-
84
- SELECT COUNT(DISTINCT user_id) INTO migrated_count
85
- FROM user_roles;
86
-
87
- IF original_count != migrated_count THEN
88
- RAISE EXCEPTION 'Migration count mismatch: % vs %',
89
- original_count, migrated_count;
90
- END IF;
91
-
92
- RAISE NOTICE 'Migration verified: % users migrated', migrated_count;
93
- END $$;
94
-
95
- -- Step 6: Create view for backward compatibility
96
- CREATE VIEW user_primary_role AS
97
- SELECT
98
- u.id as user_id,
99
- r.name as role,
100
- r.id as role_id
101
- FROM users u
102
- JOIN user_roles ur ON ur.user_id = u.id
103
- JOIN roles r ON r.id = ur.role_id
104
- WHERE ur.granted_at = (
105
- SELECT MIN(granted_at)
106
- FROM user_roles
107
- WHERE user_id = u.id
108
- );
109
-
110
- -- Step 7: Drop old role column (after application updated)
111
- -- IMPORTANT: Only run this after verifying application works with new schema
112
- -- ALTER TABLE users DROP COLUMN role;
113
- -- DROP TYPE IF EXISTS user_role;
114
-
115
- -- ============================================
116
- -- DOWN MIGRATION (Rollback)
117
- -- ============================================
118
-
119
- -- Step 1: Restore role column if dropped
120
- -- ALTER TABLE users ADD COLUMN role user_role;
121
-
122
- -- Step 2: Restore data from junction table
123
- UPDATE users u
124
- SET role = (
125
- SELECT r.name::user_role
126
- FROM user_roles ur
127
- JOIN roles r ON r.id = ur.role_id
128
- WHERE ur.user_id = u.id
129
- ORDER BY ur.granted_at
130
- LIMIT 1
131
- );
132
-
133
- -- Step 3: Drop new tables
134
- DROP VIEW IF EXISTS user_primary_role;
135
- DROP TABLE IF EXISTS user_roles;
136
- DROP TABLE IF EXISTS roles;
137
-
138
- -- Step 4: Restore from backup if needed
139
- -- DROP TABLE users;
140
- -- ALTER TABLE users_backup_20240120 RENAME TO users;
141
-
142
- -- ============================================
143
- -- VERIFICATION
144
- -- ============================================
145
-
146
- -- Compare counts
147
- SELECT
148
- (SELECT COUNT(*) FROM users WHERE deleted_at IS NULL) as total_users,
149
- (SELECT COUNT(DISTINCT user_id) FROM user_roles) as users_with_roles,
150
- (SELECT COUNT(*) FROM user_roles) as total_assignments;
151
-
152
- -- Check role distribution matches original
153
- SELECT r.name, COUNT(ur.user_id) as user_count
154
- FROM roles r
155
- LEFT JOIN user_roles ur ON ur.role_id = r.id
156
- GROUP BY r.name
157
- ORDER BY user_count DESC;
158
-
159
- -- Verify no orphaned records
160
- SELECT COUNT(*) as orphaned
161
- FROM user_roles ur
162
- WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.id = ur.user_id);
163
-
164
- -- Test backward compatibility view
165
- SELECT * FROM user_primary_role LIMIT 5;
166
-
167
- -- ============================================
168
- -- CLEANUP (Run after verification period)
169
- -- ============================================
170
-
171
- -- Drop backup table
172
- -- DROP TABLE IF EXISTS users_backup_20240120;
173
-
174
- -- Drop old column and enum
175
- -- ALTER TABLE users DROP COLUMN IF EXISTS role;
176
- -- DROP TYPE IF EXISTS user_role;
177
-
178
- -- ============================================
179
- -- APPLICATION CODE UPDATES
180
- -- ============================================
181
-
182
- -- Old query:
183
- -- SELECT * FROM users WHERE role = 'admin';
184
-
185
- -- New query:
186
- -- SELECT u.*
187
- -- FROM users u
188
- -- JOIN user_roles ur ON ur.user_id = u.id
189
- -- JOIN roles r ON r.id = ur.role_id
190
- -- WHERE r.name = 'admin';
191
-
192
- -- Or using the view for backward compatibility:
193
- -- SELECT u.*
194
- -- FROM users u
195
- -- JOIN user_primary_role upr ON upr.user_id = u.id
196
- -- WHERE upr.role = 'admin';
@@ -1,163 +0,0 @@
1
- -- Migration: Drop deprecated 'legacy_id' column
2
- -- Author: ProAgents
3
- -- Date: 2024-01-21
4
- -- Risk Level: High
5
- -- Requires Approval: Manager + DBA
6
- -- Estimated Duration: < 1 second (metadata only, but irreversible)
7
-
8
- -- Description:
9
- -- Removes the deprecated 'legacy_id' column that was used during
10
- -- migration from the old system. All references have been updated
11
- -- to use the new UUID primary key.
12
- --
13
- -- CRITICAL: This is a DESTRUCTIVE operation. Data cannot be recovered
14
- -- without a backup. Ensure all dependent code and queries have been updated.
15
-
16
- -- ============================================
17
- -- PRE-FLIGHT CHECKS (MANDATORY)
18
- -- ============================================
19
-
20
- -- 1. Check if column is still referenced in application code
21
- -- Run: grep -r "legacy_id" ./src/
22
- -- Expected: No results
23
-
24
- -- 2. Check if column is used in any views
25
- SELECT viewname, definition
26
- FROM pg_views
27
- WHERE definition LIKE '%legacy_id%';
28
-
29
- -- 3. Check if column is used in any functions/procedures
30
- SELECT proname, prosrc
31
- FROM pg_proc
32
- WHERE prosrc LIKE '%legacy_id%';
33
-
34
- -- 4. Check if column is used in any triggers
35
- SELECT tgname
36
- FROM pg_trigger t
37
- JOIN pg_proc p ON p.oid = t.tgfoid
38
- WHERE p.prosrc LIKE '%legacy_id%';
39
-
40
- -- 5. Check if any foreign keys reference this column
41
- SELECT
42
- tc.constraint_name,
43
- tc.table_name,
44
- kcu.column_name,
45
- ccu.table_name AS foreign_table_name,
46
- ccu.column_name AS foreign_column_name
47
- FROM information_schema.table_constraints AS tc
48
- JOIN information_schema.key_column_usage AS kcu
49
- ON tc.constraint_name = kcu.constraint_name
50
- JOIN information_schema.constraint_column_usage AS ccu
51
- ON ccu.constraint_name = tc.constraint_name
52
- WHERE ccu.column_name = 'legacy_id'
53
- OR kcu.column_name = 'legacy_id';
54
-
55
- -- 6. Verify backup exists or create one
56
- SELECT COUNT(*) FROM users; -- Note the count
57
- -- CREATE TABLE users_backup_legacy_id AS SELECT id, legacy_id FROM users;
58
-
59
- -- ============================================
60
- -- UP MIGRATION
61
- -- ============================================
62
-
63
- -- OPTION 1: Immediate drop (brief exclusive lock)
64
- ALTER TABLE users
65
- DROP COLUMN legacy_id;
66
-
67
- -- OPTION 2: Two-phase drop (for zero-downtime on very large tables)
68
- -- Phase 1: Mark column as dropped (immediate, no lock)
69
- -- UPDATE pg_attribute
70
- -- SET attisdropped = true
71
- -- WHERE attrelid = 'users'::regclass
72
- -- AND attname = 'legacy_id';
73
- --
74
- -- Phase 2: Actually reclaim space (during maintenance window)
75
- -- VACUUM FULL users;
76
-
77
- -- Drop related indexes if any
78
- DROP INDEX IF EXISTS idx_users_legacy_id;
79
-
80
- -- Drop related constraints if any
81
- ALTER TABLE users
82
- DROP CONSTRAINT IF EXISTS chk_users_legacy_id;
83
-
84
- -- ============================================
85
- -- DOWN MIGRATION (Rollback)
86
- -- ============================================
87
-
88
- -- WARNING: This only recreates the column structure.
89
- -- DATA CANNOT BE RECOVERED without a backup!
90
-
91
- -- Recreate column
92
- ALTER TABLE users
93
- ADD COLUMN legacy_id INTEGER;
94
-
95
- -- Recreate index if it existed
96
- CREATE INDEX idx_users_legacy_id ON users(legacy_id);
97
-
98
- -- Restore data from backup (if backup exists)
99
- UPDATE users u
100
- SET legacy_id = b.legacy_id
101
- FROM users_backup_legacy_id b
102
- WHERE u.id = b.id;
103
-
104
- -- If no backup exists, column will be NULL for all rows
105
- -- Consider whether this is acceptable before dropping
106
-
107
- -- ============================================
108
- -- VERIFICATION
109
- -- ============================================
110
-
111
- -- Confirm column no longer exists
112
- SELECT column_name
113
- FROM information_schema.columns
114
- WHERE table_name = 'users'
115
- AND column_name = 'legacy_id';
116
- -- Expected: 0 rows
117
-
118
- -- Check table structure
119
- \d users
120
-
121
- -- Verify no errors in dependent objects
122
- SELECT *
123
- FROM pg_stat_user_functions
124
- WHERE funcname LIKE '%legacy%';
125
-
126
- -- Test that application queries still work
127
- EXPLAIN ANALYZE
128
- SELECT id, email, created_at
129
- FROM users
130
- WHERE email = 'test@example.com';
131
-
132
- -- ============================================
133
- -- CLEANUP
134
- -- ============================================
135
-
136
- -- After verification period, drop backup table
137
- -- DROP TABLE IF EXISTS users_backup_legacy_id;
138
-
139
- -- Update documentation
140
- -- - Remove legacy_id from API docs
141
- -- - Remove legacy_id from database schema docs
142
- -- - Update migration guides
143
-
144
- -- ============================================
145
- -- COMMON ISSUES AND SOLUTIONS
146
- -- ============================================
147
-
148
- -- Issue: "column does not exist" errors after drop
149
- -- Solution: Application code still references the column
150
- -- Search and update all references
151
-
152
- -- Issue: View/function errors after drop
153
- -- Solution: Recreate views/functions without the column
154
- -- DROP VIEW affected_view;
155
- -- CREATE VIEW affected_view AS ...;
156
-
157
- -- Issue: Need to restore data
158
- -- Solution: Restore from backup table or full database backup
159
- -- If no backup, data is permanently lost
160
-
161
- -- Issue: Replication lag causes errors
162
- -- Solution: Ensure all replicas have applied the change
163
- -- before updating application code
@@ -1,89 +0,0 @@
1
- # Database Migration Examples
2
-
3
- Concrete migration examples for common database operations.
4
-
5
- ---
6
-
7
- ## Example Migrations
8
-
9
- | Migration | Description | Risk Level |
10
- |-----------|-------------|------------|
11
- | [001-create-users](./001-create-users.sql) | Create users table | Low |
12
- | [002-add-preferences](./002-add-preferences.sql) | Add column with default | Low |
13
- | [003-add-index](./003-add-index.sql) | Add performance index | Medium |
14
- | [004-rename-column](./004-rename-column.sql) | Rename existing column | Medium |
15
- | [005-add-foreign-key](./005-add-foreign-key.sql) | Add relationship constraint | Medium |
16
- | [006-data-migration](./006-data-migration.sql) | Transform existing data | High |
17
- | [007-drop-column](./007-drop-column.sql) | Remove column safely | High |
18
-
19
- ---
20
-
21
- ## Using These Examples
22
-
23
- ### 1. Copy and Customize
24
-
25
- ```bash
26
- # Copy template to your migrations folder
27
- cp .proagents/database/examples/001-create-users.sql \
28
- migrations/20240115_001_create_users.sql
29
- ```
30
-
31
- ### 2. Naming Convention
32
-
33
- ```
34
- YYYYMMDD_NNN_description.sql
35
-
36
- Example:
37
- 20240115_001_create_users.sql
38
- 20240115_002_add_user_email_index.sql
39
- ```
40
-
41
- ### 3. Required Structure
42
-
43
- Every migration file must include:
44
- - Header comment with metadata
45
- - UP migration (forward)
46
- - DOWN migration (rollback)
47
- - Verification query
48
-
49
- ---
50
-
51
- ## Migration Template
52
-
53
- ```sql
54
- -- Migration: [description]
55
- -- Author: [name]
56
- -- Date: [YYYY-MM-DD]
57
- -- Risk Level: [Low|Medium|High|Critical]
58
- -- Requires Approval: [Yes|No]
59
- -- Estimated Duration: [time]
60
-
61
- -- ============================================
62
- -- UP MIGRATION
63
- -- ============================================
64
-
65
- -- [Your forward migration SQL here]
66
-
67
- -- ============================================
68
- -- DOWN MIGRATION (Rollback)
69
- -- ============================================
70
-
71
- -- [Your rollback SQL here]
72
-
73
- -- ============================================
74
- -- VERIFICATION
75
- -- ============================================
76
-
77
- -- [Query to verify migration succeeded]
78
- ```
79
-
80
- ---
81
-
82
- ## Risk Levels
83
-
84
- | Level | Examples | Approval Required |
85
- |-------|----------|------------------|
86
- | **Low** | Add nullable column, create table | No |
87
- | **Medium** | Add index, add constraint | Team lead |
88
- | **High** | Data migration, drop column | Manager + DBA |
89
- | **Critical** | Drop table, schema restructure | CTO + DBA |