Skip to content

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

feat(embeddings): zero downtime refresh

Greg Richardson60953ee06646
diff --git a/apps/docs/package.json b/apps/docs/package.json
index ca12c92170b..091e9591136 100644
--- a/apps/docs/package.json
+++ b/apps/docs/package.json
@@ -94,6 +94,7 @@
"ui": "*",
"unist-builder": "^3.0.1",
"unist-util-filter": "^4.0.1",
+ "uuid": "^9.0.0",
"valtio": "^1.7.6"
},
"devDependencies": {
diff --git a/apps/docs/scripts/search/generate-embeddings.ts b/apps/docs/scripts/search/generate-embeddings.ts
index d04765afc67..cf23afaba54 100644
--- a/apps/docs/scripts/search/generate-embeddings.ts
+++ b/apps/docs/scripts/search/generate-embeddings.ts
@@ -3,6 +3,7 @@ import dotenv from 'dotenv'
import 'openai'
import { Configuration, OpenAIApi } from 'openai'
import { inspect } from 'util'
+import { v4 as uuidv4 } from 'uuid'
import { fetchSources } from './sources'
dotenv.config()
@@ -33,6 +34,12 @@ async function generateEmbeddings() {
}
)
+ // Use this version to track which pages to purge
+ // after the refresh
+ const refreshVersion = uuidv4()
+
+ const refreshDate = new Date()
+
const embeddingSources = await fetchSources()
console.log(`Discovered ${embeddingSources.length} pages`)
@@ -92,6 +99,18 @@ async function generateEmbeddings() {
throw updatePageError
}
}
+
+ // No content update required
+ // Update this page to the latest refresh version & timestamp
+ const { error: updatePageError } = await supabaseClient
+ .from('page')
+ .update({ version: refreshVersion, last_refresh: refreshDate })
+ .filter('id', 'eq', existingPage.id)
+
+ if (updatePageError) {
+ throw updatePageError
+ }
+
continue
}
@@ -215,6 +234,16 @@ async function generateEmbeddings() {
}
}
+ // Delete pages that have been removed (and their sections via cascade)
+ const { error: deletePageError } = await supabaseClient
+ .from('page')
+ .delete()
+ .filter('version', 'neq', refreshVersion)
+
+ if (deletePageError) {
+ throw deletePageError
+ }
+
console.log('Embedding generation complete')
}
diff --git a/package-lock.json b/package-lock.json
index 0b6dffd6aa2..f86805970a7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -89,6 +89,7 @@
"ui": "*",
"unist-builder": "^3.0.1",
"unist-util-filter": "^4.0.1",
+ "uuid": "^9.0.0",
"valtio": "^1.7.6"
},
"devDependencies": {
@@ -593,6 +594,14 @@
"url": "https://opencollective.com/unified"
}
},
+ "apps/docs/node_modules/uuid": {
+ "version": "9.0.0",
+ "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
+ "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==",
+ "bin": {
+ "uuid": "dist/bin/uuid"
+ }
+ },
"apps/docs/node_modules/valtio": {
"version": "1.7.6",
"license": "MIT",
@@ -49780,7 +49789,7 @@
"@types/react": "^17.0.37",
"@types/react-dom": "^17.0.11",
"config": "*",
- "react-use": "*",
+ "react-use": "^17.4.0",
"tsconfig": "*",
"typescript": "^4.5.3"
},
@@ -51542,6 +51551,7 @@
"ui": "*",
"unist-builder": "^3.0.1",
"unist-util-filter": "^4.0.1",
+ "uuid": "^9.0.0",
"valtio": "^1.7.6"
},
"dependencies": {
@@ -51877,6 +51887,11 @@
"unist-util-is": "^4.0.0"
}
},
+ "uuid": {
+ "version": "9.0.0",
+ "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
+ "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg=="
+ },
"valtio": {
"version": "1.7.6",
"requires": {
@@ -66373,7 +66388,7 @@
"config": "*",
"configcat-js": "^7.0.0",
"dayjs": "^1.11.0",
- "eslint-config-next": "*",
+ "eslint-config-next": "^13.3.0",
"file-saver": "^2.0.5",
"generate-password": "^1.7.0",
"graphql": "^16.6.0",
diff --git a/supabase/migrations/20230421193603_page_version.sql b/supabase/migrations/20230421193603_page_version.sql
new file mode 100644
index 00000000000..bc48f4dcb62
--- /dev/null
+++ b/supabase/migrations/20230421193603_page_version.sql
@@ -0,0 +1,3 @@
+alter table "public"."page"
+add "version" uuid,
+add "last_refresh" timestamptz;