Skip to content

Rename a number of tables to better fit in current scheme #5016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions warehouse/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ class DisableReason(enum.Enum):

class User(SitemapMixin, db.Model):

__tablename__ = "accounts_user"
__tablename__ = "users"
__table_args__ = (
CheckConstraint("length(username) <= 50", name="packages_valid_name"),
CheckConstraint("length(username) <= 50", name="users_valid_username_length"),
CheckConstraint(
"username ~* '^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$'",
name="accounts_user_valid_username",
name="users_valid_username",
),
)

Expand Down Expand Up @@ -110,16 +110,16 @@ class UnverifyReasons(enum.Enum):

class Email(db.ModelBase):

__tablename__ = "accounts_email"
__tablename__ = "user_emails"
__table_args__ = (
UniqueConstraint("email", name="accounts_email_email_key"),
Index("accounts_email_user_id", "user_id"),
UniqueConstraint("email", name="user_emails_email_key"),
Index("user_emails_user_id", "user_id"),
)

id = Column(Integer, primary_key=True, nullable=False)
user_id = Column(
UUID(as_uuid=True),
ForeignKey("accounts_user.id", deferrable=True, initially="DEFERRED"),
ForeignKey("users.id", deferrable=True, initially="DEFERRED"),
nullable=False,
)
email = Column(String(length=254), nullable=False)
Expand Down
2 changes: 1 addition & 1 deletion warehouse/admin/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class AdminFlag(db.ModelBase):

__tablename__ = "warehouse_admin_flag"
__tablename__ = "admin_flags"

id = Column(Text, primary_key=True, nullable=False)
description = Column(Text, nullable=False)
Expand Down
6 changes: 3 additions & 3 deletions warehouse/admin/squats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

class Squat(db.ModelBase):

__tablename__ = "warehouse_admin_squat"
__tablename__ = "admin_squats"

id = Column(Integer, primary_key=True, nullable=False)
created = Column(
DateTime(timezone=False), nullable=False, server_default=sql.func.now()
)
squatter_id = Column(
ForeignKey("packages.id", onupdate="CASCADE", ondelete="CASCADE"),
ForeignKey("projects.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
squattee_id = Column(
ForeignKey("packages.id", onupdate="CASCADE", ondelete="CASCADE"),
ForeignKey("projects.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
squatter = orm.relationship("Project", foreign_keys=[squatter_id], lazy=False)
Expand Down
161 changes: 161 additions & 0 deletions warehouse/migrations/versions/06bfbc92f67d_rename_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# 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.
"""
Rename tables

Revision ID: 06bfbc92f67d
Revises: eeb23d9b4d00
Create Date: 2018-11-06 04:36:58.531272
"""

from alembic import op


revision = "06bfbc92f67d"
down_revision = "e612a92c1017"


def upgrade():
# The new verbiage in Warehouse is to call these things packages, but this table
# name was inherited from legacy PyPI.
op.rename_table("packages", "projects")
op.execute("ALTER TABLE projects RENAME CONSTRAINT packages_pkey TO projects_pkey")
op.execute(
"""
ALTER TABLE projects
RENAME CONSTRAINT packages_valid_name
TO projects_valid_name
"""
)
op.execute(
""" CREATE OR REPLACE FUNCTION maintain_project_last_serial()
RETURNS TRIGGER AS $$
DECLARE
targeted_name text;
BEGIN
IF TG_OP = 'INSERT' THEN
targeted_name := NEW.name;
ELSEIF TG_OP = 'UPDATE' THEN
targeted_name := NEW.name;
ELSIF TG_OP = 'DELETE' THEN
targeted_name := OLD.name;
END IF;

UPDATE projects
SET last_serial = j.last_serial
FROM (
SELECT max(id) as last_serial
FROM journals
WHERE journals.name = targeted_name
) as j
WHERE projects.name = targeted_name;

RETURN NULL;
END;
$$ LANGUAGE plpgsql;
"""
)
op.execute(
"UPDATE row_counts SET table_name = 'projects' WHERE table_name = 'packages'"
)

# We took the name of these tables from a failed Django port, the new names are
# cleaner and fit the overall "theme" of our table names better.
op.rename_table("accounts_user", "users")
op.execute("ALTER TABLE users RENAME CONSTRAINT accounts_user_pkey TO users_pkey")
op.execute(
"""
ALTER TABLE users
RENAME CONSTRAINT accounts_user_username_key
TO users_username_key
"""
)
op.execute(
"""
ALTER TABLE users
RENAME CONSTRAINT accounts_user_valid_username
TO users_valid_username
"""
)
op.execute(
"""
ALTER TABLE users
RENAME CONSTRAINT packages_valid_name
TO users_valid_username_length
"""
)
op.execute(
"UPDATE row_counts SET table_name = 'users' WHERE table_name = 'accounts_user'"
)

op.rename_table("accounts_email", "user_emails")
op.execute(
"""
ALTER TABLE user_emails
RENAME CONSTRAINT accounts_email_pkey
TO user_emails_pkey
"""
)
op.execute(
"""
ALTER TABLE user_emails
RENAME CONSTRAINT accounts_email_email_key
TO user_emails_email_key
"""
)
op.execute(
"""
ALTER TABLE user_emails
RENAME CONSTRAINT accounts_email_user_id_fkey
TO user_emails_user_id_fkey
"""
)
op.execute("ALTER INDEX accounts_email_user_id RENAME TO user_emails_user_id")

# While the admin prefix on these tables is useful to let us know they are specific
# to the admins, the warehouse prefix is not. All of these tables in this database
# are for Warehouse.
op.rename_table("warehouse_admin_flag", "admin_flags")
op.execute(
"""
ALTER TABLE admin_flags
RENAME CONSTRAINT warehouse_admin_flag_pkey
TO admin_flags_pkey
"""
)

op.rename_table("warehouse_admin_squat", "admin_squats")
op.execute(
"""
ALTER TABLE admin_squats
RENAME CONSTRAINT warehouse_admin_squat_pkey
TO admin_squats_pkey
"""
)
op.execute(
"""
ALTER TABLE admin_squats
RENAME CONSTRAINT warehouse_admin_squat_squattee_id_fkey
TO admin_squats_squattee_id_fkey
"""
)
op.execute(
"""
ALTER TABLE admin_squats
RENAME CONSTRAINT warehouse_admin_squat_squatter_id_fkey
TO admin_squats_squatter_id_fkey
"""
)


def downgrade():
raise RuntimeError("Order No. 227 - Ни шагу назад!")
17 changes: 8 additions & 9 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ class Role(db.Model):

role_name = Column(Text)
user_id = Column(
ForeignKey("accounts_user.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE"), nullable=False
)
project_id = Column(
ForeignKey("packages.id", onupdate="CASCADE", ondelete="CASCADE"),
ForeignKey("projects.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)

Expand Down Expand Up @@ -97,11 +96,11 @@ def __getitem__(self, project):

class Project(SitemapMixin, db.Model):

__tablename__ = "packages"
__tablename__ = "projects"
__table_args__ = (
CheckConstraint(
"name ~* '^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$'::text",
name="packages_valid_name",
name="projects_valid_name",
),
)

Expand Down Expand Up @@ -267,7 +266,7 @@ def __table_args__(cls): # noqa
__name__ = dotted_navigator("version")

project_id = Column(
ForeignKey("packages.id", onupdate="CASCADE", ondelete="CASCADE"),
ForeignKey("projects.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
)
version = Column(Text, nullable=False)
Expand Down Expand Up @@ -349,7 +348,7 @@ def __table_args__(cls): # noqa
project_urls = association_proxy("_project_urls", "specifier")

uploader_id = Column(
ForeignKey("accounts_user.id", onupdate="CASCADE", ondelete="SET NULL"),
ForeignKey("users.id", onupdate="CASCADE", ondelete="SET NULL"),
nullable=True,
index=True,
)
Expand Down Expand Up @@ -509,7 +508,7 @@ def __table_args__(cls): # noqa
DateTime(timezone=False), nullable=False, server_default=sql.func.now()
)
_submitted_by = Column(
"submitted_by", CIText, ForeignKey("accounts_user.username", onupdate="CASCADE")
"submitted_by", CIText, ForeignKey("users.username", onupdate="CASCADE")
)
submitted_by = orm.relationship(User)
submitted_from = Column(Text)
Expand All @@ -532,7 +531,7 @@ class BlacklistedProject(db.Model):
)
name = Column(Text, unique=True, nullable=False)
_blacklisted_by = Column(
"blacklisted_by", UUID(as_uuid=True), ForeignKey("accounts_user.id"), index=True
"blacklisted_by", UUID(as_uuid=True), ForeignKey("users.id"), index=True
)
blacklisted_by = orm.relationship(User)
comment = Column(Text, nullable=False, server_default="")