table creation sql script
This commit is contained in:
parent
32fb68c5ea
commit
d044943309
83
migration.py
Normal file
83
migration.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
"""Create the necessary tables in an SQLite database"""
|
||||||
|
|
||||||
|
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
def connect_db():
|
||||||
|
return sqlite3.connect('database/database.sqlite')
|
||||||
|
|
||||||
|
|
||||||
|
def create_user_table():
|
||||||
|
query = """
|
||||||
|
create table if not exists users (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT,
|
||||||
|
email TEXT,
|
||||||
|
username TEXT NOT NULL UNIQUE,
|
||||||
|
password TEXT NOT NULL,
|
||||||
|
created_on TEXT default CURRENT_TIMESTAMP,
|
||||||
|
active BOOLEAN default TRUE
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
connection = connect_db()
|
||||||
|
connection.execute(query)
|
||||||
|
connection.commit()
|
||||||
|
except Exception as e:
|
||||||
|
print("Error creating users table", e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def create_keys_table():
|
||||||
|
query = """
|
||||||
|
create table if not exists keys (
|
||||||
|
user_id INTEGER,
|
||||||
|
encryption_key TEXT,
|
||||||
|
encryption_key_salt TEXT,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users (id)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
connection = connect_db()
|
||||||
|
connection.execute(query)
|
||||||
|
connection.commit()
|
||||||
|
except Exception as e:
|
||||||
|
print("Error creating keys table", e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def create_secrets_table():
|
||||||
|
query = """
|
||||||
|
create table if not exists secrets (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
user_id INTEGER,
|
||||||
|
data TEXT NOT NULL,
|
||||||
|
added_on TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
modified_on TEXT,
|
||||||
|
active BOOLEAN DEFAULT TRUE,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
connection = connect_db()
|
||||||
|
connection.execute(query)
|
||||||
|
connection.commit()
|
||||||
|
except Exception as e:
|
||||||
|
print("Error creating secrets tabe", e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
create_user_table()
|
||||||
|
create_keys_table()
|
||||||
|
create_secrets_table()
|
Loading…
Reference in New Issue
Block a user