diff --git a/src/containers/Tenant/Query/NewSQL/NewSQL.tsx b/src/containers/Tenant/Query/NewSQL/NewSQL.tsx index 814c915ef..cfc12c154 100644 --- a/src/containers/Tenant/Query/NewSQL/NewSQL.tsx +++ b/src/containers/Tenant/Query/NewSQL/NewSQL.tsx @@ -58,6 +58,14 @@ export function NewSQL() { text: i18n('action.drop-external-table'), action: actions.dropExternalTable, }, + { + text: i18n('action.add-index'), + action: actions.addTableIndex, + }, + { + text: i18n('action.drop-index'), + action: actions.dropTableIndex, + }, ], }, { diff --git a/src/containers/Tenant/Query/NewSQL/i18n/en.json b/src/containers/Tenant/Query/NewSQL/i18n/en.json index ffd5682bb..d08599580 100644 --- a/src/containers/Tenant/Query/NewSQL/i18n/en.json +++ b/src/containers/Tenant/Query/NewSQL/i18n/en.json @@ -10,6 +10,8 @@ "action.select-from-external-table": "Select from external table", "action.delete-rows": "Delete rows", "action.drop-table": "Drop table", + "action.add-index": "Add index", + "action.drop-index": "Drop index", "action.drop-external-table": "Drop external table", "menu.tables": "Tables", "menu.topics": "Topics", diff --git a/src/containers/Tenant/i18n/en.json b/src/containers/Tenant/i18n/en.json index 43a986e9a..3d5795d22 100644 --- a/src/containers/Tenant/i18n/en.json +++ b/src/containers/Tenant/i18n/en.json @@ -25,6 +25,7 @@ "actions.copied": "The path is copied to the clipboard", "actions.notCopied": "Couldn’t copy the path", "actions.copyPath": "Copy path", + "actions.dropIndex": "Drop index", "actions.openPreview": "Open preview", "actions.createTable": "Create table...", "actions.createExternalTable": "Create external table...", @@ -36,6 +37,7 @@ "actions.dropTopic": "Drop topic...", "actions.dropView": "Drop view...", "actions.alterTable": "Alter table...", + "actions.addTableIndex": "Add index...", "actions.alterTopic": "Alter topic...", "actions.selectQuery": "Select query...", "actions.upsertQuery": "Upsert query...", diff --git a/src/containers/Tenant/utils/newSQLQueryActions.ts b/src/containers/Tenant/utils/newSQLQueryActions.ts index d20ff5423..3898160ca 100644 --- a/src/containers/Tenant/utils/newSQLQueryActions.ts +++ b/src/containers/Tenant/utils/newSQLQueryActions.ts @@ -1,6 +1,7 @@ import {changeUserInput} from '../../../store/reducers/executeQuery'; import { + addTableIndex, alterAsyncReplicationTemplate, alterTableTemplate, alterTopicTemplate, @@ -17,6 +18,7 @@ import { dropAsyncReplicationTemplate, dropExternalTableTemplate, dropGroupTemplate, + dropTableIndex, dropTableTemplate, dropTopicTemplate, dropUserTemplate, @@ -25,7 +27,7 @@ import { selectQueryTemplate, updateTableTemplate, upsertQueryTemplate, -} from './newSQLQueryTemplates'; +} from './schemaQueryTemplates'; export const bindActions = (dispatch: React.Dispatch) => { const inputQuery = (query: () => string) => () => { @@ -58,5 +60,7 @@ export const bindActions = (dispatch: React.Dispatch) => { revokePrivilege: inputQuery(revokePrivilegeTemplate), dropUser: inputQuery(dropUserTemplate), dropGroup: inputQuery(dropGroupTemplate), + addTableIndex: inputQuery(addTableIndex), + dropTableIndex: inputQuery(dropTableIndex), }; }; diff --git a/src/containers/Tenant/utils/newSQLQueryTemplates.ts b/src/containers/Tenant/utils/newSQLQueryTemplates.ts deleted file mode 100644 index 20f59fab0..000000000 --- a/src/containers/Tenant/utils/newSQLQueryTemplates.ts +++ /dev/null @@ -1,250 +0,0 @@ -export const createTableTemplate = () => { - return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table -CREATE TABLE \`$path\` ( - category_id Uint64 NOT NULL, - id Uint64, - expire_at Datetime, - updated_on Datetime, - name Text, - \`binary-payload\` Bytes, - attributes JsonDocument, - -- uncomment to add a secondary index - -- INDEX idx_row_table_id GLOBAL SYNC ON ( id ) COVER ( name, attributes ), -- Secondary indexes docs https://ydb.tech/en/docs/yql/reference/syntax/create_table#secondary_index - PRIMARY KEY (category_id, id) -) -WITH ( - AUTO_PARTITIONING_BY_SIZE=\`$autoPartitioningBySize\`, - AUTO_PARTITIONING_PARTITION_SIZE_MB=\`$autoPartitioningPartitionSizeMb\`, - AUTO_PARTITIONING_BY_LOAD=\`$autoPartitioningByLoad\`, - AUTO_PARTITIONING_MIN_PARTITIONS_COUNT=\`$autoPartitioningMinPartitionsCount\`, - AUTO_PARTITIONING_MAX_PARTITIONS_COUNT=\`$autoPartitioningMaxPartitionsCount\` - -- uncomment to create a table with predefined partitions - -- , UNIFORM_PARTITIONS=\`$uniformPartitions\` -- The number of partitions for uniform initial table partitioning. - -- The primary key's first column must have type Uint64 or Uint32. - -- A created table is immediately divided into the specified number of partitions - -- uncomment to launch read only replicas in every AZ - -- , READ_REPLICAS_SETTINGS=\`$readReplicasSettings\` -- Enable read replicas for stale read, launch one replica in every availability zone - -- uncomment to enable ttl - -- , TTL=\`$ttl\` ON expire_at -- Enable background deletion of expired rows https://ydb.tech/en/docs/concepts/ttl - -- uncomment to create a table with a bloom filter - -- , KEY_BLOOM_FILTER=\`$keyBloomFilter\` -- With a Bloom filter, you can more efficiently determine - -- if some keys are missing in a table when making multiple single queries by the primary key. -)`; -}; - -export const createColumnTableTemplate = () => { - return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table#olap-tables -CREATE TABLE \`$path\` ( - id Int64 NOT NULL, - author Text, - title Text, - body Text, - PRIMARY KEY (id) -) -PARTITION BY HASH(id) -WITH (STORE=\`$store\`)`; -}; - -export const deleteRowsTemplate = () => { - return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/delete -DELETE FROM $path -WHERE Key1 == $key1 AND Key2 >= $key2;`; -}; - -export const updateTableTemplate = () => { - return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/update -UPDATE my_table -SET Value1 = YQL::ToString($value2 + 1), Value2 = $value2 - 1 -WHERE Key1 > $key1;`; -}; - -export const createUserTemplate = () => { - return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create-user -CREATE USER $user_name [option] --- user_name: The name of the user. It may contain lowercase Latin letters and digits. --- option: The password of the user: - -- PASSWORD 'password' creates a user with the password password. The ENCRYPTED option is always enabled. - -- PASSWORD NULL creates a user with an empty password.`; -}; - -export const createGroupTemplate = () => { - return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create-group -CREATE GROUP $group_name --- group_name: The name of the group. It may contain lowercase Latin letters and digits.`; -}; - -export const createAsyncReplicationTemplate = () => { - return `CREATE OBJECT secret_name (TYPE SECRET) WITH value=\`$secretValue\`; - -CREATE ASYNC REPLICATION my_replication -FOR \`$remotePath\` AS \`$localTableName\` --[, \`$anotherRemotePath\` AS \`$anotherLocalTableName\` ...] -WITH ( - CONNECTION_STRING=\`$connectionString\`, - TOKEN_SECRET_NAME=\`$tokenSecretName\` - -- ENDPOINT=\`$endpoint\`, - -- DATABASE=\`$database\`, - -- USER=\`$user\`, - -- PASSWORD_SECRET_NAME=\`$passwordSecretName\` -);`; -}; - -export const alterTableTemplate = () => { - return `ALTER TABLE \`$path\` - ADD COLUMN numeric_column Int32;`; -}; - -export const selectQueryTemplate = () => { - return `SELECT * - FROM \`$path\` - LIMIT 10;`; -}; - -export const upsertQueryTemplate = () => { - return `UPSERT INTO \`$path\` - ( \`id\`, \`name\` ) -VALUES ( );`; -}; - -export const dropExternalTableTemplate = () => { - return `DROP EXTERNAL TABLE \`$path\`;`; -}; - -export const dropUserTemplate = () => { - return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/drop-user -DROP USER [ IF EXISTS ] $user_name [, ...] - --- IF EXISTS: Suppress an error if the user doesn't exist. --- user_name: The name of the user to be deleted.`; -}; - -export const dropGroupTemplate = () => { - return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/drop-group -DROP GROUP [ IF EXISTS ] $group_name [, ...] - --- IF EXISTS: Suppress an error if the group doesn't exist. --- group_name: The name of the group to be deleted.`; -}; - -export const createCdcStreamTemplate = () => { - return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create_changefeed -ADD CHANGEFEED $name WITH ( - MODE = $mode, -- KEYS_ONLY, UPDATES, NEW_IMAGE, OLD_IMAGE, or NEW_AND_OLD_IMAGES - FORMAT = $format, -- JSON or DEBEZIUM_JSON - VIRTUAL_TIMESTAMPS = $virtualTimestamps, -- true or false - RETENTION_PERIOD = $retentionPeriod, -- Interval value, e.g., Interval('PT24H') - TOPIC_MIN_ACTIVE_PARTITIONS = $topicMinActivePartitions, - INITIAL_SCAN = $initialScan -- true or false -) - --- MODE options: --- KEYS_ONLY: Only the primary key components and change flag are written. --- UPDATES: Updated column values that result from updates are written. --- NEW_IMAGE: Any column values resulting from updates are written. --- OLD_IMAGE: Any column values before updates are written. --- NEW_AND_OLD_IMAGES: A combination of NEW_IMAGE and OLD_IMAGE modes.`; -}; - -export const grantPrivilegeTemplate = () => { - return ` -GRANT $permission_name [, ...] | ALL [PRIVILEGES] -ON $path_to_scheme_object [, ...] -TO $role_name [, ...] -[WITH GRANT OPTION] - --- permission_name: The name of the access right to schema objects that needs to be assigned. --- path_to_scheme_object: The path to the schema object for which rights are being granted. --- role_name: The name of the user or group to whom rights on the schema object are being granted. --- WITH GRANT OPTION: Using this construct gives the user or group of users the right to manage access rights - --- to assign or revoke certain rights. This construct has functionality similar to granting --- the "ydb.access.grant" or GRANT right. A subject with the ydb.access.grant right cannot --- grant rights broader than they possess themselves.`; -}; - -export const revokePrivilegeTemplate = () => { - return ` -REVOKE [GRANT OPTION FOR] $permission_name [, ...] | ALL [PRIVILEGES] -ON $path_to_scheme_object [, ...] -FROM $role_name [, ...] - --- permission_name: The name of the access right to schema objects that needs to be revoked. --- path_to_scheme_object: The path to the schema object from which rights are being revoked. --- role_name: The name of the user or group from whom rights on the schema object are being revoked. --- GRANT OPTION FOR: Using this construct revokes the user's or group's right to manage access rights. --- All previously granted rights by this user remain in effect. --- This construct has functionality similar to revoking the "ydb.access.grant" or GRANT right.`; -}; - -export const createExternalTableTemplate = () => { - return `CREATE EXTERNAL TABLE \`$path\` ( - column1 Int, - column2 Int -) WITH ( - DATA_SOURCE=\`$dataSource\`, - LOCATION=\`$location\`, - FORMAT=\`$format\`, - \`file_pattern\`=\`$filePattern\` -);`; -}; - -export const createTopicTemplate = () => { - return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_topic -CREATE TOPIC \`$path\` ( - CONSUMER consumer1, - CONSUMER consumer2 WITH (read_from=\`$readFrom\`) -- Sets up the message write time starting from which the consumer will receive data. - -- Value type: Datetime OR Timestamp OR integer (unix-timestamp in the numeric format). - -- Default value: now -) WITH ( - min_active_partitions=\`$minActivePartitions\`, -- Minimum number of topic partitions. - partition_count_limit=\`$partitionCountLimit\`, -- Maximum number of active partitions in the topic. 0 is interpreted as unlimited. - retention_period=\`$retentionPeriod\`, -- Data retention period in the topic. Value type: Interval. - retention_storage_mb=\`$retentionStorageMb\`, -- Limit on the maximum disk space occupied by the topic data. - -- When this value is exceeded, the older data is cleared, like under a retention policy. - -- 0 is interpreted as unlimited. - partition_write_speed_bytes_per_second=\`$partitionWriteSpeedBytesPerSecond\`, -- Maximum allowed write speed per partition. - partition_write_burst_bytes=\`$partitionWriteBurstBytes\` -- Write quota allocated for write bursts. - -- When set to zero, the actual write_burst value is equalled to - -- the quota value (this allows write bursts of up to one second). -);`; -}; - -export const alterTopicTemplate = () => { - return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/alter_topic -ALTER TOPIC \`$path\` - ADD CONSUMER new_consumer WITH (read_from=\`$readFrom\`), -- Sets up the message write time starting from which the consumer will receive data. - -- Value type: Datetime OR Timestamp OR integer (unix-timestamp in the numeric format). - -- Default value: now - ALTER CONSUMER consumer1 SET (read_from=\`$readFrom\`), - DROP CONSUMER consumer2, - SET ( - min_active_partitions=\`$minActivePartitions\`, -- Minimum number of topic partitions. - partition_count_limit=\`$partitionCountLimit\`, -- Maximum number of active partitions in the topic. 0 is interpreted as unlimited. - retention_period=\`$retentionPeriod\`, -- Data retention period in the topic. Value type: Interval. - retention_storage_mb=\`$retentionStorageMb\`, -- Limit on the maximum disk space occupied by the topic data. - -- When this value is exceeded, the older data is cleared, like under a retention policy. - -- 0 is interpreted as unlimited. - partition_write_speed_bytes_per_second=\`$partitionWriteSpeedBytesPerSecond\`, -- Maximum allowed write speed per partition. - partition_write_burst_bytes=\`$partitionWriteBurstBytes\` -- Write quota allocated for write bursts. - -- When set to zero, the actual write_burst value is equalled to - -- the quota value (this allows write bursts of up to one second). - );`; -}; - -export const dropTopicTemplate = () => { - return `DROP TOPIC \`$path\`;`; -}; - -export const createViewTemplate = () => { - return `CREATE VIEW \`$path\` WITH (security_invoker=\`$securityInvoker\`) AS SELECT 1;`; -}; - -export const dropTableTemplate = () => { - return `DROP TABLE \`$path\`;`; -}; - -export const dropAsyncReplicationTemplate = () => { - return `DROP ASYNC REPLICATION \`$path\`;`; -}; - -export const alterAsyncReplicationTemplate = () => { - return `ALTER ASYNC REPLICATION \`$path\` SET (STATE=\`$state\`, FAILOVER_MODE=\`$failoverMode\`);`; -}; diff --git a/src/containers/Tenant/utils/schemaActions.ts b/src/containers/Tenant/utils/schemaActions.ts index 9035c3e34..590b4e2ed 100644 --- a/src/containers/Tenant/utils/schemaActions.ts +++ b/src/containers/Tenant/utils/schemaActions.ts @@ -9,7 +9,9 @@ import createToast from '../../../utils/createToast'; import {transformPath} from '../ObjectSummary/transformPath'; import i18n from '../i18n'; +import type {SchemaQueryParams} from './schemaQueryTemplates'; import { + addTableIndex, alterAsyncReplicationTemplate, alterTableTemplate, alterTopicTemplate, @@ -21,6 +23,7 @@ import { createViewTemplate, dropAsyncReplicationTemplate, dropExternalTableTemplate, + dropTableIndex, dropTopicTemplate, dropViewTemplate, selectQueryTemplate, @@ -34,28 +37,28 @@ interface ActionsAdditionalEffects { } const bindActions = ( - {path, relativePath}: {path: string; relativePath: string}, + schemaQueryParams: SchemaQueryParams, dispatch: React.Dispatch, additionalEffects: ActionsAdditionalEffects, ) => { const {setActivePath, updateQueryExecutionSettings, showCreateDirectoryDialog} = additionalEffects; - const inputQuery = (tmpl: (path: string) => string, mode?: QueryMode) => () => { + const inputQuery = (tmpl: (params?: SchemaQueryParams) => string, mode?: QueryMode) => () => { if (mode) { updateQueryExecutionSettings({queryMode: mode}); } - dispatch(changeUserInput({input: tmpl(relativePath)})); + dispatch(changeUserInput({input: tmpl(schemaQueryParams)})); dispatch(setTenantPage(TENANT_PAGES_IDS.query)); dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery)); - setActivePath(path); + setActivePath(schemaQueryParams.path); }; return { createDirectory: showCreateDirectoryDialog ? () => { - showCreateDirectoryDialog(path); + showCreateDirectoryDialog(schemaQueryParams.path); } : undefined, createTable: inputQuery(createTableTemplate, 'script'), @@ -74,9 +77,11 @@ const bindActions = ( dropTopic: inputQuery(dropTopicTemplate, 'script'), createView: inputQuery(createViewTemplate, 'script'), dropView: inputQuery(dropViewTemplate, 'script'), + dropIndex: inputQuery(dropTableIndex, 'script'), + addTableIndex: inputQuery(addTableIndex, 'script'), copyPath: () => { try { - copy(relativePath); + copy(schemaQueryParams.relativePath); createToast({ name: 'Copied', title: i18n('actions.copied'), @@ -126,6 +131,7 @@ export const getActions = {text: i18n('actions.alterTable'), action: actions.alterTable}, {text: i18n('actions.selectQuery'), action: actions.selectQuery}, {text: i18n('actions.upsertQuery'), action: actions.upsertQuery}, + {text: i18n('actions.addTableIndex'), action: actions.addTableIndex}, ], ]; @@ -167,6 +173,10 @@ export const getActions = ], ]; + const INDEX_SET: ActionsSet = [ + [copyItem, {text: i18n('actions.dropIndex'), action: actions.dropIndex}], + ]; + const JUST_COPY: ActionsSet = [copyItem]; // verbose mapping to guarantee a correct actions set for new node types @@ -184,7 +194,7 @@ export const getActions = topic: TOPIC_SET, stream: JUST_COPY, - index: JUST_COPY, + index: INDEX_SET, external_table: EXTERNAL_TABLE_SET, external_data_source: EXTERNAL_DATA_SOURCE_SET, diff --git a/src/containers/Tenant/utils/schemaQueryTemplates.ts b/src/containers/Tenant/utils/schemaQueryTemplates.ts index 5d33324f6..0a319dd8b 100644 --- a/src/containers/Tenant/utils/schemaQueryTemplates.ts +++ b/src/containers/Tenant/utils/schemaQueryTemplates.ts @@ -1,6 +1,11 @@ -export const createTableTemplate = (path: string) => { +export interface SchemaQueryParams { + path: string; + relativePath: string; +} + +export const createTableTemplate = (params?: SchemaQueryParams) => { return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table -CREATE TABLE \`${path}/ydb_row_table\` ( +CREATE TABLE \`${params?.relativePath || '$path'}/ydb_row_table\` ( category_id Uint64 NOT NULL, id Uint64, expire_at Datetime, @@ -31,9 +36,9 @@ WITH ( -- if some keys are missing in a table when making multiple single queries by the primary key. )`; }; -export const createColumnTableTemplate = (path: string) => { +export const createColumnTableTemplate = (params?: SchemaQueryParams) => { return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table#olap-tables -CREATE TABLE \`${path}/ydb_column_table\` ( +CREATE TABLE \`${params?.relativePath || '$path'}/ydb_column_table\` ( id Int64 NOT NULL, author Text, title Text, @@ -57,44 +62,44 @@ WITH ( -- PASSWORD_SECRET_NAME="your_password" );`; }; -export const alterTableTemplate = (path: string) => { - return `ALTER TABLE \`${path}\` +export const alterTableTemplate = (params?: SchemaQueryParams) => { + return `ALTER TABLE \`${params?.relativePath || '$path'}\` ADD COLUMN numeric_column Int32;`; }; -export const selectQueryTemplate = (path: string) => { +export const selectQueryTemplate = (params?: SchemaQueryParams) => { return `SELECT * - FROM \`${path}\` + FROM \`${params?.relativePath || '$path'}\` LIMIT 10;`; }; -export const upsertQueryTemplate = (path: string) => { - return `UPSERT INTO \`${path}\` +export const upsertQueryTemplate = (params?: SchemaQueryParams) => { + return `UPSERT INTO \`${params?.relativePath || '$path'}\` ( \`id\`, \`name\` ) VALUES ( );`; }; -export const dropExternalTableTemplate = (path: string) => { - return `DROP EXTERNAL TABLE \`${path}\`;`; +export const dropExternalTableTemplate = (params?: SchemaQueryParams) => { + return `DROP EXTERNAL TABLE \`${params?.relativePath || '$path'}\`;`; }; -export const createExternalTableTemplate = (path: string) => { +export const createExternalTableTemplate = (params?: SchemaQueryParams) => { // Remove data source name from path // to create table in the same folder with data source - const targetPath = path.split('/').slice(0, -1).join('/'); + const targetPath = params?.relativePath.split('/').slice(0, -1).join('/'); - return `CREATE EXTERNAL TABLE \`${targetPath}/my_external_table\` ( + return `CREATE EXTERNAL TABLE \`${targetPath || '$path'}/my_external_table\` ( column1 Int, column2 Int ) WITH ( - DATA_SOURCE="${path}", + DATA_SOURCE="${params?.relativePath || '$path'}", LOCATION="", FORMAT="json_as_string", \`file_pattern\`="" );`; }; -export const createTopicTemplate = (path: string) => { +export const createTopicTemplate = (params?: SchemaQueryParams) => { return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_topic -CREATE TOPIC \`${path}/my_topic\` ( +CREATE TOPIC \`${params?.relativePath || '$path'}/my_topic\` ( CONSUMER consumer1, CONSUMER consumer2 WITH (read_from = Datetime('1970-01-01T00:00:00Z')) -- Sets up the message write time starting from which the consumer will receive data. -- Value type: Datetime OR Timestamp OR integer (unix-timestamp in the numeric format). @@ -113,9 +118,9 @@ CREATE TOPIC \`${path}/my_topic\` ( );`; }; -export const alterTopicTemplate = (path: string) => { +export const alterTopicTemplate = (params?: SchemaQueryParams) => { return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/alter_topic -ALTER TOPIC \`${path}\` +ALTER TOPIC \`${params?.relativePath || '$path'}\` ADD CONSUMER new_consumer WITH (read_from = Datetime('1970-01-01T00:00:00Z')), -- Sets up the message write time starting from which the consumer will receive data. -- Value type: Datetime OR Timestamp OR integer (unix-timestamp in the numeric format). -- Default value: now @@ -135,21 +140,128 @@ ALTER TOPIC \`${path}\` );`; }; -export const dropTopicTemplate = (path: string) => { - return `DROP TOPIC \`${path}\`;`; +export const dropTopicTemplate = (params?: SchemaQueryParams) => { + return `DROP TOPIC \`${params?.relativePath || '$path'}\`;`; +}; + +export const createViewTemplate = (params?: SchemaQueryParams) => { + return `CREATE VIEW \`${params?.relativePath || '$path'}/my_view\` WITH (security_invoker = TRUE) AS SELECT 1;`; +}; + +export const dropViewTemplate = (params?: SchemaQueryParams) => { + return `DROP VIEW \`${params?.relativePath || '$path'}\`;`; +}; +export const dropAsyncReplicationTemplate = (params?: SchemaQueryParams) => { + return `DROP ASYNC REPLICATION \`${params?.relativePath || '$path'}\`;`; +}; + +export const alterAsyncReplicationTemplate = (params?: SchemaQueryParams) => { + return `ALTER ASYNC REPLICATION \`${params?.relativePath || '$path'}\` SET (STATE = "DONE", FAILOVER_MODE = "FORCE");`; +}; + +export const addTableIndex = (params?: SchemaQueryParams) => { + return `ALTER TABLE \`${params?.relativePath || '$path'}\` ADD INDEX \`$indexName\` GLOBAL ON (\`$columnName\`);`; +}; + +export const dropTableIndex = (params?: SchemaQueryParams) => { + const indexName = params?.relativePath.split('/').pop(); + const path = params?.relativePath.split('/').slice(0, -1).join('/'); + return `ALTER TABLE \`${path || '$path'}\` DROP INDEX \`${indexName || '$indexName'}\`;`; +}; + +export const createCdcStreamTemplate = () => { + return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create_changefeed +ADD CHANGEFEED $name WITH ( + MODE = $mode, -- KEYS_ONLY, UPDATES, NEW_IMAGE, OLD_IMAGE, or NEW_AND_OLD_IMAGES + FORMAT = $format, -- JSON or DEBEZIUM_JSON + VIRTUAL_TIMESTAMPS = $virtualTimestamps, -- true or false + RETENTION_PERIOD = $retentionPeriod, -- Interval value, e.g., Interval('PT24H') + TOPIC_MIN_ACTIVE_PARTITIONS = $topicMinActivePartitions, + INITIAL_SCAN = $initialScan -- true or false +) + +-- MODE options: +-- KEYS_ONLY: Only the primary key components and change flag are written. +-- UPDATES: Updated column values that result from updates are written. +-- NEW_IMAGE: Any column values resulting from updates are written. +-- OLD_IMAGE: Any column values before updates are written. +-- NEW_AND_OLD_IMAGES: A combination of NEW_IMAGE and OLD_IMAGE modes.`; }; -export const createViewTemplate = (path: string) => { - return `CREATE VIEW \`${path}/my_view\` WITH (security_invoker = TRUE) AS SELECT 1;`; +export const createGroupTemplate = () => { + return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create-group +CREATE GROUP $group_name +-- group_name: The name of the group. It may contain lowercase Latin letters and digits.`; }; -export const dropViewTemplate = (path: string) => { - return `DROP VIEW \`${path}\`;`; +export const createUserTemplate = () => { + return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create-user +CREATE USER $user_name [option] +-- user_name: The name of the user. It may contain lowercase Latin letters and digits. +-- option: The password of the user: + -- PASSWORD 'password' creates a user with the password password. The ENCRYPTED option is always enabled. + -- PASSWORD NULL creates a user with an empty password.`; }; -export const dropAsyncReplicationTemplate = (path: string) => { - return `DROP ASYNC REPLICATION \`${path}\`;`; + +export const deleteRowsTemplate = (params?: SchemaQueryParams) => { + return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/delete +DELETE FROM \`${params?.relativePath || '$path'}\` +WHERE Key1 == $key1 AND Key2 >= $key2;`; +}; + +export const dropGroupTemplate = () => { + return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/drop-group +DROP GROUP [ IF EXISTS ] $group_name [, ...] + +-- IF EXISTS: Suppress an error if the group doesn't exist. +-- group_name: The name of the group to be deleted.`; +}; + +export const dropUserTemplate = () => { + return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/drop-user +DROP USER [ IF EXISTS ] $user_name [, ...] + +-- IF EXISTS: Suppress an error if the user doesn't exist. +-- user_name: The name of the user to be deleted.`; +}; + +export const grantPrivilegeTemplate = (params?: SchemaQueryParams) => { + return ` +GRANT $permission_name [, ...] | ALL [PRIVILEGES] +ON \`${params?.relativePath || '$path_to_scheme_object'}\` [, ...] +TO $role_name [, ...] +[WITH GRANT OPTION] + +-- permission_name: The name of the access right to schema objects that needs to be assigned. +-- path_to_scheme_object: The path to the schema object for which rights are being granted. +-- role_name: The name of the user or group to whom rights on the schema object are being granted. +-- WITH GRANT OPTION: Using this construct gives the user or group of users the right to manage access rights - +-- to assign or revoke certain rights. This construct has functionality similar to granting +-- the "ydb.access.grant" or GRANT right. A subject with the ydb.access.grant right cannot +-- grant rights broader than they possess themselves.`; +}; + +export const revokePrivilegeTemplate = (params?: SchemaQueryParams) => { + return ` +REVOKE [GRANT OPTION FOR] $permission_name [, ...] | ALL [PRIVILEGES] +ON \`${params?.relativePath || '$path_to_scheme_object'}\` [, ...] +FROM $role_name [, ...] + +-- permission_name: The name of the access right to schema objects that needs to be revoked. +-- path_to_scheme_object: The path to the schema object from which rights are being revoked. +-- role_name: The name of the user or group from whom rights on the schema object are being revoked. +-- GRANT OPTION FOR: Using this construct revokes the user's or group's right to manage access rights. +-- All previously granted rights by this user remain in effect. +-- This construct has functionality similar to revoking the "ydb.access.grant" or GRANT right.`; +}; + +export const updateTableTemplate = (params?: SchemaQueryParams) => { + return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/update +UPDATE \`${params?.relativePath || '$path'}\` +SET Value1 = YQL::ToString($value2 + 1), Value2 = $value2 - 1 +WHERE Key1 > $key1;`; }; -export const alterAsyncReplicationTemplate = (path: string) => { - return `ALTER ASYNC REPLICATION \`${path}\` SET (STATE = "DONE", FAILOVER_MODE = "FORCE");`; +export const dropTableTemplate = (params?: SchemaQueryParams) => { + return `DROP TABLE \`${params?.relativePath || '$path'}\`;`; };