News: normalize category select values; fix Studio news editor category persistence; add CSV→SQL generator and news_dates.sql
This commit is contained in:
30
scripts/csv_to_sql.py
Normal file
30
scripts/csv_to_sql.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/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()
|
||||
Reference in New Issue
Block a user