import sys

class AccessViewRecord:
    def __init__(self, view, access_user):
        self.view = view
        self.access_user = access_user

f = open(sys.argv[1], mode="r")

dump_file = open(sys.argv[2], mode="w")

rows = f.read().split("\n")

dump_file.write("""
CREATE TABLE evi.access_views
(
    view text COLLATE pg_catalog."default",
    access_user text COLLATE pg_catalog."default",
    CONSTRAINT access_views_access_user_fkey FOREIGN KEY (access_user)
        REFERENCES evi."user" (user_id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE CASCADE,
    CONSTRAINT access_views_view_fkey FOREIGN KEY (view)
        REFERENCES evi.views (view_id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE CASCADE
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS evi.access_views
    OWNER to core;
\n""")

for row in rows:
    if len(row) == 0:
        continue

    cols = row.split(",")
    record = AccessViewRecord(cols[0], cols[1])

    dump_file.write(f"""INSERT INTO evi.access_views (view, access_user)
                    VALUES ('{record.view}', '{record.access_user}');\n""")
