From d09368a2c552b557efc57bd23994f6316e925c57 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Thu, 15 Jan 2026 18:25:13 +0100 Subject: [PATCH 1/4] Rename variables and fix code : Rename files and indent SQL --- ...ema.sql => V1__create_tables_accounts_articles_feedback.sql} | 0 migrations/V3__rename_role_colum_in_account.sql | 1 - migrations/V3__rename_role_column_in_account.sql | 2 ++ migrations/V4__add_password_in_accounts.sql | 2 +- 4 files changed, 3 insertions(+), 2 deletions(-) rename migrations/{V1__init_schema.sql => V1__create_tables_accounts_articles_feedback.sql} (100%) delete mode 100644 migrations/V3__rename_role_colum_in_account.sql create mode 100644 migrations/V3__rename_role_column_in_account.sql diff --git a/migrations/V1__init_schema.sql b/migrations/V1__create_tables_accounts_articles_feedback.sql similarity index 100% rename from migrations/V1__init_schema.sql rename to migrations/V1__create_tables_accounts_articles_feedback.sql diff --git a/migrations/V3__rename_role_colum_in_account.sql b/migrations/V3__rename_role_colum_in_account.sql deleted file mode 100644 index c1f4d1f..0000000 --- a/migrations/V3__rename_role_colum_in_account.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE Accounts RENAME COLUMN role TO account_role; diff --git a/migrations/V3__rename_role_column_in_account.sql b/migrations/V3__rename_role_column_in_account.sql new file mode 100644 index 0000000..22a3292 --- /dev/null +++ b/migrations/V3__rename_role_column_in_account.sql @@ -0,0 +1,2 @@ +ALTER TABLE accounts + RENAME COLUMN role TO account_role; diff --git a/migrations/V4__add_password_in_accounts.sql b/migrations/V4__add_password_in_accounts.sql index 8e6f8e8..018921b 100644 --- a/migrations/V4__add_password_in_accounts.sql +++ b/migrations/V4__add_password_in_accounts.sql @@ -1,2 +1,2 @@ ALTER TABLE accounts -ADD COLUMN account_password TEXT NOT NULL; + ADD COLUMN account_password TEXT NOT NULL; From 5ff6a352b8bda63a44642ad17c4b7f4c15891264 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Thu, 15 Jan 2026 18:29:52 +0100 Subject: [PATCH 2/4] Rename variables and fix code : Rename columns, update foreign keys, and convert feedback to comments --- migrations/V5__rename_and_update_schema.sql | 76 +++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 migrations/V5__rename_and_update_schema.sql diff --git a/migrations/V5__rename_and_update_schema.sql b/migrations/V5__rename_and_update_schema.sql new file mode 100644 index 0000000..fe2163d --- /dev/null +++ b/migrations/V5__rename_and_update_schema.sql @@ -0,0 +1,76 @@ +ALTER TABLE accounts + RENAME COLUMN username TO account_username; + +ALTER TABLE accounts + RENAME COLUMN email TO account_email; + +ALTER TABLE accounts + RENAME COLUMN created_at TO account_created_at; + +ALTER TABLE articles + RENAME COLUMN writer_id TO article_author_id; + +ALTER TABLE articles + RENAME COLUMN title TO article_title; + +ALTER TABLE articles + RENAME COLUMN content TO article_content; + +ALTER TABLE articles + RENAME COLUMN published_at TO article_published_at; + +ALTER TABLE articles + DROP CONSTRAINT articles_writer_id_fkey; + +ALTER TABLE articles + ADD CONSTRAINT articles_author_fk + FOREIGN KEY (article_author_id) + REFERENCES accounts(account_id) + ON DELETE CASCADE; + +ALTER TABLE feedback + RENAME TO comments; + +ALTER TABLE comments + RENAME COLUMN feedback_id TO comment_id; + +ALTER TABLE comments + RENAME COLUMN article_ref TO comment_article_id; + +ALTER TABLE comments + RENAME COLUMN commenter_id TO comment_written_account_id; + +ALTER TABLE comments + RENAME COLUMN reply_to TO comment_reply_to; + +ALTER TABLE comments + RENAME COLUMN message TO comment_content; + +ALTER TABLE comments + RENAME COLUMN posted_at TO comment_posted_at; + +ALTER TABLE comments + DROP CONSTRAINT feedback_article_ref_fkey; + +ALTER TABLE comments + ADD CONSTRAINT comments_article_fk + FOREIGN KEY (comment_article_id) + REFERENCES articles(article_id) + ON DELETE CASCADE; + +ALTER TABLE comments + DROP CONSTRAINT feedback_commenter_id_fkey; + +ALTER TABLE comments + ADD CONSTRAINT comments_author_fk + FOREIGN KEY (comment_written_account_id) + REFERENCES accounts(account_id) + ON DELETE CASCADE; + +ALTER TABLE comments + DROP CONSTRAINT feedback_reply_to_fkey; + +ALTER TABLE comments + ADD CONSTRAINT comments_reply_fk + FOREIGN KEY (comment_reply_to) + REFERENCES comments(comment_id); From 58c25b71fb879e3ac42621220738f3a1f6703133 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Thu, 15 Jan 2026 18:30:53 +0100 Subject: [PATCH 3/4] Rename variables and fix code : Update models to match renamed columns, new relationships, and updated schema --- app/models.py | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/app/models.py b/app/models.py index fbf7c7d..1f691bf 100644 --- a/app/models.py +++ b/app/models.py @@ -1,45 +1,48 @@ -from sqlalchemy import TIMESTAMP, Column, ForeignKey, Integer, Text +from sqlalchemy import TIMESTAMP, CheckConstraint, Column, ForeignKey, Integer, Text, func from sqlalchemy.orm import declarative_base, relationship Base = declarative_base() class Account(Base): __tablename__ = "accounts" + __table_args__ = ( + CheckConstraint("account_role IN ('admin', 'author', 'user')", name="accounts_role_check"), + ) account_id = Column("account_id", Integer, primary_key=True, autoincrement=True) - username = Column("username", Text, unique=True, nullable=False) + account_username = Column("account_username", Text, unique=True, nullable=False) account_password = Column("account_password", Text, nullable=False) - email = Column("email", Text) + account_email = Column("account_email", Text) account_role = Column("account_role", Text, nullable=False) - created_at = Column("created_at", TIMESTAMP) + account_created_at = Column("account_created_at", TIMESTAMP, server_default=func.now(), nullable=False) - articles = relationship("Article", back_populates="writer", cascade="all, delete-orphan") - feedbacks = relationship("Feedback", back_populates="commenter", cascade="all, delete-orphan") + articles = relationship("Article", back_populates="article_author", cascade="all, delete-orphan") + comments = relationship("Comment", back_populates="comment_author", cascade="all, delete-orphan") class Article(Base): __tablename__ = "articles" article_id = Column("article_id", Integer, primary_key=True, autoincrement=True) - writer_id = Column("writer_id", Integer, ForeignKey("accounts.account_id"), nullable=False) - title = Column("title", Text, nullable=False) - content = Column("content", Text, nullable=False) - published_at = Column("published_at", TIMESTAMP) + article_author_id = Column("article_author_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False) + article_title = Column("article_title", Text, nullable=False) + article_content = Column("article_content", Text, nullable=False) + article_published_at = Column("article_published_at", TIMESTAMP, server_default=func.now(), nullable=False) - writer = relationship("Account", back_populates="articles") - feedbacks = relationship("Feedback", back_populates="article", cascade="all, delete-orphan") + article_author = relationship("Account", back_populates="articles") + article_comments = relationship("Comment", back_populates="comment_article", cascade="all, delete-orphan") -class Feedback(Base): - __tablename__ = "feedback" +class Comment(Base): + __tablename__ = "comments" - feedback_id = Column("feedback_id", Integer, primary_key=True, autoincrement=True) - article_ref = Column("article_ref", Integer, ForeignKey("articles.article_id"), nullable=False) - commenter_id = Column("commenter_id", Integer, ForeignKey("accounts.account_id"), nullable=False) - reply_to = Column("reply_to", Integer, ForeignKey("feedback.feedback_id")) - message = Column("message", Text, nullable=False) - posted_at = Column("posted_at", TIMESTAMP) + comment_id = Column("comment_id", Integer, primary_key=True, autoincrement=True) + comment_article_id = Column("comment_article_id", Integer, ForeignKey("articles.article_id", ondelete="CASCADE"), nullable=False) + comment_written_account_id = Column("comment_written_account_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False) + comment_reply_to = Column("comment_reply_to", Integer, ForeignKey("comments.comment_id"), nullable=True) + comment_content = Column("comment_content", Text, nullable=False) + comment_posted_at = Column("comment_posted_at", TIMESTAMP, server_default=func.now(), nullable=False) - article = relationship("Article", back_populates="feedbacks") - commenter = relationship("Account", back_populates="feedbacks") - replies = relationship("Feedback", remote_side=[feedback_id]) + comment_article = relationship("Article", back_populates="article_comments") + comment_author = relationship("Account", back_populates="comments") + replies = relationship("Comment", backref="parent", remote_side=[comment_id]) From 4557aa41176205b21899351f274de75cee005302 Mon Sep 17 00:00:00 2001 From: raydeveloppeur-admin Date: Thu, 15 Jan 2026 18:31:26 +0100 Subject: [PATCH 4/4] Rename variables and fix code : Update test suite to reflect renamed fields, new relationships, and updated schema --- tests/test_models.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/test_models.py b/tests/test_models.py index 25be17d..c4a02ab 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,41 +1,41 @@ -from app.models import Account, Article, Feedback +from app.models import Account, Article, Comment def test_create_account(db_session): - account = Account(username="pytest_user_account", account_password="123456789", email="test_account@example.com", account_role="user") + account = Account(account_username="pytest_user_account", account_password="123456789", account_email="test_account@example.com", account_role="user") db_session.add(account) db_session.commit() - result = db_session.query(Account).filter_by(username="pytest_user_account").first() + result = db_session.query(Account).filter_by(account_username="pytest_user_account").first() assert result is not None assert result.account_password is not None assert result.account_role == "user" def test_create_article(db_session): - author = Account(username="pytest_author_article", account_password="123456789", email="author_article@test.com", account_role="author") + author = Account(account_username="pytest_author_article", account_password="123456789", account_email="author_article@test.com", account_role="author") db_session.add(author) db_session.commit() - article = Article(writer_id=author.account_id, title="Titre article", content="Contenu article") + article = Article(article_author_id=author.account_id, article_title="Titre article", article_content="Contenu article") db_session.add(article) db_session.commit() - result = db_session.query(Article).filter_by(title="Titre article").first() + result = db_session.query(Article).filter_by(article_title="Titre article").first() assert result is not None - assert result.writer.username == "pytest_author_article" - assert result.writer.account_password is not None + assert result.article_author.account_username == "pytest_author_article" + assert result.article_author.account_password is not None -def test_create_feedback(db_session): - author = Account(username="pytest_author_feedback", account_password="123456789", email="author_feedback@test.com", account_role="author") - user = Account(username="pytest_user_feedback", account_password="123456789", email="user_feedback@test.com", account_role="user") +def test_create_comment(db_session): + author = Account(account_username="pytest_author_comment", account_password="123456789", account_email="author_comment@test.com", account_role="author") + user = Account(account_username="pytest_user_comment", account_password="123456789", account_email="user_comment@test.com", account_role="user") db_session.add_all([author, user]) db_session.commit() - article = Article(writer_id=author.account_id, title="Titre feedback", content="Contenu feedback") + article = Article(article_author_id=author.account_id, article_title="Titre comment", article_content="Contenu comment") db_session.add(article) db_session.commit() - feedback = Feedback(article_ref=article.article_id, commenter_id=user.account_id, message="Bravo !") - db_session.add(feedback) + comment = Comment(comment_article_id=article.article_id, comment_written_account_id=user.account_id, comment_content="Bravo !") + db_session.add(comment) db_session.commit() - result = db_session.query(Feedback).filter_by(message="Bravo !").first() + result = db_session.query(Comment).filter_by(comment_content="Bravo !").first() assert result is not None - assert result.commenter.username == "pytest_user_feedback" - assert result.commenter.account_password is not None - assert result.article.title == "Titre feedback" - assert result.article.writer.account_password is not None + assert result.comment_author.account_username == "pytest_user_comment" + assert result.comment_author.account_password is not None + assert result.comment_article.article_title == "Titre comment" + assert result.comment_article.article_author.account_password is not None