Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

PostgreSQLSessionStorage throw error: relation "shopify_sessions" already exists #412

Closed
1 task done
chuangbo opened this issue Jun 25, 2022 · 14 comments · Fixed by #413 or Shopify/shopify-app-js#92
Closed
1 task done

Comments

@chuangbo
Copy link
Contributor

Issue summary

Initialize context with PostgreSQLSessionStorage throws error: relation "shopify_sessions" already exists.

Expected behavior

No error.

Actual behavior

error: relation "shopify_sessions" already exists
    at Parser.parseErrorMessage (.../node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket (.../node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (.../node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (.../node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (node:events:527:28)
    at Socket.emit (node:domain:475:12)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

Steps to reproduce the problem

Shopify.Context.initialize({
  ...
  SESSION_STORAGE: new Shopify.Session.PostgreSQLSessionStorage(
    new URL(process.env.DATABASE_URL)
  ),
});

Reduced test case

The best way to get your bug fixed is to provide a reduced test case.


Checklist

  • I have described this issue in a way that is actionable (if possible)
@h4gen
Copy link

h4gen commented Dec 16, 2022

Hi!

This is still an Issue for me with "@shopify/shopify-app-session-storage-postgresql": "^1.0.0"

        const message = name === 'notice' ? new messages_1.NoticeMessage(length, messageValue) : new messages_1.DatabaseError(messageValue, length, name);
                                                                                                 ^
error: relation "shopify_sessions" already exists
    at Parser.parseErrorMessage (.../node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket (.../node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (.../node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (.../node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (node:events:527:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {

Best,
Hagen

@kelreel
Copy link

kelreel commented Dec 28, 2022

Yes, this problem remains on version 1.0.1

@SeanMythen
Copy link

SeanMythen commented Dec 30, 2022

I'm also getting this error, and it prevents my app from re-deploying if the shopify_sessions table already exists.

would adding something like CREATE TABLE IF NOT EXISTS prevent this error?

edit: I've tested it a bit more and adding that seems to work... I'm not sure it it's a true fix, someone with more experience with this will need to confirm.

@yadsgroup
Copy link

Hi there, any news about this issue?

@conspireagency
Copy link

conspireagency commented Jan 2, 2023 via email

@yadsgroup
Copy link

Hi @conspireagency, I'm facing the same issue with the patched version. Will try to go for a custom adapter

@conspireagency
Copy link

conspireagency commented Jan 2, 2023 via email

@SeanMythen
Copy link

@yadsgroup did adding IF NOT EXISTS work for you?

@yadsgroup
Copy link

@yadsgroup did adding IF NOT EXISTS work for you?

It worked when modify directly the file from node_module but I didn't succeed in forking the repo to have a stable solution (I'm not a Node expert 😬)

@SeanMythen
Copy link

@yadsgroup did adding IF NOT EXISTS work for you?

It worked when modify directly the file from node_module but I didn't succeed in forking the repo to have a stable solution (I'm not a Node expert 😬)

Great! and yes, I manually copied and modified the file into a different folder so that instead of having it download the package on deployment, it just uses the custom file I modified.

@yadsgroup
Copy link

@yadsgroup did adding IF NOT EXISTS work for you?

It worked when modify directly the file from node_module but I didn't succeed in forking the repo to have a stable solution (I'm not a Node expert 😬)

Great! and yes, I manually copied and modified the file into a different folder so that instead of having it download the package on deployment, it just uses the custom file I modified.

Thanks, will try 😊

@oguz-kara
Copy link

Hi, I just did -> web/node_modules/@shopify/shopify-app-session-storage-postgresql/build/cjs/postgresql.js
and change createTable() function with this

async createTable() {
    const hasSessionTable = await this.hasSessionTable();
    if (!hasSessionTable) {
      const query = `
        CREATE TABLE IF NOT EXISTS ${this.options.sessionTableName} (
          id varchar(255) NOT NULL PRIMARY KEY,
          shop varchar(255) NOT NULL,
          state varchar(255) NOT NULL,
          isOnline boolean NOT NULL,
          scope varchar(255),
          expires integer,
          onlineAccessInfo varchar(255),
          accessToken varchar(255)
        )
      `;
      await this.query(query);
    }
  }

it only adds CREATE TABLE IF NOT EXISTS query to the function..
and problem will be fixed.

@SeanMythen
Copy link

yes, that will fix the issue locally, but when you go to deploy, it won't use that file unless you specifiy it to. @oguz-kara

@conspireagency
Copy link

conspireagency commented Jan 18, 2023 via email

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
7 participants