50 lines
1.4 KiB
Python
Executable File
50 lines
1.4 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import os
|
|
|
|
import psycopg2
|
|
|
|
PG_HOST = os.getenv('PG_HOST', 'localhost')
|
|
PG_PORT = os.getenv('PG_PORT', 5432)
|
|
PG_USER = os.getenv('PG_USER', 'app_user')
|
|
PG_PASSWORD = os.getenv('PG_PASSWORD', 'app_pass')
|
|
PG_DB = os.getenv('PG_DB', 'omnivore')
|
|
PG_TIMEOUT = os.getenv('PG_TIMEOUT', 10)
|
|
|
|
|
|
def batch_update_library_items(conn):
|
|
batch_size = 100
|
|
# update original_content to NULL in batches
|
|
with conn.cursor() as cursor:
|
|
while True:
|
|
cursor.execute(f"""
|
|
UPDATE omnivore.library_item
|
|
SET original_content = NULL
|
|
WHERE ctid IN (
|
|
SELECT ctid
|
|
FROM omnivore.library_item
|
|
WHERE original_content IS NOT NULL
|
|
LIMIT {batch_size}
|
|
)
|
|
""")
|
|
rows_updated = cursor.rowcount
|
|
conn.commit()
|
|
if rows_updated == 0:
|
|
break
|
|
|
|
|
|
# postgres connection
|
|
conn = psycopg2.connect(
|
|
f'host={PG_HOST} port={PG_PORT} dbname={PG_DB} user={PG_USER} \
|
|
password={PG_PASSWORD} connect_timeout={PG_TIMEOUT}')
|
|
print('Postgres connection:', conn.info)
|
|
|
|
try:
|
|
print('Starting migration')
|
|
batch_update_library_items(conn)
|
|
print('Migration complete')
|
|
except Exception as err:
|
|
print('Migration error', err)
|
|
finally:
|
|
print('Closing connections')
|
|
conn.close()
|