-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile.db
More file actions
517 lines (469 loc) · 21.9 KB
/
Makefile.db
File metadata and controls
517 lines (469 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# Database Management Makefile
# Contains all database-related operations for Sorting Office
.PHONY: migrate migrate-revert migrate-reset migrate-all seed create-seed-migration test-db-setup test-db-clean test-db-reset prod-db-setup prod-db-reset list-domains count-domains list-aliases count-aliases list-users count-users db-shell
# Database configuration
MYSQL ?= mysql
MYSQL_USER ?= root
MYSQL_PASSWORD ?= rootpassword
MYSQL_DATABASE ?= sortingoffice
MYSQL_HOST ?= 127.0.0.1
MYSQL_PORT ?= 3306
MYSQL_CMD = $(MYSQL) -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) -N -e
# Optional DATABASE_URL parameter for targeting other databases
DATABASE_URL_ARG ?=
# Shell access to database
db-shell:
./docker.sh db-shell
# General database operations
migrate:
@if [ "$(ENVIRONMENT)" = "production" ] || [ "$(ENVIRONMENT)" = "prod" ]; then \
echo "❌ MIGRATIONS BLOCKED: Cannot run migrations on production database!"; \
echo " Set ENVIRONMENT=development to override this protection."; \
echo " Or use make migrate-force to bypass this check (not recommended)."; \
exit 1; \
fi
@if [ -n "$(DATABASE_URL_ARG)" ]; then \
echo "Running migrations on $(DATABASE_URL_ARG)..."; \
diesel migration run --database-url=$(DATABASE_URL_ARG); \
else \
echo "Running migrations on default database..."; \
diesel migration run; \
fi
# Force migration (bypasses production protection - use with caution!)
migrate-force:
@echo "⚠️ FORCE MIGRATION: Bypassing production protection!"
@echo " This will run migrations even on production databases."
@echo " Are you sure you want to continue? (y/N)"
@read -p "" confirm; \
if [ "$$confirm" != "y" ] && [ "$$confirm" != "Y" ]; then \
echo "Operation cancelled."; \
exit 0; \
fi
@if [ -n "$(DATABASE_URL_ARG)" ]; then \
echo "Running migrations on $(DATABASE_URL_ARG)..."; \
diesel migration run --database-url=$(DATABASE_URL_ARG); \
else \
echo "Running migrations on default database..."; \
diesel migration run; \
fi
migrate-revert:
@if [ "$(ENVIRONMENT)" = "production" ] || [ "$(ENVIRONMENT)" = "prod" ]; then \
echo "❌ MIGRATION REVERT BLOCKED: Cannot revert migrations on production database!"; \
echo " Set ENVIRONMENT=development to override this protection."; \
echo " Or use make migrate-revert-force to bypass this check (not recommended)."; \
exit 1; \
fi
@if [ -n "$(DATABASE_URL_ARG)" ]; then \
echo "Reverting last migration on $(DATABASE_URL_ARG)..."; \
diesel migration revert --database-url=$(DATABASE_URL_ARG); \
else \
echo "Reverting last migration on default database..."; \
diesel migration revert; \
fi
# Force migration revert (bypasses production protection - use with caution!)
migrate-revert-force:
@echo "⚠️ FORCE MIGRATION REVERT: Bypassing production protection!"
@echo " This will revert migrations even on production databases."
@echo " Are you sure you want to continue? (y/N)"
@read -p "" confirm; \
if [ "$$confirm" != "y" ] && [ "$$confirm" != "Y" ]; then \
echo "Operation cancelled."; \
exit 0; \
fi
@if [ -n "$(DATABASE_URL_ARG)" ]; then \
echo "Reverting last migration on $(DATABASE_URL_ARG)..."; \
diesel migration revert --database-url=$(DATABASE_URL_ARG); \
else \
echo "Reverting last migration on default database..."; \
diesel migration revert; \
fi
migrate-reset:
@if [ "$(ENVIRONMENT)" = "production" ] || [ "$(ENVIRONMENT)" = "prod" ]; then \
echo "❌ MIGRATION RESET BLOCKED: Cannot reset migrations on production database!"; \
echo " Set ENVIRONMENT=development to override this protection."; \
echo " Or use make migrate-reset-force to bypass this check (not recommended)."; \
exit 1; \
fi
@if [ -n "$(DATABASE_URL_ARG)" ]; then \
echo "Resetting migrations on $(DATABASE_URL_ARG)..."; \
diesel migration revert --database-url=$(DATABASE_URL_ARG); \
diesel migration run --database-url=$(DATABASE_URL_ARG); \
else \
echo "Resetting migrations on default database..."; \
diesel migration revert; \
diesel migration run; \
fi
# Force migration reset (bypasses production protection - use with caution!)
migrate-reset-force:
@echo "⚠️ FORCE MIGRATION RESET: Bypassing production protection!"
@echo " This will reset migrations even on production databases."
@echo " Are you sure you want to continue? (y/N)"
@read -p "" confirm; \
if [ "$$confirm" != "y" ] && [ "$$confirm" != "Y" ]; then \
echo "Operation cancelled."; \
exit 0; \
fi
@if [ -n "$(DATABASE_URL_ARG)" ]; then \
echo "Resetting migrations on $(DATABASE_URL_ARG)..."; \
diesel migration revert --database-url=$(DATABASE_URL_ARG); \
diesel migration run --database-url=$(DATABASE_URL_ARG); \
else \
echo "Resetting migrations on default database..."; \
diesel migration revert; \
diesel migration run; \
fi
# Run migrations on all configured databases
migrate-all:
@if [ "$(ENVIRONMENT)" = "production" ] || [ "$(ENVIRONMENT)" = "prod" ]; then \
echo "❌ MIGRATIONS BLOCKED: Cannot run migrations on production databases!"; \
echo " Set ENVIRONMENT=development to override this protection."; \
echo " Or use make migrate-all-force to bypass this check (not recommended)."; \
exit 1; \
fi
@echo "🔄 Running migrations on all configured databases..."
@cargo run --bin migrate-all-databases 2>/dev/null || \
(echo "⚠️ Migration binary not found, running via application startup..."; \
echo " This will start the application briefly to run migrations."; \
timeout 30s cargo run --quiet 2>/dev/null || echo "✅ Migrations completed or application started successfully")
# Force migrate all (bypasses production protection - use with caution!)
migrate-all-force:
@echo "⚠️ FORCE MIGRATE ALL: Bypassing production protection!"
@echo " This will run migrations on all databases even in production."
@echo " Are you sure you want to continue? (y/N)"
@read -p "" confirm; \
if [ "$$confirm" != "y" ] && [ "$$confirm" != "Y" ]; then \
echo "Operation cancelled."; \
exit 0; \
fi
@echo "🔄 Running migrations on all configured databases..."
@cargo run --bin migrate-all-databases 2>/dev/null || \
(echo "⚠️ Migration binary not found, running via application startup..."; \
echo " This will start the application briefly to run migrations."; \
timeout 30s cargo run --quiet 2>/dev/null || echo "✅ Migrations completed or application started successfully")
# Seed data management
seed:
@if [ "$(ENVIRONMENT)" = "production" ] || [ "$(ENVIRONMENT)" = "prod" ]; then \
echo "❌ SEEDING BLOCKED: Cannot seed production database!"; \
echo " Set ENVIRONMENT=development to override this protection."; \
echo " Or use make seed-force to bypass this check (not recommended)."; \
exit 1; \
fi
@if [ -n "$(MYSQL_DATABASE)" ]; then \
echo "🌱 Seeding database $(MYSQL_DATABASE) with initial data..."; \
if [ -f "seed_data/all.sql" ]; then \
echo "Running seed data from seed_data/all.sql..."; \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/all.sql; \
echo "✅ Seed data loaded successfully!"; \
else \
echo "❌ No seed data found. Creating default seed data..."; \
make create-seed-data; \
fi \
else \
echo "🌱 Seeding default database with initial data..."; \
if [ -f "seed_data/all.sql" ]; then \
echo "Running seed data from seed_data/all.sql..."; \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/all.sql; \
echo "✅ Seed data loaded successfully!"; \
else \
echo "❌ No seed data found. Creating default seed data..."; \
make create-seed-data; \
fi \
fi
# Force seed (bypasses production protection - use with caution!)
seed-force:
@echo "⚠️ WARNING: Force seeding database (bypassing production protection)..."
@if [ -n "$(MYSQL_DATABASE)" ]; then \
echo "🌱 Force seeding database $(MYSQL_DATABASE) with initial data..."; \
if [ -f "seed_data/all.sql" ]; then \
echo "Running seed data from seed_data/all.sql..."; \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/all.sql; \
echo "✅ Seed data loaded successfully!"; \
else \
echo "❌ No seed data found. Creating default seed data..."; \
make create-seed-data; \
fi \
else \
echo "🌱 Force seeding default database with initial data..."; \
if [ -f "seed_data/all.sql" ]; then \
echo "Running seed data from seed_data/all.sql..."; \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/all.sql; \
echo "✅ Seed data loaded successfully!"; \
else \
echo "❌ No seed data found. Creating default seed data..."; \
make create-seed-data; \
fi \
fi
seed-domains:
@if [ -n "$(MYSQL_DATABASE)" ]; then \
echo "🌱 Seeding domains table in $(MYSQL_DATABASE)..."; \
if [ -f "seed_data/domains.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/domains.sql; \
echo "✅ Domains seeded successfully!"; \
else \
echo "❌ seed_data/domains.sql not found!"; \
fi \
else \
echo "🌱 Seeding domains table in default database..."; \
if [ -f "seed_data/domains.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/domains.sql; \
echo "✅ Domains seeded successfully!"; \
else \
echo "❌ seed_data/domains.sql not found!"; \
fi \
fi
seed-users:
@if [ -n "$(MYSQL_DATABASE)" ]; then \
echo "🌱 Seeding users table in $(MYSQL_DATABASE)..."; \
if [ -f "seed_data/users.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/users.sql; \
echo "✅ Users seeded successfully!"; \
else \
echo "❌ seed_data/users.sql not found!"; \
fi \
else \
echo "🌱 Seeding users table in default database..."; \
if [ -f "seed_data/users.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/users.sql; \
echo "✅ Users seeded successfully!"; \
else \
echo "❌ seed_data/users.sql not found!"; \
fi \
fi
seed-aliases:
@echo "🌱 Seeding aliases table..."
@if [ -f "seed_data/aliases.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/aliases.sql; \
echo "✅ Aliases seeded successfully!"; \
else \
echo "❌ seed_data/aliases.sql not found!"; \
fi
seed-backups:
@echo "🌱 Seeding backups table..."
@if [ -f "seed_data/backups.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/backups.sql; \
echo "✅ Backups seeded successfully!"; \
else \
echo "❌ seed_data/backups.sql not found!"; \
fi
seed-relocated:
@echo "🌱 Seeding relocated table..."
@if [ -f "seed_data/relocated.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/relocated.sql; \
echo "✅ Relocated seeded successfully!"; \
else \
echo "❌ seed_data/relocated.sql not found!"; \
fi
seed-relays:
@echo "🌱 Seeding relays table..."
@if [ -f "seed_data/relays.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/relays.sql; \
echo "✅ Relays seeded successfully!"; \
else \
echo "❌ seed_data/relays.sql not found!"; \
fi
seed-clients:
@echo "🌱 Seeding clients table..."
@if [ -f "seed_data/clients.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/clients.sql; \
echo "✅ Clients seeded successfully!"; \
else \
echo "❌ seed_data/clients.sql not found!"; \
fi
seed-analytics-test:
@echo "📊 Seeding analytics test data..."
@if [ -f "seed_data/analytics_test_domains.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/analytics_test_domains.sql; \
echo "✅ Analytics test domains seeded successfully!"; \
else \
echo "❌ seed_data/analytics_test_domains.sql not found!"; \
fi
@if [ -f "seed_data/analytics_test_aliases.sql" ]; then \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) < seed_data/analytics_test_aliases.sql; \
echo "✅ Analytics test aliases seeded successfully!"; \
else \
echo "❌ seed_data/analytics_test_aliases.sql not found!"; \
fi
@echo "📊 Analytics test data seeded! This will show analytics aliases in the domain wizard and domain show pages."
create-seed-data:
@echo "📝 Creating default seed data files..."
@mkdir -p seed_data
@echo "-- Seed data for domains table" > seed_data/domains.sql
@echo "-- This file contains initial domain data for development/testing" >> seed_data/domains.sql
@echo "" >> seed_data/domains.sql
@echo "-- Insert seed data for domains" >> seed_data/domains.sql
@echo "INSERT INTO domains (domain, transport, enabled) VALUES" >> seed_data/domains.sql
@echo "('example.com', 'virtual', 1);" >> seed_data/domains.sql
@echo "Seed data files created in seed_data/ directory"
@echo "Edit the files and run 'make seed' to load the data"
# Test database management
test-db-setup:
@echo "🧪 Setting up test database..."
@echo "Creating test database if it doesn't exist..."
@mysql -uroot -prootpassword -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "CREATE DATABASE IF NOT EXISTS sortingoffice_test;"
@echo "Granting permissions to sortingoffice user..."
@mysql -uroot -prootpassword -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "GRANT ALL PRIVILEGES ON sortingoffice_test.* TO 'sortingoffice'@'%';"
@echo "Running migrations on test database..."
@DATABASE_URL=mysql://sortingoffice:sortingoffice@$(MYSQL_HOST):$(MYSQL_PORT)/sortingoffice_test diesel migration run
test-db-clean:
@echo "🧹 Cleaning test database..."
@mysql -uroot -prootpassword -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "DROP DATABASE IF EXISTS sortingoffice_test;"
test-db-reset: test-db-clean test-db-setup
@echo "🔄 Test database reset complete"
# Production database management
prod-db-create:
@echo "🏭 Setting up production database..."
@echo "Creating production database if it doesn't exist..."
@mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "CREATE DATABASE IF NOT EXISTS sortingoffice;"
@echo "Running migrations on production database..."
@diesel migration run
# Production database management
prod-db-setup:
@echo "🏭 Setting up production database..."
@echo "Creating production database if it doesn't exist..."
@mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "CREATE DATABASE IF NOT EXISTS sortingoffice;"
@echo "Running migrations on production database..."
@diesel migration run
@echo "✅ Production database setup complete!"
@echo "⚠️ Note: Seed data is not automatically loaded in production for safety."
@echo " Use 'make seed' (development only) or 'make seed-force' (use with caution) if needed."
# Development database setup (includes seeding)
dev-db-setup:
@echo "🛠️ Setting up development database..."
@echo "Creating development database if it doesn't exist..."
@mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "CREATE DATABASE IF NOT EXISTS sortingoffice;"
@echo "Running migrations on development database..."
@diesel migration run
@echo "Seeding development database..."
@ENVIRONMENT=development make seed
prod-db-reset:
@echo "⚠️ WARNING: This will reset the production database!"
@echo "Are you sure? (y/N)"
@read -p "" confirm; \
if [ "$$confirm" = "y" ] || [ "$$confirm" = "Y" ]; then \
echo "Resetting production database..."; \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "DROP DATABASE IF EXISTS sortingoffice;"; \
make prod-dp-setup
else \
echo "Operation cancelled."; \
fi
prod-db-clean:
@echo "⚠️ WARNING: This will clean the production database!"
@echo "Are you sure? (y/N)"
@read -p "" confirm; \
if [ "$$confirm" = "y" ] || [ "$$confirm" = "Y" ]; then \
echo "Resetting production database..."; \
mysql -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) -e "DROP DATABASE IF EXISTS sortingoffice;"; \
else \
echo "Operation cancelled."; \
fi
# Database inspection commands
list-domains:
@if [ -n "$(MYSQL_DATABASE)" ]; then \
echo "Listing domains in $(MYSQL_DATABASE)..."; \
$(MYSQL) -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) -N -e "SELECT * FROM domains;"; \
else \
echo "Listing domains in default database..."; \
$(MYSQL_CMD) "SELECT * FROM domains;"; \
fi
count-domains:
@if [ -n "$(MYSQL_DATABASE)" ]; then \
echo "Counting domains in $(MYSQL_DATABASE)..."; \
$(MYSQL) -u$(MYSQL_USER) -p$(MYSQL_PASSWORD) -h$(MYSQL_HOST) -P$(MYSQL_PORT) $(MYSQL_DATABASE) -N -e "SELECT COUNT(*) FROM domains;"; \
else \
echo "Counting domains in default database..."; \
$(MYSQL_CMD) "SELECT COUNT(*) FROM domains;"; \
fi
list-aliases:
@$(MYSQL_CMD) "SELECT * FROM aliases;"
count-aliases:
@$(MYSQL_CMD) "SELECT COUNT(*) FROM aliases;"
list-users:
@$(MYSQL_CMD) "SELECT * FROM users;"
count-users:
@$(MYSQL_CMD) "SELECT COUNT(*) FROM users;"
list-relocated:
@$(MYSQL_CMD) "SELECT * FROM relocated;"
count-relocated:
@$(MYSQL_CMD) "SELECT COUNT(*) FROM relocated;"
list-relays:
@$(MYSQL_CMD) "SELECT * FROM relays;"
count-relays:
@$(MYSQL_CMD) "SELECT COUNT(*) FROM relays;"
list-clients:
@$(MYSQL_CMD) "SELECT * FROM clients;"
count-clients:
@$(MYSQL_CMD) "SELECT COUNT(*) FROM clients;"
# Analytics queries
show-analytics-aliases:
@echo "📊 Showing analytics aliases (aliases with 3+ occurrences):"
@$(MYSQL_CMD) "SELECT SUBSTRING_INDEX(mail, '@', 1) as alias_name, COUNT(*) as count FROM aliases WHERE mail LIKE '%@%' GROUP BY SUBSTRING_INDEX(mail, '@', 1) HAVING COUNT(*) >= 3 ORDER BY count DESC;"
show-all-alias-counts:
@echo "📊 Showing all alias name counts:"
@$(MYSQL_CMD) "SELECT SUBSTRING_INDEX(mail, '@', 1) as alias_name, COUNT(*) as count FROM aliases WHERE mail LIKE '%@%' GROUP BY SUBSTRING_INDEX(mail, '@', 1) ORDER BY count DESC;"
test-analytics-function:
@echo "🧪 Testing analytics function with cargo..."
@cargo run --bin test-analytics 2>/dev/null || echo "⚠️ Analytics test binary not found. You can test by visiting a domain show page."
list-databases:
./scripts/list-databases.sh
# Help for database operations
db-help:
@echo "🗄️ Database Management Commands"
@echo "================================"
@echo ""
@echo "Migrations:"
@echo " make migrate - Run pending migrations (development only, blocked in production)"
@echo " e.g. make migrate DATABASE_URL_ARG=mysql://user:pass@host/db"
@echo " make migrate-force - Force run migrations (bypasses production protection)"
@echo " make migrate-revert - Revert last migration (development only, blocked in production)"
@echo " make migrate-revert-force - Force revert migration (bypasses production protection)"
@echo " make migrate-reset - Reset database (development only, blocked in production)"
@echo " make migrate-reset-force - Force reset migrations (bypasses production protection)"
@echo " make migrate-all - Run migrations on all databases (development only, blocked in production)"
@echo " make migrate-all-force - Force run migrations on all databases (bypasses production protection)"
@echo ""
@echo "Seed Data:"
@echo " make seed - Seed database with all initial data (development only, blocked in production)"
@echo " e.g. make seed MYSQL_DATABASE=sortingoffice_dev MYSQL_HOST=127.0.0.1 MYSQL_PORT=3308"
@echo " make seed-force - Force seed database (bypasses production protection - use with caution!)"
@echo " make seed-domains - Seed only domains table"
@echo " make seed-users - Seed only users table"
@echo " make seed-aliases - Seed only aliases table"
@echo " make seed-backups - Seed only backups table"
@echo " make seed-relocated - Seed only relocated table"
@echo " make seed-relays - Seed only relays table"
@echo " make seed-clients - Seed only clients table"
@echo " make create-seed-data - Create default seed data files"
@echo ""
@echo "Test Database:"
@echo " make test-db-setup - Setup test database"
@echo " make test-db-clean - Clean test database"
@echo " make test-db-reset - Reset test database"
@echo ""
@echo "Production Database:"
@echo " make prod-db-create - Create production database"
@echo " make prod-db-setup - Setup production database (migrations only, no seeding)"
@echo " make dev-db-setup - Setup development database (includes seeding)"
@echo " make prod-db-clean - Clean production database (WARNING: destructive)"
@echo " make prod-db-reset - Reset production database (WARNING: destructive) with seed data"
@echo ""
@echo "Database Inspection:"
@echo " make list-domains - List all domains"
@echo " make count-domains - Count all domains"
@echo " make list-aliases - List all aliases"
@echo " make count-aliases - Count all aliases"
@echo " make list-users - List all users"
@echo " make count-users - Count all users"
@echo " make list-relocated - List all relocated"
@echo " make count-relocated - Count all relocated"
@echo " make list-relays - List all relays"
@echo " make count-relays - Count all relays"
@echo " make list-clients - List all clients"
@echo " make count-clients - Count all clients"
@echo " make list-databases - List all configured databases (id, label, url) from config.toml"
@echo " e.g. make list-databases"
@echo ""
@echo "Analytics:"
@echo " make seed-analytics-test - Seed database with analytics test data"
@echo " make show-analytics-aliases - Show aliases that would appear in analytics (3+ occurrences)"
@echo " make show-all-alias-counts - Show all alias name counts"
@echo " make test-analytics-function - Test the analytics function"