Skip to content

Commit

Permalink
Merge pull request #37 from indivisible-irl/master
Browse files Browse the repository at this point in the history
Major database rejig,

Ok, if there are no objections I'll merge. We can revisit the redundant table exists checking later but as I said - the updates will be called rarely (and now that I think about it likely before the bot even connects to a server).
  • Loading branch information
indivisible-irl committed Nov 22, 2013
2 parents 5bab9bb + 122439b commit 0138e23
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 19 deletions.
4 changes: 2 additions & 2 deletions addons/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def send_mail(self, arguments, messageInfo):
mail_id = binascii.b2a_hex(os.urandom(3)).decode()
mail_dict = { "sender" : sender, "recipient" : recipient, "message" : message.strip("\r"), "id" : mail_id }
if DB().db_add_data("mail", mail_dict):
ircHelpers.pmInChannel(sender, "sent message to %s" % recipient)
ircHelpers.pmInChannel(sender, "sent message to %s: %s" % (recipient, message.strip("\r")))
return True
else:
ircHelpers.pmInChannel(sender, "Failed to send message to %s" % recipient)
Expand All @@ -41,7 +41,7 @@ def get_mail(self, arguments, messageInfo):
return True
else:
for mail_tuple in data:
ircHelpers.privateMessage(mail_tuple[1], "%s says: %s id: %s" % (mail_tuple[0],mail_tuple[2],mail_tuple[3]))
ircHelpers.privateMessage(mail_tuple[1], "[ %s ] <%s>: %s" % (mail_tuple[3],mail_tuple[0],mail_tuple[2]))
return True

def delete_mail(self, arguments, messageInfo):
Expand Down
3 changes: 2 additions & 1 deletion addons/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def add_projects(self, arguments, messageInfo):
proj_id = binascii.b2a_hex(os.urandom(3)).decode()
proj_dict = { "name" : name, "language" : language, "link" : link, "description" : description.strip("\r"), "id" : proj_id }
if DB().db_add_data("projects", proj_dict):
ircHelpers.sayInChannel("added project %s" % name)
ircHelpers.pmInChannel(messageInfo['user'], "Added project %s. Thank's for taking part!" % name)
ircHelpers.sayInChannel("Added new project: %s" % name)
else:
ircHelpers.pmInChannel("Error trying to add project %s" % name)

Expand Down
107 changes: 91 additions & 16 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,27 @@
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.unmanaged_tables = ('logs', 'users', 'tables')
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 +38,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 +58,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 +80,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 +109,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 +130,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 +151,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 +173,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 +195,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 +216,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 +225,68 @@ 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()
all_successful = True
for table in all_tables:
if table in self.unmanaged_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 0138e23

Please sign in to comment.