Skip to content

Commit

Permalink
change ORM query to SQL TEXT ones
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Jul 23, 2024
1 parent ed28ade commit 93d5c50
Showing 1 changed file with 109 additions and 87 deletions.
196 changes: 109 additions & 87 deletions backend/geonature/migrations/versions/imports/2b0b3bd0248c_multidest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@

from alembic import op
import sqlalchemy as sa
from sqlalchemy.orm import Session
from sqlalchemy.schema import Table, MetaData
from sqlalchemy.dialects.postgresql import JSON

from geonature.core.gn_commons.models.base import CorModuleDataset, TModules
from geonature.core.gn_permissions.models import (
PermObject,
PermissionAvailable,
PermAction,
Permission,
)


# revision identifiers, used by Alembic.
revision = "2b0b3bd0248c"
Expand All @@ -30,22 +21,6 @@
depends_on = None


def get_module_id(session, module_code):
return session.scalar(sa.select(TModules.id_module).where(TModules.module_code == module_code))


def get_id_object(session, code_object):
return session.scalar(
sa.select(PermObject.id_object).where(PermObject.code_object == code_object)
)


def get_id_action(session, code_action):
return session.scalar(
sa.select(PermAction.id_action).where(PermAction.code_action == code_action)
)


def upgrade():
meta = MetaData(bind=op.get_bind())
# Rename synthese_field → dest_field
Expand Down Expand Up @@ -240,49 +215,74 @@ def upgrade():
for column_name in ["desc_field", "id_theme", "order_field", "comment"]:
op.drop_column(schema="gn_imports", table_name="bib_fields", column_name=column_name)

session = Session(bind=op.get_bind())

id_module_import = get_module_id(session, "IMPORT")
id_module_synthese = get_module_id(session, "SYNTHESE")

id_object_import = get_id_object(session, "IMPORT")
id_object_all = get_id_object(session, "ALL")

id_action_create = get_id_action(session, "C")

session.close()

### Permissions

#### Enlever scope dans C IMPORT
op.execute(
sa.update(PermissionAvailable)
.where(
PermissionAvailable.id_action == id_action_create,
PermissionAvailable.id_module == id_module_import,
"""
WITH CTE AS (
SELECT
m.id_module, o.id_object, a.id_action
FROM
gn_commons.t_modules m,
gn_permissions.t_objects o,
gn_permissions.bib_actions a
WHERE
m.module_code = 'IMPORT'
AND
o.code_object = 'ALL'
AND
a.code_action = 'C'
)
.values(scope_filter=False)
UPDATE gn_permissions.t_permissions_available pa
SET scope_filter = false
FROM CTE
WHERE
pa.id_module = CTE.id_module
AND
pa.id_object = CTE.id_object
AND
pa.id_action = CTE.id_action
"""
)

op.execute(
sa.insert(PermissionAvailable).values(
id_action=id_action_create, id_module=id_module_synthese, id_object=id_object_all
)
"""
INSERT INTO
gn_permissions.t_permissions_available (id_module, id_object, id_action, label, scope_filter)
SELECT
m.id_module, o.id_object, a.id_action, 'Importer des observations', TRUE
FROM
gn_commons.t_modules m,
gn_permissions.t_objects o,
gn_permissions.bib_actions a
WHERE
m.module_code = 'SYNTHESE'
AND
o.code_object = 'ALL'
AND
a.code_action = 'C'
"""
)

op.execute(
sa.insert(Permission).from_select(
["id_role", "id_action", "id_module", "id_object"],
sa.select(
Permission.id_role,
Permission.id_action,
sa.literal(id_module_synthese).label("id_module"),
sa.literal(id_object_all).label("id_object"),
).where(
Permission.id_action == id_action_create,
Permission.id_module == id_module_import,
),
)
"""
INSERT INTO
gn_permissions.t_permissions (id_role, id_module, id_object, id_action, scope_value)
SELECT
p.id_role, new_module.id_module, new_object.id_object, p.id_action, p.scope_value
FROM
gn_permissions.t_permissions p
JOIN gn_permissions.bib_actions a USING(id_action)
JOIN gn_commons.t_modules m USING(id_module)
JOIN gn_permissions.t_objects o USING(id_object)
JOIN utilisateurs.t_roles r USING(id_role),
gn_commons.t_modules new_module,
gn_permissions.t_objects new_object
WHERE
a.code_action = 'C' AND m.module_code = 'IMPORT' AND o.code_object = 'IMPORT'
AND
new_module.module_code = 'SYNTHESE' AND new_object.code_object = 'ALL';
"""
)

# TODO constraint entity_field.entity.id_destination == entity_field.field.id_destination
Expand Down Expand Up @@ -360,53 +360,75 @@ def upgrade():
"""
)

ID_MODULE_IMPORT = (
op.get_bind()
.execute(
"""
SELECT id_module FROM gn_commons.t_modules WHERE module_code = 'IMPORT';
"""
)
.first()[0]
)
ID_MODULE_SYNTHESE = (
op.get_bind()
.execute(
"""
SELECT id_module FROM gn_commons.t_modules WHERE module_code = 'SYNTHESE';
"""
)
.first()[0]
)

## JDD IMPORT -> JDD Synthese
# update row with module = import to module=synthese in cor_module_dataset, only if the dataset is not already associated with a synthese
cte_ = (
sa.select(CorModuleDataset.id_dataset.label("id_dataset"))
.where(CorModuleDataset.id_module == id_module_synthese)
.cte("dataset_already_associated_with_synthese")
)
op.execute(
sa.update(CorModuleDataset)
.where(
CorModuleDataset.id_module == id_module_import,
CorModuleDataset.id_dataset != cte_.c.id_dataset,
)
.values(id_module=id_module_synthese)
f"""
UPDATE gn_commons.cor_module_dataset
SET id_module = {ID_MODULE_IMPORT}
WHERE id_module = {ID_MODULE_SYNTHESE};
"""
)

# delete remaining rows with association between datasets and the import module
op.execute(
sa.delete(CorModuleDataset).where(
CorModuleDataset.id_module == id_module_import,
)
f"""
DELETE FROM gn_commons.cor_module_dataset
WHERE id_module = {ID_MODULE_IMPORT};
"""
)


def downgrade():
session = Session(bind=op.get_bind())

id_module_import = get_module_id(session, "IMPORT")
id_module_synthese = get_module_id(session, "SYNTHESE")

id_action_create = get_id_action(session, "C")

session.close()

## C IMPORT -> C Synthese
warnings.warn("!!!!! Created synthese permissions created previously will remain !!!!)")
## JDD Synthese -> JDD Import
warnings.warn(
"Re-add association between datasets and the import module (!!!!! association between synthese and dataset created previously will remain! !!!!)"
)
op.execute(
sa.insert(CorModuleDataset).from_select(
["id_module", "id_dataset"],
sa.select(
sa.literal(id_module_import).label("id_module"),
CorModuleDataset.id_dataset.label("id_dataset"),
).where(CorModuleDataset.id_module == id_module_synthese),

ID_MODULE_IMPORT = (
op.get_bind()
.execute(
"""
SELECT id_module FROM gn_commons.t_modules WHERE module_code = 'IMPORT';
"""
)
.first()[0]
)
ID_MODULE_SYNTHESE = (
op.get_bind()
.execute(
"""
SELECT id_module FROM gn_commons.t_modules WHERE module_code = 'SYNTHESE';
"""
)
.first()[0]
)
op.execute(
f"""INSERT INTO gn_commons.cor_module_dataset (id_module, id_dataset)
SELECT {ID_MODULE_IMPORT}, id_dataset FROM gn_commons.cor_module_dataset WHERE id_module = {ID_MODULE_SYNTHESE};
"""
)

meta = MetaData(bind=op.get_bind())
Expand Down

0 comments on commit 93d5c50

Please sign in to comment.