]> git.datanom.net - securemail.git/commitdiff
Add feature to create tables in database
authorMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 22:31:54 +0000 (00:31 +0200)
committerMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 22:31:54 +0000 (00:31 +0200)
.gitignore
db.py

index 69fb7d400043ad06705d3ce57dc9fbbad7115c87..303883f2d54cbc8c42fd12a9b975c0dda899b4e6 100644 (file)
@@ -13,6 +13,7 @@ _ropeproject/
 *.bak
 *.rej
 *~
+*.db
 cur/
 tmp/
 __pycache__/
diff --git a/db.py b/db.py
index 9715b89a162d8201064e5bc871fd6ede5e158ae7..cabd8bf1da347429613491854d6b8b9d39d21447 100644 (file)
--- a/db.py
+++ b/db.py
 # along with SecureMail.  If not, see <https://www.gnu.org/licenses/>.
 
 # sqlite
-create table account (
-id int auto_increment,
-token char(128) unique not null,
-cipher text not null,
-# primary key (id));
-#
+sqlite_sql = """create table account (
+id int auto_increment,
+token char(128) unique not null,
+cipher text not null,
+primary key (id))"""
+
 # mysql
-create table account (
-id int auto_increment,
-token char(128) unique not null,
-cipher text not null,
-# primary key (id));
-#
+mysql_sql = """create table account (
+id int auto_increment,
+token char(128) unique not null,
+cipher text not null,
+primary key (id))"""
+
 # postgresql
-create table account (
-id serial,
-token char(128) unique not null,
-cipher bytea not null,
-# primary key (id));
+postgresql_sql = """create table account (
+id serial,
+token char(128) unique not null,
+cipher bytea not null,
+primary key (id))"""
 
 import base64
 from config import DBTYPE, DBNAME
@@ -139,3 +139,65 @@ class DBInterface:
             raise e
         finally:
             cursor.close()
+
+    @staticmethod
+    def create_database():
+        if DBTYPE == 'mysql':
+            from MySQLdb import Error as DBError
+        elif DBTYPE == 'postgresql':
+            from psycopg2 import Error as DBError
+        elif DBTYPE == 'sqlite':
+            from apsw import Error as DBError
+        conn = DB().get_connection()
+        cursor = conn.cursor()
+        try:
+            if DBTYPE != 'sqlite':
+                if DBTYPE == 'mysql':
+                    sql = mysql_sql
+                elif DBTYPE == 'postgresql':
+                    sql = postgresql_sql
+                cursor.execute(sql)
+                conn.commit()
+            else:
+                cursor.execute('begin')
+                cursor.execute(sqlite_sql)
+                cursor.execute('commit')
+        except DBError as e:
+            print (e)
+            if DBTYPE != 'sqlite':
+                conn.rollback()
+            else:
+                cursor.execute('rollback')
+            raise e
+        finally:
+            cursor.close()
+
+def main():
+    from optparse import OptionParser
+    
+    usage = "usage: %prog [options] arg"
+    parser = OptionParser(usage)
+    parser.add_option("-c", "--create", action="store_true", dest="create",
+                      help="Create tables in database using config.py", default=False)
+    parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
+                      help="Run in verbose mode", default=False)
+    (options, args) = parser.parse_args()
+    
+    if  options.create:
+        try:
+            if options.verbose:
+                print("Creating empty database")
+                print("Database Engine: {0}".format(DBTYPE))
+                if DBTYPE != 'sqlite':
+                    print("Database Host: {0}".format(DBHOST))
+                    print("Database Port: {0}".format(DBPORT))
+                else:
+                    print("Database File: ./{0}.db".format(DBNAME))
+            DBInterface.create_database()
+            print("Database created")
+        except Exception as e:
+            print("Creating database failed!")
+            print(e)
+        
+if __name__ == '__main__':
+    main()
This page took 0.036067 seconds and 5 git commands to generate.