2026-11-11 10:29:01 +08:00
from pathlib import Path
import pytest
from alembic import command
from alembic . config import Config
2026-11-14 14:07:26 +08:00
from sqlalchemy import create_engine , inspect , text
2026-11-11 10:29:01 +08:00
def test_v4_preserves_private_assets_and_adds_sharing_tombstones ( tmp_path ) :
2026-11-14 14:07:26 +08:00
config = Config ( str ( Path ( __file__ ) . parents [ 1 ] / " alembic.ini " ) )
path = tmp_path / " upgrade-v4.sqlite "
config . attributes [ " url " ] = " sqlite+aiosqlite:/// " + str ( path )
command . upgrade ( config , " 003 " )
engine = create_engine ( " sqlite:/// " + str ( path ) )
2026-11-11 10:29:01 +08:00
with engine . begin ( ) as connection :
2026-11-14 14:07:26 +08:00
connection . execute (
text (
" INSERT INTO visual_users(id,username,display_name,password_hash,role,disabled,must_change_password,created_at) VALUES( ' owner ' , ' owner ' , ' Owner ' , ' UNCHANGED ' , ' user ' ,0,0,CURRENT_TIMESTAMP) "
)
)
connection . execute (
text (
" INSERT INTO visual_assets(id,owner_id,name,storage_key,kind,mime_type,size_bytes,sha256,tags,favorite,origin,ready,created_at) VALUES( ' asset ' , ' owner ' , ' Original ' , ' owner/asset/source.png ' , ' image ' , ' image/png ' ,123,:sha, ' [] ' ,0, ' upload ' ,1,CURRENT_TIMESTAMP) "
) ,
{ " sha " : " 0 " * 64 } ,
)
command . upgrade ( config , " 004 " )
command . upgrade ( config , " 004 " )
2026-11-11 10:29:01 +08:00
with engine . begin ( ) as connection :
2026-11-14 14:07:26 +08:00
assert connection . scalar ( text ( " SELECT version_num FROM alembic_version " ) ) == " 004 "
assert connection . execute (
text ( " SELECT name,size_bytes,library_scope FROM visual_assets WHERE id= ' asset ' " )
) . one ( ) == ( " Original " , 123 , " private " )
assert (
connection . scalar ( text ( " SELECT password_hash FROM visual_users WHERE id= ' owner ' " ) )
== " UNCHANGED "
)
inspector = inspect ( connection )
for name in [ " visual_jobs " , " visual_drafts " , " visual_workflow_runs " , " visual_batches " ] :
column = next ( c for c in inspector . get_columns ( name ) if c [ " name " ] == " deleted_at " )
assert column [ " nullable " ]
assert " visual_shared_assets " in inspector . get_table_names ( )
# 历史 004 行为由上方断言保留;当前 metadata 在全部增量升级后校验。
command . upgrade ( config , " head " )
with engine . begin ( ) as connection :
from app . core . schema import schema_preflight
from app . models import Base
assert schema_preflight ( connection , Base . metadata ) . matches
2026-11-11 10:29:01 +08:00
def test_v4_reruns_after_partial_ddl ( tmp_path ) :
2026-11-14 14:07:26 +08:00
config = Config ( str ( Path ( __file__ ) . parents [ 1 ] / " alembic.ini " ) )
path = tmp_path / " partial-v4.sqlite "
config . attributes [ " url " ] = " sqlite+aiosqlite:/// " + str ( path )
command . upgrade ( config , " 003 " )
engine = create_engine ( " sqlite:/// " + str ( path ) )
2026-11-11 10:29:01 +08:00
with engine . begin ( ) as connection :
connection . execute ( text ( " ALTER TABLE visual_jobs ADD COLUMN deleted_at DATETIME NULL " ) )
2026-11-14 14:07:26 +08:00
connection . execute (
text (
" ALTER TABLE visual_assets ADD COLUMN library_scope VARCHAR(16) NOT NULL DEFAULT ' private ' "
)
)
command . upgrade ( config , " 004 " )
2026-11-11 10:29:01 +08:00
with engine . begin ( ) as connection :
2026-11-14 14:07:26 +08:00
assert connection . scalar ( text ( " SELECT version_num FROM alembic_version " ) ) == " 004 "