Skip to content

refactor: replace db.session with sessionmaker in console controllers#35228

Open
wdeveloper16 wants to merge 10 commits intolanggenius:mainfrom
wdeveloper16:refactor/db-session-controllers
Open

refactor: replace db.session with sessionmaker in console controllers#35228
wdeveloper16 wants to merge 10 commits intolanggenius:mainfrom
wdeveloper16:refactor/db-session-controllers

Conversation

@wdeveloper16
Copy link
Copy Markdown
Contributor

Summary

  • Migrates controllers/console/admin.py and controllers/console/explore/installed_app.py to explicit sessionmaker sessions.
  • admin.py: consolidates the previously mixed session_factory.create_session() + db.session pattern in InsertExploreAppListApi.post into a single expire_on_commit=False session so the lazy-loaded app.site relationship is
    accessible; InsertExploreAppApi.delete similarly consolidated from three separate sessions into one; banner insert/delete and the chunked email-lookup loop use targeted write sessions.
  • installed_app.py: the get list-build is moved inside the session so the lazy-loaded installed_app.app relationship is resolved before detach; post consolidates all reads and the conditional insert into one session; delete and patch use session.merge() to re-attach the passed-in installed_app object before mutation.

Test plan

  • Explore apps — insert a new app into the explore list; update an existing one; remove it via the admin delete endpoint.
  • Trial apps — insert an explore app with can_trial=true; verify TrialApp row is created.
  • Banners — create and delete an explore banner.
  • Installed apps — list, install, uninstall, and pin/unpin an app from the explore page.
  • Notification accounts — call batch_add_notification_accounts with a CSV file and with a JSON body; verify matched account IDs are forwarded correctly.

Part of #24138.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels Apr 14, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

1 similar comment
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

No changes detected.

@wdeveloper16 wdeveloper16 force-pushed the refactor/db-session-controllers branch from 0bffe5a to e89300b Compare April 15, 2026 00:04
@wdeveloper16 wdeveloper16 requested a review from laipz8200 as a code owner April 15, 2026 00:04
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 00:06:02.917223207 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 00:05:51.927117397 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:594:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:595:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:596:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:597:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 00:08:27.682055219 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 00:08:16.812126437 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:594:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:595:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:596:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:597:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@wdeveloper16 wdeveloper16 force-pushed the refactor/db-session-controllers branch from dca35a3 to f2a5897 Compare April 15, 2026 00:42
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 00:44:06.928821681 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 00:43:56.554764837 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:593:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:594:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:595:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:596:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 00:46:51.257040018 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 00:46:40.674055851 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:593:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:594:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:595:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:596:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@wdeveloper16 wdeveloper16 force-pushed the refactor/db-session-controllers branch from 98f2879 to 709624c Compare April 15, 2026 01:11
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 01:12:37.621770243 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 01:12:27.142766532 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:600:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:601:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:602:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:603:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 01:14:43.784469638 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 01:14:33.537400323 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:600:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:601:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:602:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:603:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@wdeveloper16 wdeveloper16 force-pushed the refactor/db-session-controllers branch from 4c66cff to f59c9f9 Compare April 15, 2026 02:00
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 02:01:40.166806888 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 02:01:29.952572477 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:600:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:601:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:602:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:603:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 02:03:51.639260068 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 02:03:40.785265046 +0000
@@ -2003,13 +2003,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:600:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:601:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:602:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:603:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors console controllers to stop using db.session directly and instead use explicit SQLAlchemy sessionmaker sessions, aligning with the ongoing move toward engine-based session management.

Changes:

  • Migrate api/controllers/console/admin.py endpoints to sessionmaker(...).begin() sessions (including explore apps, banners, and chunked notification account lookups).
  • Migrate api/controllers/console/explore/installed_app.py to explicit sessions, keeping list construction inside the session and using merge() before mutation in delete/patch.
  • Update unit tests to patch db.engine and mock sessionmaker(...).begin() flows.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
api/controllers/console/admin.py Replaces mixed db.session/custom session usage with a consistent sessionmaker-based session scope.
api/controllers/console/explore/installed_app.py Moves DB operations to explicit sessions; ensures relationships are accessed before detach; uses merge() for updates/deletes.
api/tests/unit_tests/controllers/console/test_admin.py Updates admin controller tests to mock sessionmaker sessions and patch db.engine.
api/tests/unit_tests/controllers/console/explore/test_installed_app.py Updates installed-app controller tests to mock sessionmaker sessions and patch db.engine.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread api/tests/unit_tests/controllers/console/test_admin.py
wdeveloper16 and others added 2 commits April 14, 2026 22:37
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…d_app.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 02:39:00.954848629 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 02:38:51.361744256 +0000
@@ -2001,13 +2001,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:601:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:602:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:603:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:604:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-04-15 02:39:07.365028984 +0000
+++ /tmp/pyrefly_pr.txt	2026-04-15 02:38:56.843052103 +0000
@@ -2001,13 +2001,13 @@
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `NoneType` has no attribute `get`
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:657:29
+   --> tests/unit_tests/controllers/console/test_admin.py:601:29
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:658:34
+   --> tests/unit_tests/controllers/console/test_admin.py:602:34
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:659:39
+   --> tests/unit_tests/controllers/console/test_admin.py:603:39
 ERROR Object of class `str` has no attribute `get` [missing-attribute]
-   --> tests/unit_tests/controllers/console/test_admin.py:660:42
+   --> tests/unit_tests/controllers/console/test_admin.py:604:42
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_extension.py:14:5
 ERROR No attribute `MethodView` in module `builtins` [missing-attribute]

@wdeveloper16
Copy link
Copy Markdown
Contributor Author

wdeveloper16 commented Apr 15, 2026

Hi, @asukaminato0721
I'd be happy if you review this when you have a chance. 😉
Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants