Skip to content

Commit

Permalink
Merge ab777b1 into c1b616a
Browse files Browse the repository at this point in the history
  • Loading branch information
shosca committed Apr 16, 2019
2 parents c1b616a + ab777b1 commit 30953ec
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
99 changes: 95 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@ This will create a polls app with standard django app layout:
3 directories, 12 files
And lets add our ``polls`` app in ``INSTALLED_APPS`` in ``mysite/settings.py``:
And lets add our ``polls`` app and ``django_sorcery`` in ``INSTALLED_APPS`` in ``mysite/settings.py``:

.. code:: python
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_sorcery',
'polls.apps.PollsConfig',
]
Now we're going to make a twist and start building our app with ``sqlalchemy``. Lets define our models in
Expand Down Expand Up @@ -97,8 +98,98 @@ Now we're going to make a twist and start building our app with ``sqlalchemy``.
question = db.ManyToOne(Question, backref=db.backref("choices", cascade="all, delete-orphan"))
db.configure_mappers()
db.create_all()
Now that we have some models, lets create a migration using ``alembic`` integration:

.. code:: console
$ python manage.py sorcery revision -m "Add question and poll models" polls
Generating ./polls/migrations/3983fc419e10_add_question_and_poll_models.py ... done
Let's take a look at the generated migration file ``./polls/migrations/3983fc419e10_add_question_and_poll_models.py``:

.. code:: python
"""
Add question and poll models
Revision ID: 3983fc419e10
Revises:
Create Date: 2019-04-16 20:57:48.154179
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '3983fc419e10'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('question',
sa.Column('pk', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('question_text', sa.String(length=200), nullable=True),
sa.Column('pub_date', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('pk')
)
op.create_table('choice',
sa.Column('pk', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('choice_text', sa.String(length=200), nullable=True),
sa.Column('votes', sa.Integer(), nullable=True),
sa.Column('question_pk', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['question_pk'], ['question.pk'], ),
sa.PrimaryKeyConstraint('pk')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('choice')
op.drop_table('question')
# ### end Alembic commands ###
Let's take a look at generated sql:

.. code:: console
$ python manage.py sorcery upgrade --sql polls
CREATE TABLE alembic_version_polls (
version_num VARCHAR(32) NOT NULL,
CONSTRAINT alembic_version_polls_pkc PRIMARY KEY (version_num)
);
-- Running upgrade -> d7d86e07cc8e
CREATE TABLE question (
pk INTEGER NOT NULL,
question_text VARCHAR(200),
pub_date DATETIME,
PRIMARY KEY (pk)
);
CREATE TABLE choice (
pk INTEGER NOT NULL,
choice_text VARCHAR(200),
votes INTEGER,
question_pk INTEGER,
PRIMARY KEY (pk),
FOREIGN KEY(question_pk) REFERENCES question (pk)
);
INSERT INTO alembic_version_polls (version_num) VALUES ('d7d86e07cc8e');
Let's bring our db up to date:

.. code:: console
$ python manage.py sorcery upgrade
Running migrations for polls on database default
Right now, we have enough to hop in django shell:

Expand Down
3 changes: 3 additions & 0 deletions django_sorcery/management/alembic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import alembic.config
import six

from sqlalchemy.orm import configure_mappers

from django.apps import apps
from django.core.management.base import BaseCommand, CommandError
from django.utils.functional import cached_property
Expand Down Expand Up @@ -127,6 +129,7 @@ def run_env(self, context, appconfig):
"""
Executes an alembic context, just like the env.py file of alembic
"""
configure_mappers()
try:
if context.is_offline_mode():
self.run_migrations_offline(context, appconfig)
Expand Down

0 comments on commit 30953ec

Please sign in to comment.