-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement user_favori table and drop favori
- Loading branch information
1 parent
2deca23
commit f78df04
Showing
8 changed files
with
146 additions
and
24 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
migrations/versions/16c8bc08a922_create_user_favori_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
"""Delete and re-create favori table | ||
Revision ID: 16c8bc08a922 | ||
Revises: a3fe3092f50c | ||
Create Date: 2023-10-29 18:56:52.112998 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import mysql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "16c8bc08a922" | ||
down_revision = "a3fe3092f50c" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"user_favori", | ||
sa.Column("formation_id", sa.String(length=36), nullable=False), | ||
sa.Column("user_id", sa.Integer(), nullable=False), | ||
sa.Column("created_at", sa.DateTime(), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["formation_id"], | ||
["formation.id"], | ||
ondelete="CASCADE", | ||
name="favori_formation_id_fk", | ||
), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], ["user.id"], ondelete="CASCADE", name="favori_user_id_fk" | ||
), | ||
sa.PrimaryKeyConstraint("formation_id", "user_id", name="favori_pk"), | ||
) | ||
op.create_index( | ||
op.f("ix_user_favori_formation_id"), | ||
"user_favori", | ||
["formation_id"], | ||
unique=False, | ||
) | ||
op.create_index( | ||
op.f("ix_user_favori_user_id"), "user_favori", ["user_id"], unique=False | ||
) | ||
|
||
op.execute( | ||
""" | ||
INSERT INTO user_favori (formation_id, user_id, created_at, updated_at) | ||
SELECT fr.id, f.request_user_id, f.created_at, f.updated_at | ||
FROM favori f | ||
JOIN formation fr ON f.code_nsf = fr.code_nsf | ||
AND f.sigle_type_formation = fr.type | ||
AND f.libelle_formation_principal = fr.libelle | ||
AND f.tutelle = fr.tutelle | ||
AND f.url_et_id_onisep = fr.url | ||
AND f.niveau_de_sortie_indicatif = fr.niveau_de_sortie | ||
AND f.duree = fr.duree | ||
AND f.created_at = fr.created_at | ||
AND f.updated_at = fr.updated_at; | ||
""" | ||
) | ||
|
||
op.drop_table("favori") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"favori", | ||
sa.Column("id", mysql.INTEGER(), autoincrement=True, nullable=False), | ||
sa.Column("code_nsf", mysql.TEXT(), nullable=True), | ||
sa.Column("sigle_type_formation", mysql.TEXT(), nullable=True), | ||
sa.Column("libelle_type_formation", mysql.TEXT(), nullable=True), | ||
sa.Column("libelle_formation_principal", mysql.TEXT(), nullable=True), | ||
sa.Column("sigle_formation", mysql.TEXT(), nullable=True), | ||
sa.Column("duree", mysql.TEXT(), nullable=True), | ||
sa.Column("niveau_de_sortie_indicatif", mysql.TEXT(), nullable=True), | ||
sa.Column("code_rncp", mysql.TEXT(), nullable=True), | ||
sa.Column("niveau_de_certification", mysql.TEXT(), nullable=True), | ||
sa.Column("libelle_niveau_de_certification", mysql.TEXT(), nullable=True), | ||
sa.Column("tutelle", mysql.TEXT(), nullable=True), | ||
sa.Column("url_et_id_onisep", mysql.TEXT(), nullable=False), | ||
sa.Column( | ||
"request_user_id", mysql.INTEGER(), autoincrement=False, nullable=True | ||
), | ||
sa.Column("created_at", mysql.DATETIME(), nullable=False), | ||
sa.Column("updated_at", mysql.DATETIME(), nullable=False), | ||
sa.ForeignKeyConstraint(["request_user_id"], ["user.id"], name="favori_ibfk_1"), | ||
sa.PrimaryKeyConstraint("id"), | ||
mysql_collate="utf8mb4_0900_ai_ci", | ||
mysql_default_charset="utf8mb4", | ||
mysql_engine="InnoDB", | ||
) | ||
op.drop_index(op.f("ix_user_favori_user_id"), table_name="user_favori") | ||
op.drop_index(op.f("ix_user_favori_formation_id"), table_name="user_favori") | ||
op.drop_table("user_favori") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from src.models.formation import Formation | ||
from .user import User | ||
from .favori import Favori | ||
from .user_favori import UserFavori | ||
|
||
models = [User, Favori, Formation] | ||
models = [User, UserFavori, Formation] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
from dataclasses import dataclass | ||
from typing import Callable | ||
|
||
from cuid2 import cuid_wrapper | ||
from src import db | ||
from src.models.base_model import BaseModel | ||
|
||
|
||
@dataclass | ||
class Favori(BaseModel): | ||
__tablename__ = "favori" | ||
|
||
formation_id: str | ||
user_id: int | ||
class UserFavori(BaseModel): | ||
__tablename__ = "user_favori" | ||
|
||
formation_id = db.Column( | ||
db.String(36), | ||
db.ForeignKey("formation.id", ondelete="CASCADE"), | ||
index=True, | ||
primary_key=True, | ||
index=True, | ||
) | ||
|
||
user_id = db.Column( | ||
db.Integer, | ||
db.ForeignKey("user.id", ondelete="CASCADE"), | ||
index=True, | ||
primary_key=True, | ||
index=True, | ||
) | ||
|
||
formation = db.relationship("Formation", back_populates="favoris") | ||
user = db.relationship("User", back_populates="users") |