File: //opt/alt/python38/lib64/python3.8/site-packages/playhouse/__pycache__/migrate.cpython-38.opt-1.pyc
U
S��W�] � @ s� d Z ddlmZ ddlZddlZddlT ddlmZ ddlmZ ddlmZ ddlm Z dd lm
Z
dd
lmZ G dd� de�Z
d
d� ZG dd� de�ZG dd� de�ZdZG dd� dede��ZG dd� de�ZG dd� de�Zdd� ZdS )a�
Lightweight schema migrations.
NOTE: Currently tested with SQLite and Postgresql. MySQL may be missing some
features.
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'))
� )�
namedtupleN)�*)�CommaClause)�EnclosedClause��Entity)�
Expression)�Node)�OPc @ s8 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� ZdS )
� Operationz/Encapsulate a single schema altering operation.c O s || _ || _|| _|| _d S �N)�migrator�method�args�kwargs)�selfr
r r r � r �D/opt/alt/python38/lib64/python3.8/site-packages/playhouse/migrate.py�__init__u s zOperation.__init__c C s | j j�� }|�|�S r )r
�database�compilerZ
parse_node)r �noder r r r �_parse_node{ s zOperation._parse_nodec C s"