Advertisement
NLinker

Immutable update to apple_mapping

Feb 10th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE apple_mapping_dev0210x
  2. (
  3.   id          BIGSERIAL PRIMARY KEY NOT NULL,
  4.   entity_uuid UUID,
  5.   entity_type INTEGER,
  6.   external_id VARCHAR(128),
  7.   is_deleted  BOOLEAN,
  8.   created_at  TIMESTAMP,
  9.   updated_at  TIMESTAMP
  10. );
  11. INSERT INTO apple_mapping_dev0210x (
  12.   id,
  13.   entity_uuid,
  14.   entity_type,
  15.   external_id,
  16.   is_deleted,
  17.   created_at,
  18.   updated_at
  19. )(
  20.   SELECT
  21.     id,
  22.     entity_uuid,
  23.     entity_type,
  24.     external_id,
  25.     true,
  26.     now(),
  27.     now()
  28.   FROM apple_mapping
  29. );
  30. -- THE easiest way to ensure the ALTER statements below work
  31. DROP TABLE apple_mapping_dev0210;
  32. ALTER TABLE apple_mapping_dev0210x RENAME TO apple_mapping_dev0210;
  33. ALTER SEQUENCE apple_mapping_dev0210x_id_seq
  34.   RENAME TO apple_mapping_dev0210_id_seq;
  35. SELECT setval('apple_mapping_dev0210_id_seq' :: REGCLASS,
  36.               (SELECT max(id) FROM apple_mapping) :: BIGINT);
  37. ALTER TABLE apple_mapping_dev0210 ALTER COLUMN id
  38.   SET DEFAULT nextval('apple_mapping_dev0210_id_seq' :: REGCLASS);
  39.  
  40. -- do this at the end of the processing
  41. -- when all the stuff gets ready
  42. CREATE UNIQUE INDEX apple_mapping_dev0210_external_id_entity_type_idx ON apple_mapping_dev0210 (entity_type, external_id);
  43. CREATE INDEX apple_mapping_dev0210_stuff0_idx ON apple_mapping_dev0210 (entity_uuid, is_deleted);
  44. CREATE INDEX apple_mapping_dev0210_entity_uuid_idx ON apple_mapping_dev0210 (entity_uuid);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement