Last active
January 13, 2016 05:54
-
-
Save pdvyas/2d1f6bfa0972f7870f25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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