diff --git a/warehouse/accounts/models.py b/warehouse/accounts/models.py index 8fb2badf9719..5d27caf80207 100644 --- a/warehouse/accounts/models.py +++ b/warehouse/accounts/models.py @@ -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", ), ) @@ -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) diff --git a/warehouse/admin/flags.py b/warehouse/admin/flags.py index ae74b79bce51..7b01a5159c06 100644 --- a/warehouse/admin/flags.py +++ b/warehouse/admin/flags.py @@ -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) diff --git a/warehouse/admin/squats.py b/warehouse/admin/squats.py index b1cada82cd57..ccb1d68562f4 100644 --- a/warehouse/admin/squats.py +++ b/warehouse/admin/squats.py @@ -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) diff --git a/warehouse/migrations/versions/06bfbc92f67d_rename_tables.py b/warehouse/migrations/versions/06bfbc92f67d_rename_tables.py new file mode 100644 index 000000000000..24f4eba3f453 --- /dev/null +++ b/warehouse/migrations/versions/06bfbc92f67d_rename_tables.py @@ -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 - Ни шагу назад!") diff --git a/warehouse/packaging/models.py b/warehouse/packaging/models.py index 4f140fa50a73..66e47861d796 100644 --- a/warehouse/packaging/models.py +++ b/warehouse/packaging/models.py @@ -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, ) @@ -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", ), ) @@ -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) @@ -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, ) @@ -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) @@ -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="")