Skip to content

Commit

Permalink
major rework of db.py. table creation now handled automatically. meth…
Browse files Browse the repository at this point in the history
…ods to handle schema upgrades. error handling and returns added to all methods.
  • Loading branch information
indivisible-irl committed Nov 21, 2013
1 parent 5aaf27d commit edec6c7
Showing 1 changed file with 91 additions and 16 deletions.
107 changes: 91 additions & 16 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@
class DB:

def __init__(self):
# info for all tables. # increment version number when updating create (look at self.__on_upgrade() too)
self.tables = {
'mail' : "sender text, recipient text, message text, id text",
'projects' : "name text, language text, link text, description text, id text"
}
## table | vers | create statement
'tables' : (1,
"table_name text PRIMARY KEY, current_version integer NOT NULL"),
'mail' : (1,
"sender text NOT NULL, recipient text NOT NULL, message text NOT NULL, id text NOT NULL"),
'projects' : (1,
"name text NOT NULL, language text NULL, link text NULL, description text NULL, id text NOT NULL"),
'users' : (1,
"_id integer PRIMARY KEY DEFAULT nextval('serial'), nick text NOT NULL, userlevel integer NOT NULL DEFAULT 0, last_online integer NOT NULL"),
'logs' : (1,
"_id integer PRIMARY KEY DEFAULT nextval('serial'), time type text NOT NULL, log text NOT NULL")
}
self.__ensure_all_tables_correct()

#future ideas:
# mail: "_id integer PRIMARY KEY DEFAULT nextval('serial'), sender text NOT NULL, recipient text NOT NULL, time_sent integer NOT NULL, message text NOT NULL"
# proj: "_id integer PRIMARY KEY DEFAULT nextval('serial'), name text NOT NULL, lang text NULL, owner text NOT NULL, link text NULL, descrip text NOT NULL, status text"
# user: possibly include timezone settings to localise timestamps on logs, mail etc for each user (maybe with a '!!me' command group)

def db_connect(self):
config = settings.read_config()
Expand All @@ -21,13 +37,14 @@ def db_connect(self):
port = config['db_port'])
return conn
except psycopg2.Error as e:
print("Error connecting to db")
print("!! Error connecting to db")
print(e)
return None

def db_add_table(self,table_name,table_info):
# table_info should follow proper sql format.
# i.e: db_add_table("test", "id PRIMARY_KEY, name varchar(20, sample_data text)")
print(".. Creating table: %s...\n Schema: %s" % (table_name, table_info))
try:
if not self.db_check_table(table_name):
conn = self.db_connect()
Expand All @@ -40,7 +57,7 @@ def db_add_table(self,table_name,table_info):
print("!! Error creating table %s. Already exists" % table_name)
return False
except psycopg2.Error as e:
print("Error creating new table: %s" % table_name)
print("!! Error creating new table: %s" % table_name)
print(e)
return False
finally:
Expand All @@ -62,7 +79,7 @@ def db_drop_table(self,table_name):
ircHelpers.sayInChannel("There is no table: %s" % table_name)
return False
except psycopg2.Error as e:
print("Error dropping table: %s" % table_name)
print("!! Error dropping table: %s" % table_name)
print(e)
return False
finally:
Expand Down Expand Up @@ -91,7 +108,7 @@ def db_add_data(self,table_name,data):
ircHelpers.sayInChannel("There is no table: %s" % table_name)
return False
except psycopg2.Error as e:
print("Error adding data to table: %s" % table_name)
print("!! Error adding data to table: %s" % table_name)
print(e)
return False
finally:
Expand All @@ -112,7 +129,7 @@ def db_get_data(self,table_name,condition_column_name,condition_value,):
else:
return None
except psycopg2.Error as e:
print("Error retrieving data from table: %s" % table_name)
print("!! Error retrieving data from table: %s" % table_name)
print(e)
return None
finally:
Expand All @@ -133,7 +150,7 @@ def db_get_all_data(self,table_name):
else:
return None
except psycopg2.Error as e:
print("Error retrieving data from table: %s" % table_name)
print("!! Error retrieving data from table: %s" % table_name)
print(e)
return None
finally:
Expand All @@ -155,7 +172,7 @@ def db_delete_data(self,table_name,condition_column_name,condition_value):
ircHelpers.sayInChannel("There is no table: %s" % table_name)
return False
except psycopg2.Error as e:
print("Error deleting data from table: %s" % table_name)
print("!! Error deleting data from table: %s" % table_name)
print(e)
return False
finally:
Expand All @@ -177,7 +194,7 @@ def db_update_data(self,table_name,column_name,changed_value,condition_column_na
ircHelpers.sayInChannel("There is no table: %s" % table_name)
return False
except psycopg2.Error as e:
print("Error updating data in table: %s" % table_name)
print("!! Error updating data in table: %s" % table_name)
print(e)
return False
finally:
Expand All @@ -198,7 +215,7 @@ def db_check_table(self,table_name):
print("!! DB Table not exists: %s" % table_name)
return response
except psycopg2.Error as e:
print("Error checking table exists: %s [ironic, right?]" % table_name)
print("!! Error checking table exists: %s [ironic, right?]" % table_name)
print(e)
return False ##ASK Really want to return False on fail here?? Table may actually exist.
finally:
Expand All @@ -207,11 +224,69 @@ def db_check_table(self,table_name):
except Exception:
pass

def ensure_all_tables_exist(self):
pass
def __ensure_all_tables_correct(self):
all_tables = self.tables.keys()
unimplemented_tables = ('logs', 'users', 'tables')
all_successful = True
for table in all_tables:
if table in unimplemented_tables:
continue
if self.__ensure_table_correct(table):
print(".. Table found: %s" % table)
else:
print("!! Table not found: %s" % table)
if self.__recreate_table(table):
print(".. Created table: %s" % table)
else:
print("!! Failed to create table: %s" % table)
all_successful = False
return all_successful

def __ensure_table_correct(self, table_name):
# TODO Test old/new version numbers and trigger __on_upgrade, __recreate etc etc
# For now we'll just test if table exists
return self.db_check_table(table_name)

def __recreate_table(self, table_name):
go_for_new = False
if self.db_check_table(table_name):
if self.db_drop_table(table_name):
go_for_new = True
else:
go_for_new = True

if go_for_new:
if self.db_add_table(table_name, self.tables[table_name][1]):
return True
return False

def recreate_table(self, table_name):
pass

def __on_upgrade(self, table_name, new_version, old_version=0):
# Can eventually handle upgrades to tables without losing data
# by making use of the version numbers
# For now just destroy and recreate the tables
# Skipping the projects table to retain info

if table_name == 'mail':
return self.__recreate_table('mail')

elif table_name == 'projects':
if self.db_check_table('projects'):
print(".. Attempt to upgrade/recreate Projects table. Ignored to preserve info until upgrade possible")
return True # cheaty
else:
return self.__recreate_table('projects')

elif table_name == 'users':
return self.__recreate_table('users')

elif table_name == 'logs':
return self.__recreate_table('logs')

else:
print("Unsupported table: %s. Add to db.tables and db.__on_upgrade to deploy")
return False


if __name__ == "__main__":
pass

0 comments on commit edec6c7

Please sign in to comment.