Skip to content

Commit a994735

Browse files
fix(seed): fix schema compatibility for user_digest_preference and schema_documentation
- Fix digest_preferences -> user_digest_preference table name - Fix documentation -> description column name in schema_documentation - Remove reviewed column (not in current schema) - Use username instead of user_id for digest preferences - Fix SchemaDocumentationDedupeInitializer to check table existence before DELETE These fixes ensure the seed script works on fresh Hibernate-created schemas. Co-authored-by: Venkat SF <venkatesh.sakamuri@stayflexi.com>
1 parent 70b22b1 commit a994735

3 files changed

Lines changed: 27 additions & 26 deletions

File tree

‎.env.compose‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ SPRING_PROFILES_ACTIVE=prod
2727

2828
# Demo seeding
2929
DEEPSQL_SEED_DEMO_DATA=1
30-
DEEPSQL_SEED_CONNECTION_NAME=Demo Shop (E-commerce)
30+
DEEPSQL_SEED_CONNECTION_NAME="Demo Shop"
3131

3232
# CORS
3333
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:*,http://localhost:*

‎backend/src/main/java/com/dbaagent/config/SchemaDocumentationDedupeInitializer.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,21 @@ public Object schemaDocumentationDedupeBootstrap(DataSource dataSource,
4747
// One transaction: a half-applied dedupe (rows deleted, index missing)
4848
// would silently re-accumulate duplicates until the next boot.
4949
new TransactionTemplate(txManager).executeWithoutResult(status -> {
50-
int repointed = jdbc.update("""
51-
UPDATE code_knowledge_suggestion s
52-
SET applied_doc_id = l.keep_id
53-
FROM (%s) l
54-
WHERE s.applied_doc_id = l.id
55-
""".formatted(LOSERS));
50+
int repointed = 0;
51+
if (tableExists(jdbc, "code_knowledge_suggestion")) {
52+
repointed = jdbc.update("""
53+
UPDATE code_knowledge_suggestion s
54+
SET applied_doc_id = l.keep_id
55+
FROM (%s) l
56+
WHERE s.applied_doc_id = l.id
57+
""".formatted(LOSERS));
58+
}
5659

57-
int embeddings = jdbc.update(
58-
"DELETE FROM rag_documents WHERE id IN (SELECT id FROM (%s) l)".formatted(LOSERS));
60+
int embeddings = 0;
61+
if (tableExists(jdbc, "rag_documents")) {
62+
embeddings = jdbc.update(
63+
"DELETE FROM rag_documents WHERE id IN (SELECT id FROM (%s) l)".formatted(LOSERS));
64+
}
5965

6066
int removed = jdbc.update(
6167
"DELETE FROM schema_documentation WHERE id IN (SELECT id FROM (%s) l)".formatted(LOSERS));

‎scripts/self-host/seed-demo-data.sh‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -794,26 +794,26 @@ fi
794794
echo ""
795795
echo "Step 8: Seeding digest preferences..."
796796

797-
if [[ -n "${connection_id:-}" && -n "${admin_id:-}" ]]; then
797+
if [[ -n "${connection_id:-}" ]]; then
798798
compose exec -T postgres psql -U postgres -d dba_agent -v ON_ERROR_STOP=1 <<EOSQL
799799
-- Create digest preference for the admin user on the demo connection
800-
INSERT INTO digest_preferences (
801-
id, user_id, connection_id, delivery_method, enabled,
802-
persona_tag, cron_expression, created_at, updated_at
800+
INSERT INTO user_digest_preference (
801+
username, connection_id, delivery_method, enabled,
802+
persona_tag, cron_expression, timezone, created_at, updated_at
803803
)
804804
SELECT
805-
gen_random_uuid(),
806-
${admin_id},
805+
'${DEEPSQL_INITIAL_ADMIN_EMAIL}',
807806
'${connection_id}',
808807
'SLACK_DM',
809808
true,
810-
'dba',
809+
'DBA',
811810
'0 0 9 * * *',
811+
'UTC',
812812
NOW(),
813813
NOW()
814814
WHERE NOT EXISTS (
815-
SELECT 1 FROM digest_preferences
816-
WHERE user_id = ${admin_id} AND connection_id = '${connection_id}'
815+
SELECT 1 FROM user_digest_preference
816+
WHERE username = '${DEEPSQL_INITIAL_ADMIN_EMAIL}' AND connection_id = '${connection_id}'
817817
);
818818
819819
SELECT 'Digest preferences seeded' AS status;
@@ -842,7 +842,7 @@ EOSQL
842842
echo " Generating web-only digest from seeded data..."
843843

844844
pref_id="$(compose exec -T postgres psql -U postgres -d dba_agent -At -c \
845-
"SELECT id FROM digest_preferences WHERE user_id = ${admin_id} AND connection_id = '${connection_id}' LIMIT 1" 2>/dev/null || echo "")"
845+
"SELECT id FROM user_digest_preference WHERE username = '${DEEPSQL_INITIAL_ADMIN_EMAIL}' AND connection_id = '${connection_id}' LIMIT 1" 2>/dev/null || echo "")"
846846

847847
# Build digest content from actual seeded data
848848
compose exec -T postgres psql -U postgres -d dba_agent -v ON_ERROR_STOP=1 <<EOSQL
@@ -923,7 +923,7 @@ EOSQL
923923
echo " Web-only digest created from actual seeded data."
924924
fi
925925
else
926-
echo " Skipping digest preferences (missing connection or admin ID)"
926+
echo " Skipping digest preferences (no connection ID available)"
927927
fi
928928

929929
# ============================================================================
@@ -948,7 +948,7 @@ AND (
948948
-- Add curated business context notes
949949
INSERT INTO schema_documentation (
950950
id, connection_id, object_type, object_name, parent_object,
951-
documentation, source, reviewed, created_at, updated_at
951+
description, source, created_at, updated_at
952952
) VALUES
953953
(
954954
gen_random_uuid()::text,
@@ -958,7 +958,6 @@ INSERT INTO schema_documentation (
958958
NULL,
959959
'Core transaction table tracking all customer orders. Status transitions: pending → confirmed → processing → shipped → delivered (or cancelled/refunded). Foreign key to customers via customer_id. Contains denormalized totals (subtotal, tax_amount, shipping_amount, discount_amount, total_amount) for query performance.',
960960
'USER',
961-
true,
962961
NOW(),
963962
NOW()
964963
),
@@ -970,7 +969,6 @@ INSERT INTO schema_documentation (
970969
NULL,
971970
'Customer master table with profile and loyalty data. The tier column (bronze/silver/gold/platinum) drives discount eligibility and marketing segmentation. loyalty_points accumulate from orders and can be redeemed.',
972971
'USER',
973-
true,
974972
NOW(),
975973
NOW()
976974
),
@@ -982,7 +980,6 @@ INSERT INTO schema_documentation (
982980
NULL,
983981
'Product catalog with inventory tracking. stock_quantity is decremented on order creation and incremented on returns/cancellations. low_stock_threshold triggers inventory alerts. Products can be soft-deleted via is_active=false.',
984982
'USER',
985-
true,
986983
NOW(),
987984
NOW()
988985
),
@@ -994,7 +991,6 @@ INSERT INTO schema_documentation (
994991
NULL,
995992
'High-volume audit trail capturing all INSERT/UPDATE/DELETE operations. old_values and new_values store JSONB diffs. This table grows rapidly and is a candidate for partitioning by changed_at. Currently missing index on (table_name, changed_at) which causes slow queries.',
996993
'USER',
997-
true,
998994
NOW(),
999995
NOW()
1000996
),
@@ -1006,7 +1002,6 @@ INSERT INTO schema_documentation (
10061002
'orders',
10071003
'Order lifecycle status. Valid values: pending, confirmed, processing, shipped, delivered, cancelled, refunded. Queries filtering on status should avoid LOWER() as it prevents index usage.',
10081004
'USER',
1009-
true,
10101005
NOW(),
10111006
NOW()
10121007
)

0 commit comments

Comments
 (0)