"""Add rsa, abschnitt columns to lv_positionen + abschnitt to positionen."""
import sqlite3, os

db_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'data', 'aufmass.db'))
if not os.path.exists(db_path):
    print("No DB found, skipping.")
    exit(0)

conn = sqlite3.connect(db_path)
c = conn.cursor()

c.execute("PRAGMA table_info(lv_positionen)")
cols = [r[1] for r in c.fetchall()]

if 'rsa' not in cols:
    c.execute("ALTER TABLE lv_positionen ADD COLUMN rsa VARCHAR(20)")
    print("Added rsa to lv_positionen")
if 'abschnitt' not in cols:
    c.execute("ALTER TABLE lv_positionen ADD COLUMN abschnitt VARCHAR(100)")
    print("Added abschnitt to lv_positionen")

c.execute("PRAGMA table_info(positionen)")
pcols = [r[1] for r in c.fetchall()]
if 'abschnitt' not in pcols:
    c.execute("ALTER TABLE positionen ADD COLUMN abschnitt VARCHAR(100)")
    print("Added abschnitt to positionen")

conn.commit()
conn.close()
print("Migration done.")
