31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
import csv
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
CSV_PATH = ROOT / 'news_dates.csv'
|
|
OUT_PATH = ROOT / 'news_dates.sql'
|
|
|
|
def main():
|
|
with CSV_PATH.open(newline='', encoding='utf-8') as f:
|
|
reader = csv.DictReader(f)
|
|
rows = list(reader)
|
|
|
|
with OUT_PATH.open('w', encoding='utf-8', newline='\n') as out:
|
|
out.write('-- Generated from news_dates.csv\n')
|
|
out.write('-- Sets published_at, created_at, updated_at to CSV date\n')
|
|
out.write('BEGIN;\n')
|
|
for row in rows:
|
|
news_id = (row.get('news_id') or '').strip().strip('"')
|
|
date = (row.get('update_date') or '').strip().strip('"')
|
|
if not news_id or not date:
|
|
continue
|
|
date = date.replace("'", "''")
|
|
out.write(
|
|
f"UPDATE news_articles SET published_at = '{date}', created_at = '{date}', updated_at = '{date}' WHERE news_id = {news_id};\n"
|
|
)
|
|
out.write('COMMIT;\n')
|
|
|
|
if __name__ == '__main__':
|
|
main()
|