From 44427fb61df5aca633d2a2aa0103457f10a656c2 Mon Sep 17 00:00:00 2001
From: Romeo Van Snick <romeo@romeovansnick.be>
Date: Mon, 1 Jul 2024 10:37:47 +0200
Subject: [PATCH] Add a reproduction for "Cannot find the new column in pick
 columns after alter schema and synced" (#44882)

* Add a reproduction for metabase#41765

* Navigate manually to the admin page
---
 .../admin/admin-reproductions.cy.spec.js      | 82 ++++++++++++++++++-
 1 file changed, 81 insertions(+), 1 deletion(-)

diff --git a/e2e/test/scenarios/admin/admin-reproductions.cy.spec.js b/e2e/test/scenarios/admin/admin-reproductions.cy.spec.js
index e1c78803e0a..7019c7cbe3d 100644
--- a/e2e/test/scenarios/admin/admin-reproductions.cy.spec.js
+++ b/e2e/test/scenarios/admin/admin-reproductions.cy.spec.js
@@ -1,5 +1,14 @@
 import { WRITABLE_DB_ID, SAMPLE_DB_ID } from "e2e/support/cypress_data";
-import { restore } from "e2e/support/helpers";
+import {
+  appBar,
+  entityPickerModal,
+  getNotebookStep,
+  popover,
+  queryWritableDB,
+  resetTestTable,
+  restore,
+  resyncDatabase,
+} from "e2e/support/helpers";
 
 describe("issue 26470", { tags: "@external" }, () => {
   beforeEach(() => {
@@ -54,3 +63,74 @@ describe("issue 21532", () => {
     });
   });
 });
+
+describe("issue 41765", { tags: ["@external"] }, () => {
+  // In this test we are testing the in-browser cache that metabase uses,
+  // so we need to navigate by clicking trough the UI without reloading the page.
+
+  const WRITABLE_DB_DISPLAY_NAME = "Writable Postgres12";
+
+  const TEST_TABLE = "scoreboard_actions";
+  const TEST_TABLE_DISPLAY_NAME = "Scoreboard Actions";
+
+  const COLUMN_NAME = "another_column";
+  const COLUMN_DISPLAY_NAME = "Another Column";
+
+  beforeEach(() => {
+    resetTestTable({ type: "postgres", table: TEST_TABLE });
+    restore("postgres-writable");
+    cy.signInAsAdmin();
+
+    resyncDatabase({
+      dbId: WRITABLE_DB_ID,
+      tableName: TEST_TABLE,
+    });
+  });
+
+  function enterAdmin() {
+    appBar().icon("gear").click();
+    popover().findByText("Admin settings").click();
+  }
+
+  function exitAdmin() {
+    appBar().findByText("Exit admin").click();
+  }
+
+  function openWritableDatabaseQuestion() {
+    // start new question without navigating
+    appBar().findByText("New").click();
+    popover().findByText("Question").click();
+
+    entityPickerModal().within(() => {
+      cy.findByText("Tables").click();
+      cy.findByText(WRITABLE_DB_DISPLAY_NAME).click();
+      cy.findByText(TEST_TABLE_DISPLAY_NAME).click();
+    });
+  }
+
+  it("re-syncing a database should invalidate the table cache (metabase#41765)", () => {
+    cy.visit("/");
+
+    queryWritableDB(
+      `ALTER TABLE ${TEST_TABLE} ADD ${COLUMN_NAME} text;`,
+      "postgres",
+    );
+
+    openWritableDatabaseQuestion();
+
+    getNotebookStep("data").button("Pick columns").click();
+    popover().findByText(COLUMN_DISPLAY_NAME).should("not.exist");
+
+    enterAdmin();
+
+    appBar().findByText("Databases").click();
+    cy.findAllByRole("link").contains(WRITABLE_DB_DISPLAY_NAME).click();
+    cy.button("Sync database schema now").click();
+
+    exitAdmin();
+    openWritableDatabaseQuestion();
+
+    getNotebookStep("data").button("Pick columns").click();
+    popover().findByText(COLUMN_DISPLAY_NAME).should("be.visible");
+  });
+});
-- 
GitLab