From 390f4dc76e262034b4555d56534930f2a65a0bfc Mon Sep 17 00:00:00 2001 From: b3yond Date: Thu, 22 Mar 2018 10:25:00 +0100 Subject: [PATCH] if table "user" doesn't exist, db is created --- db.py | 146 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 74 insertions(+), 72 deletions(-) diff --git a/db.py b/db.py index 14bd99e..41c9f16 100644 --- a/db.py +++ b/db.py @@ -14,82 +14,84 @@ class DB(object): dbfile = ':memory:' self.conn = sqlite3.connect(dbfile) self.cur = self.conn.cursor() + self.cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user';") + if self.cur.fetchone()[0] != 'user': + self.create() self.secret = urandom(32) - self.create() def create(self): # init db - self.cur.executescript(''' - CREATE TABLE "user" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `email` TEXT, - `passhash` TEXT, - `enabled` INTEGER DEFAULT 1 - ); - CREATE TABLE "twitter_request_tokens" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `request_token` TEXT, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ) - ); - CREATE TABLE "twitter_accounts" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `client_id` TEXT, - `client_secret` TEXT, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ) - ); - CREATE TABLE "trigger_good" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `words` TEXT, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ) - ); - CREATE TABLE "trigger_bad" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `words` TEXT, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ) - ); - CREATE TABLE "mastodon_instances" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `instance` TEXT, - `client_id` TEXT, - `client_secret` TEXT - ); - CREATE TABLE "mastodon_accounts" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `access_token` TEXT, - `instance_id` TEXT, - `active` INTEGER, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ), - FOREIGN KEY(`instance_id`) REFERENCES `mastodon_instances` ( `id` ) - ); - CREATE TABLE "seen_toots" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `mastodon_accounts_id` INTEGER, - `toot_id` TEXT, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ), - FOREIGN KEY(`mastodon_accounts_id`) REFERENCES `mastodon_accounts` ( `id` ) - ); - CREATE TABLE "mail" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `email` TEXT, - `active` INTEGER, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ) - ); - CREATE TABLE "seen_tweets" ( - `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `user_id` INTEGER, - `twitter_accounts_id` INTEGER, - `tweet_id` TEXT, - FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ) - FOREIGN KEY(`twitter_accounts_id`) REFERENCES `twitter_accounts` ( `id` ) - ); - ''') + self.cur.executescript('\ + CREATE TABLE "user" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `email` TEXT,\ + `passhash` TEXT,\ + `enabled` INTEGER DEFAULT 1\ + );\ + CREATE TABLE "twitter_request_tokens" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `request_token` TEXT,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\ + );\ + CREATE TABLE "twitter_accounts" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `client_id` TEXT,\ + `client_secret` TEXT,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\ + );\ + CREATE TABLE "trigger_good" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `words` TEXT,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\ + );\ + CREATE TABLE "trigger_bad" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `words` TEXT,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\ + );\ + CREATE TABLE "mastodon_instances" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `instance` TEXT,\ + `client_id` TEXT,\ + `client_secret` TEXT\ + );\ + CREATE TABLE "mastodon_accounts" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `access_token` TEXT,\ + `instance_id` TEXT,\ + `active` INTEGER,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ),\ + FOREIGN KEY(`instance_id`) REFERENCES `mastodon_instances` ( `id` )\ + );\ + CREATE TABLE "seen_toots" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `mastodon_accounts_id` INTEGER,\ + `toot_id` TEXT,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` ),\ + FOREIGN KEY(`mastodon_accounts_id`) REFERENCES `mastodon_accounts` ( `id` )\ + );\ + CREATE TABLE "mail" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `email` TEXT,\ + `active` INTEGER,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\ + );\ + CREATE TABLE "seen_tweets" (\ + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,\ + `user_id` INTEGER,\ + `twitter_accounts_id` INTEGER,\ + `tweet_id` TEXT,\ + FOREIGN KEY(`user_id`) REFERENCES `user` ( `id` )\ + FOREIGN KEY(`twitter_accounts_id`) REFERENCES `twitter_accounts` ( `id` )\ + );\ + ') def token(self, email, password): return jwt.encode({