feat: 建表脚本 + build.sh 集成建表和初始化数据
This commit is contained in:
parent
dcddf9dabf
commit
a7cc65da0f
16
build.sh
16
build.sh
@ -132,6 +132,22 @@ for d in sd_projects sd_iterations; do
|
||||
done
|
||||
|
||||
|
||||
# 10.6 Create module tables (models -> DDL -> execute)
|
||||
echo "=== Module table creation ==="
|
||||
if [ -f "$cdir/scripts/create_tables.py" ]; then
|
||||
"$cdir/py3/bin/python" "$cdir/scripts/create_tables.py" || echo " WARN: create_tables.py failed (DB not ready?)"
|
||||
else
|
||||
echo " WARN: scripts/create_tables.py not found"
|
||||
fi
|
||||
|
||||
# 10.7 Import module init data (appcodes dictionaries)
|
||||
echo "=== Module init data import ==="
|
||||
if [ -f "$cdir/scripts/import_init.py" ]; then
|
||||
"$cdir/py3/bin/python" "$cdir/scripts/import_init.py" || echo " WARN: import_init.py failed (DB not ready?)"
|
||||
else
|
||||
echo " WARN: scripts/import_init.py not found"
|
||||
fi
|
||||
|
||||
# 11. Import RBAC permissions from conf/rp.json (r:p 数据 -> permission + rolepermission)
|
||||
echo "=== RBAC permission import ==="
|
||||
if [ -f "$cdir/scripts/import_rp.py" ]; then
|
||||
|
||||
62
scripts/create_tables.py
Normal file
62
scripts/create_tables.py
Normal file
@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
"""建表脚本:从各模块 models/*.json 生成 DDL 并在 pipeline 库执行。
|
||||
|
||||
对每个模块,调用 json2ddl 生成 DDL,按分号分割后逐条执行(幂等:DDL 含 DROP TABLE IF EXISTS)。
|
||||
|
||||
用法:
|
||||
py3/bin/python scripts/create_tables.py
|
||||
"""
|
||||
import sys, os, asyncio, subprocess
|
||||
|
||||
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOT_DIR = os.path.dirname(SCRIPT_DIR)
|
||||
sys.path.insert(0, os.path.join(ROOT_DIR, 'py3', 'lib', 'python3.10', 'site-packages'))
|
||||
sys.path.insert(0, ROOT_DIR)
|
||||
|
||||
from sqlor.dbpools import DBPools
|
||||
from appPublic.jsonConfig import getConfig
|
||||
from appPublic.folderUtils import ProgramPath
|
||||
from ahserver.serverenv import ServerEnv
|
||||
from ahserver.globalEnv import initEnv
|
||||
|
||||
TABLE_MODULES = ['product_management', 'discount', 'pricing', 'unipay', 'smssend']
|
||||
|
||||
|
||||
def split_ddl(ddl):
|
||||
"""按分号分割 DDL,跳过注释行和空语句。"""
|
||||
stmts = []
|
||||
for raw in ddl.split(';'):
|
||||
lines = [l for l in raw.split('\n') if not l.strip().startswith('--')]
|
||||
s = '\n'.join(lines).strip()
|
||||
if s:
|
||||
stmts.append(s)
|
||||
return stmts
|
||||
|
||||
|
||||
async def main():
|
||||
config = getConfig(ROOT_DIR, NS={'workdir': ROOT_DIR, 'ProgramPath': ProgramPath()})
|
||||
DBPools(config.databases)
|
||||
initEnv()
|
||||
env = ServerEnv()
|
||||
env.get_module_dbname = lambda m: 'pipeline' if 'pipeline' in m else 'sage'
|
||||
|
||||
json2ddl = os.path.join(ROOT_DIR, 'py3', 'bin', 'json2ddl')
|
||||
async with DBPools().sqlorContext('pipeline') as sor:
|
||||
for mod in TABLE_MODULES:
|
||||
models_dir = os.path.join(ROOT_DIR, 'pkgs', mod, 'models')
|
||||
if not os.path.isdir(models_dir):
|
||||
print(f' skip {mod}: no models dir')
|
||||
continue
|
||||
r = subprocess.run([json2ddl, 'mysql', models_dir], capture_output=True, text=True)
|
||||
if r.returncode != 0 or not r.stdout.strip():
|
||||
print(f' skip {mod}: json2ddl failed')
|
||||
continue
|
||||
n = 0
|
||||
for stmt in split_ddl(r.stdout):
|
||||
await sor.execute(stmt, {})
|
||||
n += 1
|
||||
print(f' tables: {mod} created ({n} statements)')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
Loading…
x
Reference in New Issue
Block a user