create_invoice_history_table.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. 轻量建表脚本:创建 `aigcspace.invoice_info_history` 表
  3. 使用项目的数据库连接配置(app.database.get_engine / Session)执行建表 SQL。
  4. 运行方式(在 backend 目录下或将工作目录设置为项目根):
  5. python -m scripts.create_invoice_history_table
  6. 该脚本仅执行一次建表(使用 IF NOT EXISTS),适用于没有 Alembic 的快速部署场景。
  7. """
  8. import sys
  9. from pathlib import Path
  10. # Ensure package imports work when running script from project root or other cwd.
  11. # Insert the `backend` directory to sys.path so `import app` resolves.
  12. sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
  13. from sqlalchemy import text
  14. from app.database import engine as db_engine
  15. SQL = r"""
  16. CREATE SCHEMA IF NOT EXISTS aigcspace;
  17. CREATE TABLE IF NOT EXISTS aigcspace.invoice_info_history (
  18. id BIGSERIAL PRIMARY KEY,
  19. user_id VARCHAR(50) NOT NULL REFERENCES aigcspace.users(id) ON DELETE CASCADE,
  20. company_name VARCHAR(200) NOT NULL,
  21. tax_number VARCHAR(100),
  22. invoice_type VARCHAR(50),
  23. address VARCHAR(300),
  24. phone VARCHAR(50),
  25. register_address VARCHAR(300),
  26. register_phone VARCHAR(50),
  27. bank VARCHAR(200),
  28. bank_account VARCHAR(100),
  29. email VARCHAR(200),
  30. receiver_name VARCHAR(100),
  31. receiver_mobile VARCHAR(50),
  32. receiver_address VARCHAR(300),
  33. created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
  34. );
  35. CREATE INDEX IF NOT EXISTS idx_invoice_info_history_user ON aigcspace.invoice_info_history(user_id);
  36. """
  37. def main():
  38. engine = db_engine
  39. with engine.connect() as conn:
  40. print("Creating table aigcspace.invoice_info_history (if not exists)...")
  41. conn.execute(text(SQL))
  42. conn.commit()
  43. print("Done.")
  44. if __name__ == '__main__':
  45. main()