-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat(pg-tle-mode): deploy the functions from a provided output folder as a pg_tle extension #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
divyenduz
wants to merge
2
commits into
main
Choose a base branch
from
feat_deploy_tle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import dotenv from 'dotenv' | |
| import fs from 'fs' | ||
| import path from 'path' | ||
| import task from 'tasuku' | ||
| import { match } from 'ts-pattern' | ||
|
|
||
| import { Database } from '../helpers/Database' | ||
| import { ParseCLI } from '../helpers/ParseCLI' | ||
|
|
@@ -64,43 +65,95 @@ export async function deployCommand( | |
| } | ||
|
|
||
| const db = database.getConnection() | ||
| let deployCommands = fs | ||
| .readdirSync(outputFolderPath) | ||
| // Only extract .plv8.sql files, this will need to change if we ever make the extension configurable | ||
| .filter((file) => file.endsWith('.plv8.sql')) | ||
| .map((file) => { | ||
| const filePath = path.join(outputFolderPath, file) | ||
| return { | ||
| filePath, | ||
| sqlQueryPromise: db.file(filePath), | ||
| } | ||
| }) | ||
|
|
||
| await task( | ||
| `Deploying files from ${outputFolderPath} to the provided PostgreSQL database 🚧`.trim(), | ||
| async ({ setWarning }) => { | ||
| const taskGroup = await task.group((task) => | ||
| deployCommands.map((deployCommand) => { | ||
| const name = getFunctionNameFromFilePath(deployCommand.filePath) | ||
| return task( | ||
| `Deploying ${name}`, | ||
| async ({ setTitle: _setTitle, setError: _setError }) => { | ||
| try { | ||
| await deployCommand.sqlQueryPromise | ||
| _setTitle(`Deployed ${name}`) | ||
| } catch (e) { | ||
| _setError(`Failed to deploy ${name} (because of ${e.message})`) | ||
| setWarning(`Failed to some functions (see below))`) | ||
| } | ||
| } | ||
| await match(CLI.config.deployMode) | ||
| .with('functions', async () => { | ||
| const deployCommands = fs | ||
| .readdirSync(outputFolderPath) | ||
| // Only extract .plv8.sql files, this will need to change if we ever make the extension configurable | ||
| .filter((file) => file.endsWith('.plv8.sql')) | ||
| .map((file) => { | ||
| const filePath = path.join(outputFolderPath, file) | ||
| return { | ||
| filePath, | ||
| sqlQueryPromise: db.file(filePath), | ||
| } | ||
| }) | ||
|
|
||
| await task( | ||
| `Deploying files from ${outputFolderPath} to the provided PostgreSQL database 🚧`.trim(), | ||
| async ({ setWarning }) => { | ||
| const taskGroup = await task.group((task) => | ||
| deployCommands.map((deployCommand) => { | ||
| const name = getFunctionNameFromFilePath(deployCommand.filePath) | ||
| return task( | ||
| `Deploying ${name}`, | ||
| async ({ setTitle: _setTitle, setError: _setError }) => { | ||
| try { | ||
| await deployCommand.sqlQueryPromise | ||
| _setTitle(`Deployed ${name}`) | ||
| } catch (e) { | ||
| _setError( | ||
| `Failed to deploy ${name} (because of ${e.message})` | ||
| ) | ||
| setWarning(`Failed to some functions (see below))`) | ||
| } | ||
| } | ||
| ) | ||
| }) | ||
| ) | ||
|
|
||
| // TODO: add some batching here | ||
| await Promise.allSettled(taskGroup) | ||
| } | ||
| ) | ||
| }) | ||
| .with('pg_tle', async () => { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added two modes, 'pg_tle' mode gets the new code that instead of executing each individual SQL function file, collects them as a single string and executes |
||
| const deployableFunctionsSQL = fs | ||
| .readdirSync(outputFolderPath) | ||
| // Only extract .plv8.sql files, this will need to change if we ever make the extension configurable | ||
| .filter((file) => file.endsWith('.plv8.sql')) | ||
| .map((file) => { | ||
| const filePath = path.join(outputFolderPath, file) | ||
| const fileContents = fs.readFileSync(filePath, 'utf-8') | ||
| return fileContents | ||
| }) | ||
| .join('\n') | ||
| await task( | ||
| `Deploying files from ${outputFolderPath} to the provided PostgreSQL database as a pg_tle extension 🚧`.trim(), | ||
| async ({ setTitle, setError }) => { | ||
| try { | ||
| const extensionName = CLI.config.pgTLEExtensionName | ||
| const extensionVersion = CLI.config.pgTLEExtensionVersion | ||
| const extensionDescription = CLI.config.pgTLEExtensionDescription | ||
| await db.begin(async (db) => { | ||
| await db`CREATE EXTENSION IF NOT EXISTS pg_tle` | ||
| await db`DROP EXTENSION IF EXISTS ${db(extensionName)}` | ||
| await db`SELECT pgtle.uninstall_extension_if_exists(${extensionName})` | ||
| await db`SELECT pgtle.install_extension( | ||
| ${extensionName}, | ||
| ${extensionVersion}, | ||
| ${extensionDescription}, | ||
| ${deployableFunctionsSQL} | ||
| )` | ||
| await db`CREATE EXTENSION IF NOT EXISTS ${db(extensionName)}` | ||
| }) | ||
| await db.listen('notices', (payload) => { | ||
| console.log({ payload }) | ||
| }) | ||
| setTitle( | ||
| `Deployed files from ${outputFolderPath} to the provided PostgreSQL database as a pg_tle extension ✅` | ||
| ) | ||
| } catch (e) { | ||
| setError( | ||
| 'Failed to deploy the functions in the provided output folder as a pg_tle extension because: ' + | ||
| e | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| // TODO: add some batching here | ||
| await Promise.allSettled(taskGroup) | ||
| } | ||
| ) | ||
| }) | ||
| .exhaustive() | ||
|
|
||
| database.endConnection() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added two modes, 'functions' mode gets the existing code that was there.