ڿH d Z ddlmZ ddlZddlZddlZddlT ddlmZ ddlmZ ddlm Z ddlm Z dd lmZ dd lmZ ddlm Z ddlmZ dd lmZ ddlmZ ddlmZ ddlmZ n # e$ r dZY nw xY w G d de Zd Zd Z G d de Z G d de Z G d de Z G d d edd Z G d d e Z G d! d"e Zd# ZdS )$ah Lightweight schema migrations. Example Usage ------------- Instantiate a migrator: # Postgres example: my_db = PostgresqlDatabase(...) migrator = PostgresqlMigrator(my_db) # SQLite example: my_db = SqliteDatabase('my_database.db') migrator = SqliteMigrator(my_db) Then you will use the `migrate` function to run various `Operation`s which are generated by the migrator: migrate( migrator.add_column('some_table', 'column_name', CharField(default='')) ) Migrations are not run inside a transaction, so if you wish the migration to run in a transaction you will need to wrap the call to `migrate` in a transaction block, e.g.: with my_db.transaction(): migrate(...) Supported Operations -------------------- Add new field(s) to an existing model: # Create your field instances. For non-null fields you must specify a # default value. pubdate_field = DateTimeField(null=True) comment_field = TextField(default='') # Run the migration, specifying the database table, field name and field. migrate( migrator.add_column('comment_tbl', 'pub_date', pubdate_field), migrator.add_column('comment_tbl', 'comment', comment_field), ) Renaming a field: # Specify the table, original name of the column, and its new name. migrate( migrator.rename_column('story', 'pub_date', 'publish_date'), migrator.rename_column('story', 'mod_date', 'modified_date'), ) Dropping a field: migrate( migrator.drop_column('story', 'some_old_field'), ) Making a field nullable or not nullable: # Note that when making a field not null that field must not have any # NULL values present. migrate( # Make `pub_date` allow NULL values. migrator.drop_not_null('story', 'pub_date'), # Prevent `modified_date` from containing NULL values. migrator.add_not_null('story', 'modified_date'), ) Renaming a table: migrate( migrator.rename_table('story', 'stories_tbl'), ) Adding an index: # Specify the table, column names, and whether the index should be # UNIQUE or not. migrate( # Create an index on the `pub_date` column. migrator.add_index('story', ('pub_date',), False), # Create a multi-column index on the `pub_date` and `status` fields. migrator.add_index('story', ('pub_date', 'status'), False), # Create a unique index on the category and title fields. migrator.add_index('story', ('category_id', 'title'), True), ) Dropping an index: # Specify the index name. migrate(migrator.drop_index('story', 'story_pub_date_status')) Adding or dropping table constraints: .. code-block:: python # Add a CHECK() constraint to enforce the price cannot be negative. migrate(migrator.add_constraint( 'products', 'price_check', Check('price >= 0'))) # Remove the price check constraint. migrate(migrator.drop_constraint('products', 'price_check')) # Add a UNIQUE constraint on the first and last names. migrate(migrator.add_unique('person', 'first_name', 'last_name')) ) namedtupleN)*) CommaNodeList)EnclosedNodeListEntity) Expression)Node)NodeList)OP) callable_)sort_models)sqlite3)_truncate_constraint_name)CockroachDatabasec * e Zd ZdZd Zd Zd Zd ZdS ) Operationz/Encapsulate a single schema altering operation.c > || _ || _ || _ || _ d S N)migratormethodargskwargs)selfr r r r s q/builddir/build/BUILD/imunify360-venv-2.6.2/opt/imunify360/venv/lib/python3.11/site-packages/playhouse/migrate.py__init__zOperation.__init__ s" c D | j j | d S r )r databaseexecute)r nodes r r zOperation.execute s! &&t,,,,,r c 2 t |t t f r| | d S t |t r| d S t |t t f r|D ]}| | d S d S r ) isinstancer Contextr r runlisttuple_handle_result)r resultitems r r( zOperation._handle_result s ftWo.. *LL * * *JJLLLLL u . . * * *##D)))) * * * *r c | j }d|d<