U kÃf9ã@sîddlmZddlZddlZddlmZddlmZddlmZddlmZdd lm Z dd l m Z dd l mZdd l mZe e¡ZdZGd d„dƒZdd„Zdd„Zdd„Zdd„Zdd„Zejdd„ƒZdd„Zejdd„ƒZejdd „ƒZd!d"„Zd#d$„Z d%d&„Z!ejd'd(„ƒZ"ejd)d*„ƒZ#d+d,„Z$e %e$¡d-d.„ƒZ&ejd/d0„ƒZ'ejd1d2„ƒZ(ejd3d4„ƒZ)ejd5d6„ƒZ*d7d8„Z+ejd9d:„ƒZ,ejd;d<„ƒZ-ejd=d>„ƒZ.ejd?d@„ƒZ/ejdAdB„ƒZ0ejddCdDœdEdF„ƒZ1ejdGdH„ƒZ2dS)Ié)Ú annotationsNé)Úconfig)Úengines)Úutilé)Úexc)Úinspect)Úurl)Úddl©Úschemac@s>eZdZd dd„Zedd„ƒZedd„ƒZdd „Zd d „ZdS) ÚregisterNcCsi|_||_dS©N)ÚfnsÚ decorator)Úselfr©rúL/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/testing/provision.pyÚ__init__szregister.__init__cCstƒ d¡|ƒS©NÚ*©rÚfor_db)ÚclsÚfnrrrÚinit"sz register.initcCst|ƒ d¡Srr)rrrrrÚinit_decorator&szregister.init_decoratorcs‡‡fdd„}|S)Ncs(ˆjrˆ |¡}ˆD]}|ˆj|<qˆSr)rr)rZdbname©ÚdbnamesrrrÚdecorate+s   z!register.for_db..decorater)rrr rrrr*szregister.for_dbcOsrt|tƒrt |¡}nt|tjƒr(|}n|jj}| ¡}||jkrX|j||f|ž|ŽS|jd|f|ž|ŽSdSr) Ú isinstanceÚstrÚsa_urlÚmake_urlÚURLÚdbr Úget_backend_namer)rÚcfgÚargÚkwr ÚbackendrrrÚ__call__4s    zregister.__call__)N) Ú__name__Ú __module__Ú __qualname__rÚ classmethodrrrr,rrrrrs    rcCs0tƒD]$}t d||jj¡t||j|ƒqdS)NzCREATE database %s, URI %r)Ú_configs_for_db_operationÚlogÚinfor&r Ú create_db©Úfollower_identr(rrrÚcreate_follower_dbBs r7cCsŽt |¡ ¡}| ¡|r$t||ƒ}i}t|||ƒd|d<t ||¡}t|||ƒ|  ¡  ¡t j   ||||¡}|r||t _|rŠt||ƒ|S)NÚglobalÚscope)r#r$Ú get_dialectÚload_provisioningÚfollower_url_from_mainÚupdate_db_optsrZtesting_engineÚpost_configure_engineÚconnectÚcloserÚConfigrÚidentÚconfigure_follower)Údb_urlÚoptionsZ file_configr6ÚdialectÚdb_optsÚengr(rrrÚ setup_configHs       rIcCs0tƒD]$}t d||jj¡t||j|ƒqdS)NzDROP database %s, URI %r)r1r2r3r&r Údrop_dbr5rrrÚdrop_follower_dbes rKc csÈtƒ}t t¡}dd„dd„|DƒDƒ}|D] \}}| ¡}||j |¡q,i}|D]l\}}|j} | ¡| |krt|ƒ || ¡|| <} n|| } t|| ƒD]} | |kr°q¢| | ¡| Vq¢qVdS)a&Generate a set of URLs to test given configured URLs plus additional driver names. Given:: --dburi postgresql://db1 --dburi postgresql://db2 --dburi postgresql://db2 --dbdriver=psycopg2 --dbdriver=asyncpg?async_fallback=true Noting that the default postgresql driver is psycopg2, the output would be:: postgresql+psycopg2://db1 postgresql+asyncpg://db1 postgresql+psycopg2://db2 postgresql+psycopg2://db3 That is, for the driver in a --dburi, we want to keep that and use that driver for each URL it's part of . For a driver that is only in --dbdrivers, we want to use it just once for one of the URLs. for a driver that is both coming from --dburi as well as --dbdrivers, we want to keep it in that dburi. Driver specific query options can be specified by added them to the driver name. For example, to enable the async fallback option for asyncpg:: --dburi postgresql://db1 --dbdriver=asyncpg?async_fallback=true cSsg|]}|| ¡f‘qSr)r:)Ú.0Úurl_objrrrÚ sÿz$generate_db_urls..cSsg|]}t |¡‘qSr)r#r$)rLrDrrrrN’sN) ÚsetÚ collectionsÚ defaultdictÚget_driver_nameÚnameÚaddr;Ú differenceÚ_generate_driver_urls) Zdb_urlsÚ extra_driversÚurlsZ!backend_to_driver_we_already_haveZurls_plus_dialectsrMrFZ driver_nameZbackend_to_driver_we_needr+Zextra_per_backendZ driver_urlrrrÚgenerate_db_urlsks0!  þ  ÿþ rYccsx| ¡}| |¡t||dƒ}|Vt|ƒD]F}d|krJ| dd¡\}}n|}d}t|||ƒ}|r,| |¡|Vq,dS)NÚú?r)rRÚdiscardÚgenerate_driver_urlÚlistÚsplitÚremove)r rWZ main_driverÚdrvZ driver_onlyÚ query_strÚnew_urlrrrrV¯s     rVcCsX| ¡}|jd||fd}|r*| |¡}z | ¡Wntjk rNYdSX|SdS)Nz%s+%s)Z drivername)r'rOZupdate_query_stringr:rZNoSuchModuleError)r Zdriverrbr+rcrrrr]Ås ÿ  r]ccsˆtƒ}tj ¡D]}|j ¡qtj ¡D]>}|jj}| ¡}||j|j |j f}||kr*|V|  |¡q*tj ¡D]}|j ¡qtdSr) rOrrAZ all_configsr&Zdisposer r'ÚusernameÚhostÚdatabaserT)Úhostsr(r r+Z host_confrrrr1×s  r1cCsdSrr©r(rHrrrÚ"drop_all_schema_objects_pre_tablesêsricCsdSrrrhrrrÚ#drop_all_schema_objects_post_tablesïsrjc Cst||ƒt||ƒtjjjr(t||ƒt|ƒ}d}tjj  |¡rR||j |j f7}t j |||dt||ƒtjj  |¡rþ| ¡v}| ¡D]}| t t |¡¡¡qŒtjj  |¡rô|j |j fD]0}|j|dD]}| t tj||d¡¡qÒqÂW5QRXdS)Nr)Úconsider_schemasr )riÚ drop_viewsrÚ requirementsZmaterialized_viewsÚenabledÚdrop_materialized_viewsr ÚschemasÚenabled_for_configÚ test_schemaZ test_schema_2rZdrop_all_tablesrjÚ sequencesÚbeginZget_sequence_namesÚexecuter Z DropSequencer ÚSequence)r(rHÚ inspectorrkÚconnÚseqÚ schema_namerrrÚdrop_all_schema_objectsôs0       ÿ  ÿÿr{c CsÞt|ƒ}z | ¡}Wntk r(Ynaóz'_adapt_update_db_opts..)rZinspect_getfullargspecÚlenÚargs)rZinsprr„rÚ_adapt_update_db_opts\s r‰cCsdS)zCSet database options (db_opts) for a test database that we created.Nr)rDrGrErrrr=dsr=cCsdS)z‰Perform extra steps after configuring an engine for testing. (For the internal dialects, currently only used by sqlite, oracle) Nr)r Úenginer6rrrr>isr>cCst |¡}|j|dS)zúCreate a connection URL for a dynamically-created test database. :param url: the connection URL specified when the test run was invoked :param ident: the pytest-xdist "worker identifier" to be used as the database name )rf)r#r$rO©r rBrrrr<qs r<cCsdS)z@Create dialect-specific config settings for a follower database.Nr)r(rBrrrrC}srCcCsdS)a[Remove databases that were created during the test process, after the process has ended. This is an optional step that is invoked for certain backends that do not reliably release locks on the database as long as a process is still in use. For the internal dialects, this is currently only necessary for mssql and oracle. Nrr‹rrrÚ run_reap_dbsƒsrŒc CsÚt d¡t t¡}t t¡}i}t|ƒz}|D]n}| ¡}| d¡\}}t  |¡}||krt|  ¡||<||  ¡|  ¡|j f} ||  |¡||  |¡q0W5QRX|D]&} t|| ƒd} || } t| | ƒq®dS)NzReaping databases...ú r)r2r3rPrQrOÚopenÚstripr_r#r$r:r;r'rerTr^rŒ) Z idents_filerXZidentsZdialectsÚfile_ÚlineZdb_namerDrMZurl_keyr rBrrrÚreap_dbss&       r’cCstd|jfƒ‚dS)aOSpecify keyword arguments for creating a temporary Table. Dialect-specific implementations of this method will return the kwargs that are passed to the Table method when creating a temporary table for testing, e.g., in the define_temp_tables method of the ComponentReflectionTest class in suite/test_reflection.py z.no temp table keyword args routine for cfg: %sNr€rhrrrÚtemp_table_keyword_args¨s  ÿr“cCsdSrr)rÚ connectionrrrÚprepare_for_drop_tables¶sr•cCsdSrr)rr&ZtestclsrrrÚ stop_test_class_outside_fixtures»sr–cCs|S)a¸Specify table name for creating a temporary Table. Dialect-specific implementations of this method will return the name to use when creating a temporary table for testing, e.g., in the define_temp_tables method of the ComponentReflectionTest class in suite/test_reflection.py Default to just the base name since that's what most dialects will use. The mssql dialect's implementation will need a "#" prepended. r)r(rHÚ base_namerrrÚget_temp_table_nameÀs r˜cCstd|jjfƒ‚dS)Nz9backend does not implement a schema name set function: %s©r|r&r )r(Zdbapi_connectionrzrrrÚ set_default_schema_on_connectionÏs ÿÿršF)Ú set_lambdaÚsort_by_parameter_ordercCstd|jj›ƒ‚dS)a˜return the backends insert..on conflict / on dupe etc. construct. while we should add a backend-neutral upsert construct as well, such as insert().upsert(), it's important that we continue to test the backend-specific insert() constructs since if we do implement insert().upsert(), that would be using a different codepath for the things we need to test like insertmanyvalues, etc. z3backend does not include an upsert implementation: Nr™)r(ÚtableZ returningr›rœrrrÚupsert×s  ÿržcCs|S)zƒNormalize sequence parameters for dialect that don't start with 1 by default. The default implementation does nothing r)r(ÚsequencerrrÚnormalize_sequenceésr )3Ú __future__rrPÚloggingrZrrrrr rŠr r#Úsqlr r Ú getLoggerr-r2ZFOLLOWER_IDENTrr7rIrKrYrVrr]r1rirjr{rlror4rJr‰rr=r>r<rCrŒr’r“r•r–r˜ršržr rrrrÚ sx          %D   !!            ÿ