Repository: pypi/warehouse

Issue:
Database is out of sync with models
Autogenerating a migration from 0abc1b27707bdf9a1ca94ba8392f0086b1eeb492 produces some differences that Alembic is attempting to rectify:

Running:
```
$ docker-compose run web python -m warehouse db revision --autogenerate --message "Sync"
```

Produces:
```python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Sync

Revision ID: 71ccb4f929b0
Revises: 5ea52744d154
Create Date: 2019-07-26 17:00:45.297526
"""

from alembic import op
import sqlalchemy as sa


revision = '71ccb4f929b0'
down_revision = '5ea52744d154'

# Note: It is VERY important to ensure that a migration does not lock for a
#       long period of time and to ensure that each individual migration does
#       not break compatibility with the *previous* version of the code base.
#       This is because the migrations will be ran automatically as part of the
#       deployment process, but while the previous version of the code is still
#       up and running. Thus backwards incompatible changes must be broken up
#       over multiple migrations inside of multiple pull requests in order to
#       phase them in over multiple deploys.

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_unique_constraint('_user_macaroons_description_uc', 'macaroons', ['description', 'user_id'])
    op.drop_index('user_security_keys_label_key', table_name='user_security_keys')
    op.create_unique_constraint('user_security_keys_label_key', 'user_security_keys', ['label'])
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('user_security_keys_label_key', 'user_security_keys', type_='unique')
    op.create_index('user_security_keys_label_key', 'user_security_keys', ['user_id'], unique=False)
    op.drop_constraint('_user_macaroons_description_uc', 'macaroons', type_='unique')
    # ### end Alembic commands ### 
```

Ideally both `upgrade` and `downgrade` would be no-ops here.


Discussion:
Huh, that's interesting -- I could have sworn those indexes/constraints made their way into the migrations.

@di what's your preferred mitigation here? I could modify the respective migrations in a new PR, if that works for you.
We'll either need to change the models or add a new migration that gets them in sync, can't modify the existing migrations as the changes wouldn't be reflected in the production DBs.
Gotcha. We need those indexes and constraints, so I'll add new migrations to fix things.

Resolving patch:
diff --git a/warehouse/migrations/versions/48def930fcfd_webauthn_and_macaroon_constraints.py b/warehouse/migrations/versions/48def930fcfd_webauthn_and_macaroon_constraints.py
new file mode 100644
--- /dev/null
+++ b/warehouse/migrations/versions/48def930fcfd_webauthn_and_macaroon_constraints.py
@@ -0,0 +1,43 @@
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""
+WebAuthn and Macaroon constraints
+
+Revision ID: 48def930fcfd
+Revises: 5ea52744d154
+Create Date: 2019-07-26 17:55:41.802528
+"""
+
+from alembic import op
+
+revision = "48def930fcfd"
+down_revision = "5ea52744d154"
+
+
+def upgrade():
+    op.create_unique_constraint(
+        "_user_macaroons_description_uc", "macaroons", ["description", "user_id"]
+    )
+    op.drop_index("user_security_keys_label_key", table_name="user_security_keys")
+    op.create_unique_constraint(
+        "user_security_keys_label_key", "user_security_keys", ["label"]
+    )
+
+
+def downgrade():
+    op.drop_constraint(
+        "user_security_keys_label_key", "user_security_keys", type_="unique"
+    )
+    op.create_index(
+        "user_security_keys_label_key", "user_security_keys", ["user_id"], unique=False
+    )
+    op.drop_constraint("_user_macaroons_description_uc", "macaroons", type_="unique")


Test patch: