Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix toggle of realtime for tables logic

Joshen Limd2be37375912
diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts
index 70d0beaba31..4f1decc74e3 100644
--- a/studio/stores/pgmeta/MetaStore.ts
+++ b/studio/stores/pgmeta/MetaStore.ts
@@ -265,6 +265,46 @@ export default class MetaStore implements IMetaStore {
return await this.query(query)
}
+ async updateTableRealtime(table: PostgresTable, enable: boolean) {
+ const publications = this.publications.list()
+ const publicTables = this.tables.list((table: PostgresTable) => table.schema === 'public')
+
+ const realtimePublication = publications.find((pub) => pub.name === 'supabase_realtime')
+ const { id, tables: publicationTables } = realtimePublication
+
+ if (publicationTables === null) {
+ // UI doesn't have support for toggling realtime for ALL tables
+ // Switch it to individual tables via an array of strings
+ const realtimeTables = enable
+ ? publicTables.map((t: any) => `${t.schema}.${t.name}`)
+ : publicTables
+ .filter((t: any) => t.id !== table.id)
+ .map((t: any) => `${t.schema}.${t.name}`)
+ await this.publications.recreate(id, realtimeTables)
+ } else {
+ const isAlreadyEnabled = publicationTables.some((x: any) => x.id == table.id)
+
+ const realtimeTables =
+ isAlreadyEnabled && !enable
+ ? // Toggle realtime off
+ publicationTables
+ .filter((t: any) => t.id !== table.id)
+ .map((t: any) => `${t.schema}.${t.name}`)
+ : !isAlreadyEnabled && enable
+ ? // Toggle realtime on
+ [`${table.schema}.${table.name}`].concat(
+ publicationTables.map((t: any) => `${t.schema}.${t.name}`)
+ )
+ : null
+
+ if (realtimeTables === null) return
+
+ const payload = { id, tables: realtimeTables }
+ const { error: publicationsUpdateError } = await this.publications.update(id, payload)
+ if (publicationsUpdateError) throw publicationsUpdateError
+ }
+ }
+
async createColumn(payload: CreateColumnPayload, foreignKey?: Partial<PostgresRelationship>) {
try {
const column: any = await this.columns.create(payload)
@@ -374,21 +414,7 @@ export default class MetaStore implements IMetaStore {
if (updateTable.error) throw updateTable.error
}
- if (isRealtimeEnabled) {
- const publications = this.publications.list()
- const realtimePublication = publications.find(
- (publication) => publication.name === 'supabase_realtime'
- )
-
- const id = realtimePublication?.id
- const existingRealtimeTables = realtimePublication.tables || []
- const realtimeTables = [`${duplicateTable.schema}.${duplicatedTableName}`].concat(
- existingRealtimeTables.map((t: any) => `${t.schema}.${t.name}`)
- )
- let payload = { id, tables: realtimeTables }
- const { error: publicationsUpdateError } = await this.publications.update(id, payload)
- if (publicationsUpdateError) throw publicationsUpdateError
- }
+ if (isRealtimeEnabled && duplicatedTable) this.updateTableRealtime(duplicatedTable, true)
return duplicatedTable
}
@@ -419,21 +445,7 @@ export default class MetaStore implements IMetaStore {
}
// Toggle Realtime if configured to be
- if (isRealtimeEnabled) {
- const publications = this.publications.list()
- const realtimePublication = publications.find(
- (publication) => publication.name === 'supabase_realtime'
- )
-
- const id = realtimePublication?.id
- const existingRealtimeTables = realtimePublication.tables || []
- const realtimeTables = [`${table.schema}.${table.name}`].concat(
- existingRealtimeTables.map((t: any) => `${t.schema}.${t.name}`)
- )
- let payload = { id, tables: realtimeTables }
- const { error: publicationsUpdateError } = await this.publications.update(id, payload)
- if (publicationsUpdateError) throw publicationsUpdateError
- }
+ if (isRealtimeEnabled) this.updateTableRealtime(table, true)
// Then insert the columns - we don't do Promise.all as we want to keep the integrity
// of the column order during creation. Note that we add primary key constraints separately
@@ -647,7 +659,6 @@ export default class MetaStore implements IMetaStore {
if (primaryKeys.error) throw primaryKeys.error
}
- // Update realtime configuration
const publications = this.publications.list()
const realtimePublication = publications.find(
(publication) => publication.name === 'supabase_realtime'
diff --git a/studio/stores/pgmeta/PublicationStore.ts b/studio/stores/pgmeta/PublicationStore.ts
index d8cb993817e..c5c23b49004 100644
--- a/studio/stores/pgmeta/PublicationStore.ts
+++ b/studio/stores/pgmeta/PublicationStore.ts
@@ -22,7 +22,7 @@ export default class PublicationStore extends PostgresMetaInterface<any> {
* Will recreate a publication.
* This is required if switching from "ALL TABLES" to individual tables.
*/
- async recreate(id: any) {
+ async recreate(id: any, tables: string[] = []) {
let currentPublication = this.byId(id)
let payload: any = {
name: currentPublication.name,
@@ -31,9 +31,9 @@ export default class PublicationStore extends PostgresMetaInterface<any> {
publish_delete: currentPublication.publish_delete,
publish_truncate: currentPublication.publish_truncate,
}
- if (currentPublication.tables == null) {
+ if (currentPublication.tables === null) {
// Previously was "ALL TABLES"
- payload.tables = []
+ payload.tables = tables
}
const deleted: any = await this.del(id)
if (deleted.error) {