Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ALTER TABLE watson_searchentry ALTER COLUMN engine_slug SET DEFAULT 'default';
- ALTER TABLE watson_searchentry ALTER COLUMN description SET DEFAULT '';
- ALTER TABLE watson_searchentry ALTER COLUMN url SET DEFAULT '';
- ALTER TABLE watson_searchentry ALTER COLUMN meta_encoded SET DEFAULT '{}';
- ALTER TABLE watson_searchentry DROP CONSTRAINT IF EXISTS ws_uni;
- ALTER TABLE watson_searchentry ADD CONSTRAINT ws_uni UNIQUE (content_type_id, object_id_int);
- WITH ctyp AS (
- SELECT id AS content_type_id
- FROM django_content_type
- WHERE app_label = 'web'
- AND model = 'member'
- )
- , sel AS (
- SELECT ctyp.content_type_id
- ,m.id AS object_id_int
- ,m.id::text AS object_id -- explicit cast!
- ,m.name AS title
- ,concat_ws(' ', u.email,m.normalized_name,c.name) AS content
- -- other columns have column default now.
- FROM web_user u
- JOIN web_member m ON m.user_id = u.id
- JOIN web_country c ON c.id = m.country_id
- CROSS JOIN ctyp
- WHERE u.is_active
- )
- , del AS ( -- only if you want to del all other entries of same type
- DELETE FROM watson_searchentry
- USING ctyp
- WHERE watson_searchentry.content_type_id = ctyp.content_type_id
- AND NOT EXISTS (
- SELECT 1
- FROM sel
- WHERE sel.object_id_int = watson_searchentry.object_id_int
- )
- )
- , up AS ( -- update existing rows
- UPDATE watson_searchentry
- SET object_id = s.object_id
- ,title = s.title
- ,content = s.content
- FROM sel s
- WHERE watson_searchentry.content_type_id = s.content_type_id
- AND watson_searchentry.object_id_int = s.object_id_int
- )
- -- insert new rows
- INSERT INTO watson_searchentry (
- content_type_id, object_id_int, object_id, title, content)
- SELECT sel.* -- safe to use, because col list is defined accordingly above
- FROM sel
- LEFT JOIN watson_searchentry w1 USING (content_type_id, object_id_int)
- WHERE w1.content_type_id IS NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement