Skip to content

feat: add SQL Server database support#1174

Open
H0nGzA1 wants to merge 13 commits into
fastapi-practices:masterfrom
H0nGzA1:worktree-sqlserver-support
Open

feat: add SQL Server database support#1174
H0nGzA1 wants to merge 13 commits into
fastapi-practices:masterfrom
H0nGzA1:worktree-sqlserver-support

Conversation

@H0nGzA1

@H0nGzA1 H0nGzA1 commented May 12, 2026

Copy link
Copy Markdown

Summary

Adds full SQL Server support to the FBA project, enabling SQL Server 2022 as a database backend alongside the existing PostgreSQL, MySQL, and SQLite support.

Changes

Core Database Layer

  • Add sqlserver to DataBaseType enum
  • SQL Server connection URL builder using mssql+aioodbc dialect
  • Master database connection for initial setup / DB creation
  • SQL Server-specific database creation/drop logic
  • DATABASE_DRIVER and DATABASE_TRUST_SERVER_CERTIFICATE settings

SQL Scripts (22 files)

  • Main init/test data scripts for sys_dept, sys_menu, sys_user, sys_role, sys_data_rule, sys_data_scope
  • Plugin init/destroy scripts: code_generator, config, dict, notice, oauth2
  • Both autoincrement and snowflake ID variants
  • T-SQL features: SET IDENTITY_INSERT ON/OFF, GETDATE(), DBCC CHECKIDENT, N'...' for Unicode

CLI (fba init)

  • SQL Server database type prompt with validation
  • Schema name validation for SQL Server identifiers
  • SQL parser whitelist for T-SQL SET, DBCC, GO statements

Unicode Support (NVARCHAR)

  • UniversalStr custom type: NVARCHAR on mssql, VARCHAR elsewhere
  • UniversalText updated: NVARCHAR(MAX) on mssql
  • 18 model files updated from sa.String(N) to UniversalStr(N)
  • N'' prefix for all Chinese/Unicode string literals in SQL scripts

Docker & Dependencies

  • ODBC Driver 18 for SQL Server in Dockerfile
  • SQL Server service in docker-compose (commented, ready to enable)
  • aioodbc>=0.5.0 dependency
  • Environment variable examples

Other

  • Fix: fba init now exits when user cancels database creation
  • Tests: 15 tests covering SQL Server dispatch, CLI, scripts, env, validation

Test Plan

  • 15/15 pytest tests passing
  • Real SQL Server 2022 integration test (init + all scripts)
  • Chinese/Unicode data verified with NVARCHAR + N'' prefix
  • Ruff lint clean
  • fba init tested against remote SQL Server

Lewis_yan added 13 commits May 11, 2026 16:38
SQL Server treats bare 'string' literals as ASCII. Chinese characters
require N'...' prefix for proper Unicode (NVARCHAR) storage. Also
fixes the auto_init flow to exit when user cancels database creation.
SQL Server VARCHAR cannot store Unicode — Chinese characters get silently
corrupted. Add UniversalStr type (NVARCHAR on mssql, VARCHAR elsewhere)
and update UniversalText to use UnicodeText for mssql. Change all 18
model files from sa.String(N) to UniversalStr(N).

Also add missing N'' prefix on multi-line content in notice init SQL.
MySQL/PostgreSQL drivers are Python packages with no system
dependency, but aioodbc requires msodbcsql18 at the OS level.
Use --build-arg INSTALL_MSSQL_ODBC=true to opt in when building
the Docker image for SQL Server.
@wu-clan

wu-clan commented May 13, 2026

Copy link
Copy Markdown
Member
  • [P1] backend/app/task/celery.py:46 新增 DATABASE_TYPE='sqlserver' 后,Celery 结果后端仍默认拼成 db+postgresql+psycopg://...,只对 MySQL 做替换;SQL Server 环境启动 worker/beat 时会连到 SQL Server 主机的 PostgreSQL 驱动并失败。建议增加
    SQL Server 分支,使用同步 SQLAlchemy URL(如 db+mssql+pyodbc://...?...driver=...&TrustServerCertificate=...),或从数据库 URL 构造函数派生同步版。
  • [P2] backend/plugin/code_generator/crud/crud_gen.py:34 在非 MySQL 时直接走 PostgreSQL 的 pg_class/pg_namespace 查询;backend/plugin/code_generator/crud/crud_gen.py:135 列信息查询也使用 pg_attribute/pg_constraint。这次新增了
    code_generator 的 SQL Server 初始化脚本,SQL Server 用户可以启用插件,但“导入表/生成代码”会立即因 SQL Server 不存在这些系统表而失败。建议为 DataBaseType.sqlserver 增加独立的 INFORMATION_SCHEMA/sys.* 查询,并同步补齐 backend/plugin/
    code_generator/utils/type_conversion.py:19 的 SQL Server 类型映射和模板输出。
  • [P2] backend/plugin/validator.py:15 仍只允许 mysql/postgresql,backend/plugin/validator.py:207 也只检查这两个 SQL 目录;同时内置插件如 backend/plugin/dict/plugin.toml:7 仍只声明 ["mysql", "postgresql"]。新增的 SQL Server 插件脚本无法通
    过插件元数据正确声明/校验,第三方 SQL Server 插件会被拒绝或无法被识别为支持 SQL Server。建议把 sqlserver 加入 validator、目录完整性检查和相关 plugin.toml 的 database 列表。
  • [P3] docker-compose.yml:95 的 SQL Server 切换说明只提醒修改 DATABASE_TYPE/HOST/PORT/USER,但示例容器密码在 docker-compose.yml:105 是 YourStrong!Passw0rd,而 .env.server 仍是 123456;按说明操作会连接失败。建议把
    DATABASE_PASSWORD='YourStrong!Passw0rd' 也写进同一条说明,或统一示例密码

@wu-clan

wu-clan commented May 13, 2026

Copy link
Copy Markdown
Member

尽管此 PR 不会修改大量源代码,但仍会带来巨大维护成本,加上我们目前很少有人接触过 SQL Server,所以此 PR 理论上不会合并,感谢您的贡献

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants