Skip to content

Instantly share code, notes, and snippets.

@pdvyas
Last active January 13, 2016 05:54
Show Gist options
  • Save pdvyas/2d1f6bfa0972f7870f25 to your computer and use it in GitHub Desktop.
Save pdvyas/2d1f6bfa0972f7870f25 to your computer and use it in GitHub Desktop.
import psycopg2
conn = psycopg2.connect("dbname=endless")
conn_new = psycopg2.connect("dbname=endless_new")
cur = conn.cursor()
cur_new = conn_new.cursor()
cur.execute('select id from articles')
article_ids = [id[0] for id in cur.fetchall()]
def get_resources(conn, article_id):
cur = conn.cursor()
cur.execute("select resources from articles where id=%s", (article_id,))
return cur.fetchone()[0]
for article_id in article_ids:
resources = get_resources(conn, article_id)
resources_new = get_resources(conn_new, article_id)
assert set(resources_new) == set([a.replace('http://', 'https://') for a in resources])
CREATE OR REPLACE FUNCTION array_replace_txt(text[], text, text)
RETURNS text[]
AS
$$
DECLARE
retVal text[];
BEGIN
FOR I IN array_lower($1, 1)..array_upper($1, 1) LOOP
retVal[I] = replace($1[I], 'http://', 'https://');
END LOOP;
RETURN retVal;
END;
$$
LANGUAGE plpgsql
STABLE
RETURNS NULL ON NULL INPUT;
update articles set resources = array_replace_txt(resources, 'http://', 'https://');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment