Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37149 amit 1
-- =====================================================================
2
-- Third-party external API: client auth tables + delta-sync timestamps
3
-- =====================================================================
4
-- MUST be applied BEFORE deploying the build that maps update_timestamp
5
-- (Hibernate SELECTs the new columns).
6
--
7
-- Pre-flight sizing (decide plain ALTER vs pt-online-schema-change):
8
--   SELECT table_schema, table_name, table_rows, ROUND(data_length/1024/1024) mb
9
--   FROM information_schema.tables
10
--   WHERE (table_schema='catalog' AND table_name IN ('tag_listing','item','catalog'))
11
--      OR (table_schema='fofo' AND table_name='current_inventory_snapshot');
12
-- MySQL 5.7: ADD COLUMN is ALGORITHM=INPLACE (online DML) but a full table
13
-- rebuild. catalog.* tables are small; if current_inventory_snapshot is
14
-- multi-million rows / >~1GB use pt-osc off-peak.
15
 
16
-- 1. API client auth ---------------------------------------------------
17
 
18
CREATE TABLE fofo.api_client (
19
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
20
  name VARCHAR(100) NOT NULL,
21
  api_key_hash CHAR(64) NOT NULL COMMENT 'lowercase hex SHA-256 of raw key',
22
  active TINYINT(1) NOT NULL DEFAULT 1,
23
  create_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
24
  update_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
25
  PRIMARY KEY (id),
26
  UNIQUE KEY uq_api_client_key_hash (api_key_hash),
27
  UNIQUE KEY uq_api_client_name (name)
28
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
29
 
30
CREATE TABLE fofo.api_client_store (
31
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
32
  api_client_id INT UNSIGNED NOT NULL,
33
  fofo_id INT NOT NULL,
34
  create_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
35
  PRIMARY KEY (id),
36
  UNIQUE KEY uq_client_store (api_client_id, fofo_id),
37
  KEY idx_acs_fofo (fofo_id)
38
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
39
 
40
-- 2. Delta-sync update timestamps -------------------------------------
41
-- Existing rows get the ALTER-time value; acceptable because a partner's
42
-- first sync is always full (no updatedSince).
43
-- ON UPDATE CURRENT_TIMESTAMP fires only when a column value actually
44
-- changes; no-op UPDATEs do not bump it (correct for delta sync).
45
-- Hard DELETEs remain invisible to deltas -> partners advised to run a
46
-- weekly full sync.
47
 
48
ALTER TABLE catalog.tag_listing
49
  ADD COLUMN update_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
50
  ADD KEY idx_tl_update_ts (update_timestamp);
51
 
52
ALTER TABLE catalog.item
53
  ADD COLUMN update_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
54
  ADD KEY idx_item_update_ts (update_timestamp);
55
 
56
ALTER TABLE catalog.catalog
57
  ADD COLUMN update_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
58
 
59
ALTER TABLE fofo.current_inventory_snapshot
60
  ADD COLUMN update_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
61
  ADD KEY idx_cis_update_ts (update_timestamp);
62
 
63
-- 3. Mongo one-time backfill (run in mongo shell, CONTENT db, AFTER the
64
--    build that stamps lastModified in Mongo.insertOrUpdateById is live).
65
--    Docs missing lastModified are treated as always-included in deltas,
66
--    so deploy/backfill ordering is safe either way.
67
--
68
--   db.siteContent.updateMany(
69
--     { lastModified: { $exists: false } },
70
--     { $set: { lastModified: NumberLong(Date.now()) } }
71
--   )
72
 
73
-- 4. Client seeding template -------------------------------------------
74
-- Generate a raw key offline (e.g. openssl rand -hex 32), give it to the
75
-- partner once, store only the hash:
76
--
77
--   INSERT INTO fofo.api_client (name, api_key_hash)
78
--   VALUES ('<PARTNER_NAME>', SHA2('<raw-key>', 256));
79
--
80
--   INSERT INTO fofo.api_client_store (api_client_id, fofo_id)
81
--   SELECT ac.id, fs.id
82
--   FROM fofo.api_client ac
83
--   JOIN fofo.fofo_store fs ON fs.id IN (<fofo ids to expose>)
84
--   WHERE ac.name = '<PARTNER_NAME>';