diff --git a/lib/db.py b/lib/db.py index a6bca83..324251e 100644 --- a/lib/db.py +++ b/lib/db.py @@ -46,7 +46,8 @@ class DB: def create_tables(self): """Create the tables if not existing - assumes connected""" - commands = ( + schemes = dict() + schemes[1] = ( """ CREATE TABLE scheme ( id SERIAL PRIMARY KEY, @@ -54,7 +55,7 @@ class DB: ) """, "INSERT INTO scheme (version) VALUES(1)", - """ DROP TABLE IF EXISTS revisions""", + """DROP TABLE IF EXISTS revisions """, """ CREATE TABLE revisions ( id SERIAL PRIMARY KEY, project VARCHAR(255) NOT NULL, @@ -71,14 +72,32 @@ class DB: CREATE UNIQUE INDEX ppr ON revisions (project, package, rev); """, ) + schemes[2] = ( + """DROP TABLE IF EXISTS links""", + """ + CREATE TABLE links ( + id SERIAL PRIMARY KEY, + revision_id INTEGER, + project VARCHAR(255) NOT NULL, + package VARCHAR(255) NOT NULL + ) + """, + """ + UPDATE scheme SET version=2 + """, + ) - if self.schema_version() > 0: + schema_version = self.schema_version() + if (schema_version + 1) not in schemes: return try: cur = self.conn.cursor() # create table one by one - for command in commands: - cur.execute(command) + for version, commands in schemes.items(): + if version <= schema_version: + continue + for command in commands: + cur.execute(command) # close communication with the PostgreSQL database server cur.close() # commit the changes diff --git a/lib/db_revision.py b/lib/db_revision.py index 02109d6..7fe99c8 100644 --- a/lib/db_revision.py +++ b/lib/db_revision.py @@ -14,8 +14,12 @@ class DBRevision: ) = row def links_to(self, db, project, package): - pass - + db.cursor().execute( + "INSERT INTO links (revision_id, project, package) VALUES (%s,%s,%s)", + (self.dbid, project, package), + ) + print("links_to", project, package) + @classmethod def import_obs_rev(cls, db, revision): cur = db.cursor() @@ -41,7 +45,7 @@ class DBRevision: cur = db.cursor() cur.execute( "SELECT * FROM revisions where project=%s and package=%s and rev=%s", - (project, package, rev), + (project, package, str(rev)), ) row = cur.fetchone() cur.close()