-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmigration-name-research.sql
More file actions
81 lines (72 loc) · 2.68 KB
/
migration-name-research.sql
File metadata and controls
81 lines (72 loc) · 2.68 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
-- Migration: Create name_research table and migrate data
-- This separates ENS name research into a reusable cache
-- Step 1: Create the new name_research table
CREATE TABLE IF NOT EXISTS name_research (
id SERIAL PRIMARY KEY,
ens_name VARCHAR(255) NOT NULL UNIQUE,
research_text TEXT NOT NULL,
researched_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
source VARCHAR(50) DEFAULT 'web_search',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Step 2: Create index for fast lookups
CREATE INDEX IF NOT EXISTS idx_name_research_ens_name ON name_research(ens_name);
CREATE INDEX IF NOT EXISTS idx_name_research_researched_at ON name_research(researched_at);
-- Step 3: Migrate existing research data from ai_replies
-- Extract unique ENS names and their most recent research
INSERT INTO name_research (ens_name, research_text, researched_at, source)
SELECT DISTINCT ON (ens_name)
COALESCE(s.nft_name, r.ens_name) as ens_name,
ar.name_research,
ar.created_at as researched_at,
'migrated' as source
FROM ai_replies ar
LEFT JOIN processed_sales s ON ar.sale_id = s.id
LEFT JOIN ens_registrations r ON ar.registration_id = r.id
WHERE ar.name_research IS NOT NULL
AND ar.name_research != ''
AND (s.nft_name IS NOT NULL OR r.ens_name IS NOT NULL)
ORDER BY ens_name, ar.created_at DESC
ON CONFLICT (ens_name) DO NOTHING;
-- Step 4: Add name_research_id column to ai_replies
ALTER TABLE ai_replies ADD COLUMN IF NOT EXISTS name_research_id INTEGER REFERENCES name_research(id);
-- Step 5: Populate name_research_id for existing records
UPDATE ai_replies ar
SET name_research_id = nr.id
FROM name_research nr
WHERE nr.ens_name = (
SELECT COALESCE(s.nft_name, r.ens_name)
FROM processed_sales s
FULL OUTER JOIN ens_registrations r ON FALSE
WHERE s.id = ar.sale_id OR r.id = ar.registration_id
LIMIT 1
)
AND ar.name_research_id IS NULL;
-- Step 6: Verify migration
SELECT
'name_research' as table_name,
COUNT(*) as total_records,
COUNT(DISTINCT ens_name) as unique_names
FROM name_research
UNION ALL
SELECT
'ai_replies (linked)' as table_name,
COUNT(*) as total_records,
COUNT(DISTINCT name_research_id) as unique_research_ids
FROM ai_replies
WHERE name_research_id IS NOT NULL;
-- Step 7: Show sample data
SELECT
nr.ens_name,
nr.researched_at,
nr.source,
COUNT(ar.id) as reply_count
FROM name_research nr
LEFT JOIN ai_replies ar ON ar.name_research_id = nr.id
GROUP BY nr.id, nr.ens_name, nr.researched_at, nr.source
ORDER BY reply_count DESC
LIMIT 5;
-- Migration complete!
-- Note: We're NOT dropping the name_research column from ai_replies yet
-- This allows for safe rollback and verification before final cleanup