:PROPERTIES: :ID: 0695048f-6974-4a55-be6c-75de2dcdecec :END: #+title: 2022-05-18 * [[id:d06d3ab4-c2d0-47c3-aae1-4395567fc3d2][Normalizing tags]] in production :PROPERTIES: :header-args:sql: :engine postgresql :cmdline "-U postgres postgres" :dir /docker:postgres: :exports both :cache yes :eval no-export :END: #+CAPTION: Create a table to track normalized accounts #+begin_src sql DROP TABLE IF EXISTS accounts; CREATE TABLE accounts AS SELECT account_id, COUNT(tag) AS total, FALSE AS normalized FROM invalid_tags GROUP BY account_id; #+end_src #+RESULTS[ee123c16042ab3a3381135fd50e2e474a126af94]: | DROP TABLE | |-------------| | SELECT 3220 | #+CAPTION: Find the least affected accounts first #+begin_src sql SELECT COUNT(*) FROM accounts WHERE NOT normalized AND total < 100 #+end_src #+RESULTS[b0aec0d7b3e1a161356aec4a1d23b1b1fe3d82c5]: | count | |-------| | 2241 | #+CAPTION: Find the least affected accounts first #+NAME: first-run-accounts #+begin_src sql :results silent SELECT account_id FROM accounts WHERE NOT normalized AND total = 1 #+end_src #+begin_src emacs-lisp :var accounts=first-run-accounts :results file :eval no-export (let ((filename "~/git/normalize_account_tags/production/01-single-tag-accounts")) (with-temp-file filename (insert (s-join "\n" (-map #'first accounts)))) filename) #+end_src #+RESULTS: [[file:~/git/normalize_account_tags/production/01-single-tag-accounts]] #+begin_example ######################################## ### COMPLETED 473/473 (0 remaining) ### ELAPSED: 02:21:49 ### ESTIMATED REMAINING: 00:00:00 ######################################## #+end_example #+begin_src sql UPDATE accounts SET normalized = TRUE WHERE NOT normalized AND total = 1 #+end_src #+RESULTS[f9106d0f56ea4455273bfaa63a4b4576d8a30f7f]: | UPDATE 474 | |------------|