U /Xp*@sddlZddlmZmZmZm Z m Z m Z m Z mZmZmZmZmZddlmZGdddeZGdddeeZGdd d eeZGd d d ee Z d d ZGdddeZdS)N) MySQLMigratorPostgresqlMigratorSchemaMigratorSqliteMigrator OperationSQLEntityClausePostgresqlDatabase operationSqliteDatabase MySQLDatabase)LOGGERcsZeZdZdZefddZdddZeddZd d Z ed d Z ed dZ Z S)rzImplement migrations.csFt|trt|St|tr$t|St|tr6t|Stt| |S)zInitialize migrator by db.) isinstancer rr rr rsuperr from_database)clsdatabase __class__ /migrator.pyrs   zSchemaMigrator.from_databaseTcsfddS)Ncs jdS)Ncascade drop_tablerrmodelrrz+SchemaMigrator.drop_table..rselfrrrrrrszSchemaMigrator.drop_tablecCs.||||g}|js*||||g|S)zChange column.)alter_change_columnnullextend add_not_null)r!table column_namefieldZ operationsrrr change_columnszSchemaMigrator.change_columncCs>|jd}|_|j|}||_ttdt|td|S)Support change columns.T ALTER TABLEz ALTER COLUMN)r#rcompilerfield_definitionr rr)r!r&columnr( field_null field_clauserrrr"'sz"SchemaMigrator.alter_change_columncGstt|f|S)zExecute raw SQL.)r rr!sqlZparamsrrrr2.szSchemaMigrator.sqlcCsn|jd}|_||_|j|}||_tdt|td|g}t|tj r`| | |n||_ t |S)zKeep fieldname unchanged.Tr+z ADD COLUMN)r# db_columnrr,r-rrrpwForeignKeyFieldr$Zget_inline_fk_sqlnamer )r!r&r'r(r/r0partsrrralter_add_column3s zSchemaMigrator.alter_add_column)T) __name__ __module__ __qualname____doc__ classmethodrrr r)r"r2r8 __classcell__rrrrr s   rcs eZdZdZfddZZS)rz Support the migrations in MySQL.cs4tt||||}|jd}|jdtd|Sr*ZTYPE)rrr"nodesinsertrr!r&r'r(clauser0rrrr"Ks z!MySQLMigrator.alter_change_columnr9r:r;r<r"r>rrrrrGsrcs eZdZdZfddZZS)rz%Support the migrations in postgresql.cs4tt||||}|jd}|jdtd|Sr?)rrr"rBrCrrDrrrr"Ws z&PostgresqlMigrator.alter_change_columnrFrrrrrSsrc@s"eZdZdZdddZddZdS) rz!Support the migrations in sqlite.Tcs fddS)z0SQLite doesnt support cascade syntax by default.cs jddS)NFrrrrrrrerz+SqliteMigrator.drop_table..rr rrGrrcszSqliteMigrator.drop_tablecsfdd}|||S)r*cs&j}|}||\}}|SN)rr,r-Z parse_node)r'Z column_defr,rEr2_r(r!rr_changeis  z3SqliteMigrator.alter_change_column.._change)Z_update_column)r!r&r.r(rKrrJrr"gsz"SqliteMigrator.alter_change_columnN)T)r9r:r;r<rr"rrrrr_s rcsfdd}|S)zConvert string to model class.cs4t|tr"||j|f||S||f||SrH)rstrorm)migratorrargskwargsmethodrrwrapperss zget_model..wrapperr)rRrSrrQr get_modelqs rTc@seZdZdZddZddZddZdd Zd d Zd d Z e Z e d(ddZ e Z e ddZeZe ddZeZe ddZeZddZe ddZeZe ddZe ddZe dd Ze d!d"Ze d#d$Ze d%d&Zd'S))MigratorzProvide migrations.cCs:t|tjr|j}||_t|_t|_t |j|_ dS)zInitialize the migrator.N) rr4ZProxyobjrdictrMlistopsrrrN)r!rrrr__init__~s  zMigrator.__init__cCsD|jD]0}t|tr0td|j|j|q|q|dS)zRun operations.z%s %sN) rYrrrinforRrOrunclean)r!Zopnrrrr\s    z Migrator.runcs|jfdddS)zRun python code.cs SrHrrrOfuncrPrrrrz!Migrator.python..N)rYappend)r!r_rOrPrr^rpythonszMigrator.pythoncGs|j|jj|f|dS)zExecure raw SQL.N)rYr`rNr2r1rrrr2sz Migrator.sqlcCs t|_dS)zClean the operations.N)rXrY)r!rrrr]szMigrator.cleancCs*||j|jj<|j|j_|j|j|S)zUCreate model and table in database. >> migrator.create_table(model) )rM_metadb_tablerrYr` create_table)r!rrrrrds zMigrator.create_tableTcCs&|j|jj=|j|j||dS)zaDrop model and table from database. >> migrator.drop_table(model, cascade=True) N)rMrbrcrYr`rNrr rrrrs zMigrator.drop_tablecKsh|D]Z\}}||||j|j|jj|j||j r|j|jj |jj|jfddq|S)zCreate new fields.Tunique) items add_to_classrYr`rNZ add_columnrbrcr3rf add_indexr!rfieldsr6r(rrr add_columnss   zMigrator.add_columnscKsh|D]Z\}}||||j|j|jj|j||j r|j|jj |jj|jfddq|S)zChange fields.Tre) rgrhrYr`rNr)rbrcr3rfrirjrrrchange_columnss    zMigrator.change_columnsc sfdd|jjD}|dd}|D]n}||||jrx|j}||jj |j f}|j |j |jj ||j |j j|jj |j |dq*|S)zRemove fields from model.csg|]}|jkr|qSr)r6).0r(namesrr s z)Migrator.drop_columns..rTr)rbrkvaluespop __del_field__rfrr, index_namercr3rYr`rN drop_indexZ drop_column) r!rrprPrkrr(r,rurror drop_columnss   zMigrator.drop_columnscCsF|j|jt||jt|tjrBt|j|j|jjj |j=dS)zDelete field from model.N) rbZ remove_fieldr6delattrrr4r5Z rel_modelZ related_nameZ reverse_rel)r!rr(rrrrts   zMigrator.__del_field__cCs~|jj|}t|tjr|j}|||||_|_|||t|tjr^|jd|_}|j |j |jj |||S)zRename field in model._id)rbrkrr4r5r3rtr6rhrYr`rN rename_columnrc)r!rZold_namenew_namer(rrrrzs      zMigrator.rename_columncCs@|j|jj=||j_||j|jj<|j|j|jj||S)zRename table in database.)rMrbrcrYr`rN rename_table)r!rr{rrrr|s  zMigrator.rename_tablecOsz|dd}|jj||fg}|D]0}|jj|}t|tjrL|d}||q&|j |j j |jj ||d|S)zCreate indexes.rfFryre) rsrbindexesr`rkgetrr4r5rYrNrirc)r!rcolumnsrPrfcolumns_colr(rrrris   zMigrator.add_indexcsg}D]0}|jj|}t|tjr.|d}||q|jj |jj |}fdd|jj D|j_ |j |j|jj ||S)z Drop indexes.rycs g|]\}}|kr||fqSrr)rnZcolsrIrrrrqsz'Migrator.drop_index..)rbrkr~rr4r5r`rNrr,rurcr}rYrv)r!rrrrr(rurrrrvs  zMigrator.drop_indexcGs<|D]2}|jj|}d|_|j|j|jj|jq|S)z Add not null.F) rbrkr#rYr`rNr%rcr3r!rrpr6r(rrrr%s  zMigrator.add_not_nullcGs<|D]2}|jj|}d|_|j|j|jj|jq|S)zDrop not null.T) rbrkr#rYr`rN drop_not_nullrcr3rrrrr's  zMigrator.drop_not_nullcCs>|jj|}||jj|<|_|j|j|jj|||S)z Add default.) rbrkdefaultsdefaultrYr`rNZ apply_defaultrc)r!rr6rr(rrr add_default0s zMigrator.add_defaultN)T)r9r:r;r<rZr\rar2r]rdZ create_modelrTrZ remove_modelrlZ add_fieldsrmZ change_fieldsrwZ remove_fieldsrtrzZ rename_fieldr|rirvr%rrrrrrrUzsH             rU)Zpeeweer4Zplayhouse.migraterZMqMrZPgMrZScMrZSqMrrrr r r r r Zpeewee_migraterrTobjectrUrrrrs8 :