Advertisement
clime

Untitled

Oct 21st, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.37 KB | None | 0 0
  1. ALTER TABLE watson_searchentry ALTER COLUMN engine_slug SET DEFAULT 'default';
  2. ALTER TABLE watson_searchentry ALTER COLUMN description SET DEFAULT '';
  3. ALTER TABLE watson_searchentry ALTER COLUMN url SET DEFAULT '';
  4. ALTER TABLE watson_searchentry ALTER COLUMN meta_encoded SET DEFAULT '{}';
  5. ALTER TABLE watson_searchentry DROP CONSTRAINT IF EXISTS ws_uni;
  6. ALTER TABLE watson_searchentry ADD CONSTRAINT ws_uni UNIQUE (content_type_id, object_id_int);
  7.  
  8.  
  9.         WITH  ctyp AS (
  10.            SELECT id AS content_type_id
  11.            FROM   django_content_type
  12.            WHERE  app_label = 'web'
  13.            AND    model = 'member'
  14.            )
  15.         , sel AS (
  16.            SELECT ctyp.content_type_id
  17.                  ,m.id       AS object_id_int
  18.                  ,m.id::text AS object_id       -- explicit cast!
  19.                  ,m.name     AS title
  20.                  ,concat_ws(' ', u.email,m.normalized_name,c.name) AS content
  21.                  -- other columns have column default now.
  22.            FROM   web_user    u
  23.            JOIN   web_member  m  ON m.user_id = u.id
  24.            JOIN   web_country c  ON c.id = m.country_id
  25.            CROSS  JOIN ctyp
  26.            WHERE  u.is_active
  27.            )
  28.         , del AS (      -- only if you want to del all other entries of same type
  29.            DELETE FROM watson_searchentry
  30.            USING  ctyp
  31.            WHERE  watson_searchentry.content_type_id = ctyp.content_type_id
  32.            AND    NOT EXISTS (
  33.               SELECT 1
  34.               FROM   sel
  35.               WHERE  sel.object_id_int = watson_searchentry.object_id_int
  36.               )
  37.            )
  38.         , up AS (      -- update existing rows
  39.            UPDATE watson_searchentry
  40.            SET    object_id = s.object_id
  41.                  ,title     = s.title
  42.                  ,content   = s.content
  43.            FROM   sel s
  44.            WHERE  watson_searchentry.content_type_id = s.content_type_id
  45.            AND    watson_searchentry.object_id_int   = s.object_id_int
  46.         )
  47.                        -- insert new rows
  48.         INSERT  INTO watson_searchentry (
  49.                 content_type_id, object_id_int, object_id, title, content)
  50.         SELECT  sel.*  -- safe to use, because col list is defined accordingly above
  51.         FROM    sel
  52.         LEFT    JOIN watson_searchentry w1 USING (content_type_id, object_id_int)
  53.         WHERE   w1.content_type_id IS NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement