38 lines
1.9 KiB
Python
38 lines
1.9 KiB
Python
import sqlite3, re
|
|
DB = '/root/aff-monitor/db/monitor.sqlite'
|
|
con = sqlite3.connect(DB)
|
|
cur = con.cursor()
|
|
cols = {row[1] for row in cur.execute('PRAGMA table_info(products)')}
|
|
adds = [
|
|
('is_public', 'ALTER TABLE products ADD COLUMN is_public INTEGER DEFAULT 1'),
|
|
('is_featured', 'ALTER TABLE products ADD COLUMN is_featured INTEGER DEFAULT 0'),
|
|
('sort_order', 'ALTER TABLE products ADD COLUMN sort_order INTEGER DEFAULT 100'),
|
|
('internal_pid', 'ALTER TABLE products ADD COLUMN internal_pid TEXT'),
|
|
('provider_pid', 'ALTER TABLE products ADD COLUMN provider_pid TEXT'),
|
|
('slug', 'ALTER TABLE products ADD COLUMN slug TEXT'),
|
|
('aff_code', 'ALTER TABLE products ADD COLUMN aff_code TEXT'),
|
|
("aff_param", "ALTER TABLE products ADD COLUMN aff_param TEXT DEFAULT 'aff'"),
|
|
]
|
|
for name, sql in adds:
|
|
if name not in cols:
|
|
cur.execute(sql)
|
|
print('+', name)
|
|
cur.execute("CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT, updated_at TEXT DEFAULT (datetime('now')))" )
|
|
print('+ settings table ensured')
|
|
cols = {row[1] for row in cur.execute('PRAGMA table_info(products)')}
|
|
if 'is_public' in cols:
|
|
cur.execute('UPDATE products SET is_public=COALESCE(is_public,1)')
|
|
if 'is_featured' in cols:
|
|
cur.execute('UPDATE products SET is_featured=COALESCE(is_featured,0)')
|
|
if 'sort_order' in cols:
|
|
cur.execute('UPDATE products SET sort_order=COALESCE(sort_order,100)')
|
|
rows = cur.execute('SELECT id, name FROM products ORDER BY id').fetchall()
|
|
for pid, name in rows:
|
|
if 'internal_pid' in cols:
|
|
cur.execute('UPDATE products SET internal_pid=COALESCE(internal_pid, ?) WHERE id=?', (f'VPS-{pid:06d}', pid))
|
|
if 'slug' in cols:
|
|
base = re.sub(r'[^a-z0-9]+', '-', (name or '').lower()).strip('-') or f'product-{pid}'
|
|
cur.execute('UPDATE products SET slug=COALESCE(slug, ?) WHERE id=?', (f'{base}-{pid}', pid))
|
|
con.commit()
|
|
print('done', DB)
|