54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
import sqlite3
|
|
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
DB_PATH = "food_delivery.db"
|
|
|
|
|
|
def get_db_connection() -> sqlite3.Connection:
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
|
|
def init_db() -> None:
|
|
conn = get_db_connection()
|
|
conn.execute("""
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT UNIQUE NOT NULL,
|
|
password_hash TEXT NOT NULL,
|
|
role TEXT NOT NULL DEFAULT 'user',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
""")
|
|
conn.execute("""
|
|
CREATE TABLE IF NOT EXISTS orders (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
customer_name TEXT NOT NULL,
|
|
customer_phone TEXT NOT NULL,
|
|
customer_address TEXT NOT NULL,
|
|
customer_comment TEXT DEFAULT '',
|
|
order_items TEXT NOT NULL,
|
|
total_price INTEGER NOT NULL,
|
|
status TEXT DEFAULT 'Новый',
|
|
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
""")
|
|
columns = [row["name"] for row in conn.execute("PRAGMA table_info(orders)").fetchall()]
|
|
if "customer_comment" not in columns:
|
|
conn.execute("ALTER TABLE orders ADD COLUMN customer_comment TEXT DEFAULT ''")
|
|
|
|
admin_user = conn.execute(
|
|
"SELECT id FROM users WHERE username = ?",
|
|
("admin",),
|
|
).fetchone()
|
|
if admin_user is None:
|
|
conn.execute(
|
|
"INSERT INTO users (username, password_hash, role) VALUES (?, ?, ?)",
|
|
("admin", generate_password_hash("12345"), "admin"),
|
|
)
|
|
|
|
conn.commit()
|
|
conn.close()
|