-
Notifications
You must be signed in to change notification settings - Fork 10
[DATABRICKS-4] PLU-600: add databricks create row action #1529
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
pregnantboy
wants to merge
1
commit into
databricks/3-dynamic-data
Choose a base branch
from
databricks/4-create-row
base: databricks/3-dynamic-data
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
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
159 changes: 159 additions & 0 deletions
159
packages/backend/src/apps/databricks/actions/create-row.ts
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 |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { IGlobalVariable, IRawAction } from '@plumber/types' | ||
|
|
||
| import { z } from 'zod' | ||
|
|
||
| import { databricksConfig } from '@/config/app-env-vars/databricks' | ||
| import StepError from '@/errors/step' | ||
| import logger from '@/helpers/logger' | ||
|
|
||
| import { createSession } from '../auth/create-client' | ||
| import { columnNameSchema, tableNameSchema } from '../common/schema' | ||
|
|
||
| const databricksTableUrl = `https://${databricksConfig.serverHostname}/explore/tables/${databricksConfig.catalog}` | ||
|
|
||
| const insertRowSchema = z.object({ | ||
| tableName: tableNameSchema, | ||
| rowData: z | ||
| .array( | ||
| z.object({ | ||
| columnName: columnNameSchema, | ||
| columnValue: z.string(), | ||
| }), | ||
| ) | ||
| .min(1, { message: 'At least one column is required' }), | ||
| }) | ||
|
|
||
| const createRowAction: IRawAction = { | ||
| name: 'Create row', | ||
| key: 'createDatabricksTableRow', | ||
| description: 'Creates a new row in your databricks datable', | ||
| arguments: [ | ||
| { | ||
| label: 'Select Table', | ||
| key: 'tableName', | ||
| type: 'dropdown' as const, | ||
| required: true, | ||
| variables: false, | ||
| showOptionValue: false, | ||
| source: { | ||
| type: 'query' as const, | ||
| name: 'getDynamicData' as const, | ||
| arguments: [ | ||
| { | ||
| name: 'key', | ||
| value: 'databricks-list-table-names', | ||
| }, | ||
| ], | ||
| }, | ||
| addNewOption: { | ||
| id: 'databricks-createTable', | ||
| type: 'modal', | ||
| label: 'Create a new table', | ||
| }, | ||
| clickableLink: { | ||
| label: 'View databricks dashboard', | ||
| url: databricksTableUrl, | ||
| }, | ||
| }, | ||
| { | ||
| label: 'New row data', | ||
| key: 'rowData', | ||
| type: 'multirow-multicol' as const, | ||
| required: true, | ||
| hiddenIf: { | ||
| fieldKey: 'tableName', | ||
| op: 'is_empty', | ||
| }, | ||
| subFields: [ | ||
| { | ||
| placeholder: 'Select or type to create a column', | ||
| key: 'columnName', | ||
| type: 'dropdown' as const, | ||
| required: true, | ||
| variables: false, | ||
| showOptionValue: false, | ||
| addNewOption: { | ||
| id: 'databricks-createTableColumn', | ||
| type: 'inline', | ||
| label: 'Create a new column', | ||
| }, | ||
| source: { | ||
| type: 'query' as const, | ||
| name: 'getDynamicData' as const, | ||
| arguments: [ | ||
| { | ||
| name: 'key', | ||
| value: 'databricks-list-table-columns', | ||
| }, | ||
| { | ||
| name: 'parameters.tableName', | ||
| value: '{parameters.tableName}', | ||
| }, | ||
| ], | ||
| }, | ||
| customStyle: { flex: 3 }, | ||
| }, | ||
| { | ||
| placeholder: 'Value', | ||
| key: 'columnValue', | ||
| type: 'string' as const, | ||
| required: false, | ||
| variables: true, | ||
| customStyle: { flex: 5, minWidth: 0, maxWidth: '62.5%' }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
|
|
||
| async run($: IGlobalVariable) { | ||
| const parametersParseResult = insertRowSchema.safeParse($.step.parameters) | ||
| if (parametersParseResult.success === false) { | ||
| throw new StepError( | ||
| 'Ensure your values are correct', | ||
| parametersParseResult.error.issues[0].message, | ||
| ) | ||
| } | ||
| const { session, endSession } = await createSession($).catch((e) => { | ||
| throw new StepError('Failed to create session', e.message) | ||
| }) | ||
| try { | ||
| const { tableName, rowData } = parametersParseResult.data | ||
| const columnNames = rowData.map((row) => row.columnName) | ||
| const columnValues = rowData.map((row) => row.columnValue) | ||
|
|
||
| const statement = `INSERT INTO \`${tableName}\` (${columnNames | ||
| .map((col) => `\`${col}\``) | ||
| .join(',')}) VALUES (${columnValues | ||
| .map((_val, index) => `:val${index}`) | ||
| .join(',')})` | ||
| const namedParameters = {} as Record<string, string> | ||
| for (let i = 0; i < columnValues.length; i++) { | ||
| namedParameters[`val${i}`] = columnValues[i] | ||
| } | ||
| const operation = await session.executeStatement(statement, { | ||
| namedParameters, | ||
| }) | ||
| await operation.fetchAll() | ||
| await endSession() | ||
| $.setActionItem({ | ||
| raw: { | ||
| success: true, | ||
| }, | ||
| }) | ||
| } catch (e) { | ||
| logger.error({ | ||
| event: 'databricks-create-row', | ||
| stepId: $.step?.id, | ||
| flowId: $.flow?.id, | ||
| executionId: $.execution?.id, | ||
| testRun: $.execution?.testRun, | ||
| error: e, | ||
| }) | ||
| throw new StepError('Failed to create row', e.message) | ||
| } finally { | ||
| await endSession() | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| export default createRowAction | ||
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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export default [] | ||
| import createRow from './create-row' | ||
|
|
||
| export default [createRow] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.