Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Retort is a schema migration tool for SQLAlchemy.

Project description

Retort is a schema migration tool for SQLAlchemy, compares DB schema against table metadata, and updates DB schema according to this.

It depends on the Alembic autogenerate.

Requirements

Retort works with

  • Python 3.3+
  • SQLAlchemy
  • Alembic
  • autopep8

Installation

via pip

$ pip install retort

via setup.py

$ python setup.py install

Basic Usage Examples

Generate config file (retort_config.py)

(venv) tpdn@example:~/retort_example$ retort init
Create retort_conf.py.

Edit config file

# retort_config.py
from model import user

TARGETS = [
    {
        'engine': user.engine, #sqlalchemy engine
        'metadata': user.Base.metadata #sqlalchemy metadata
    },
]
# model/user.py
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('mysql+pymysql://foobar:abcdef@localhost/retort_test_db')
Base = declarative_base()


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    fullname = Column(String(255))
    xyz = Column(String(255))

Apply

(venv) tpdn@example:~/retort_example$ retort apply
====================
url: mysql+pymysql://foobar:abcdef@localhost/retort_test_db
logging_name: None
====================
op.create_table('users',
                sa.Column('id', sa.Integer(), nullable=False),
                sa.Column('name', sa.String(length=255), nullable=True),
                sa.Column('fullname', sa.String(length=255), nullable=True),
                sa.Column('xyz', sa.String(length=255), nullable=True),
                sa.PrimaryKeyConstraint('id')
                )

Do you really want to apply this? [y/n]: y

Applying migration......
====================
url: mysql+pymysql://foobar:abcdef@localhost/retort_test_db
logging_name: None
====================
op.create_table('users',
                sa.Column('id', sa.Integer(), nullable=False),
                sa.Column('name', sa.String(length=255), nullable=True),
                sa.Column('fullname', sa.String(length=255), nullable=True),
                sa.Column('xyz', sa.String(length=255), nullable=True),
                sa.PrimaryKeyConstraint('id')
                )

---> Processing time: 0.0894(sec)
Complete!

Update model(remove xyz column)

# model/user.py
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('mysql+pymysql://foobar:abcdef@localhost/retort_test_db')
Base = declarative_base()


class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    fullname = Column(String(255))
    # xyz = Column(String(255))

Apply with –sql option

(venv) tpdn@example:~/retort_example$ retort apply --sql
====================
url: mysql+pymysql://foobar:abcdef@localhost/retort_test_db
logging_name: None
====================
ALTER TABLE users DROP COLUMN xyz;

Do you really want to apply this? [y/n]: y

Applying migration......
====================
url: mysql+pymysql://foobar:abcdef@localhost/retort_test_db
logging_name: None
====================
ALTER TABLE users DROP COLUMN xyz;

---> Processing time: 0.0745(sec)
Complete!

Commands and Options

retort init

retort apply
  --sql # print sql mode
  --dry-run # dry run (no database update)
  --yes # skip confirmation
  --without-drop # without drop operations (DROP TABLE, DROP COLUMN, DROP INDEX, DROP CONSTRAINT)

retort print_operations
  --sql
  --without-drop

Author

NAKAMORI Ryosuke - https://github.com/tpdn

Licence

BSD License (2-Clause)

Project details


Release history Release notifications

This version

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for retort, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size retort-0.1.0-py3.5.egg (12.3 kB) File type Egg Python version 3.5 Upload date Hashes View hashes
Filename, size retort-0.1.0-py3-none-any.whl (8.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size retort-0.1.0.tar.gz (8.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page