MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / createTable

Function createTable

packages/db/src/clickhouse/migration.ts:68–116  ·  view source on GitHub ↗
({
  name: tableName,
  columns,
  indices = [],
  engine = 'MergeTree()',
  orderBy = ['tuple()'],
  partitionBy,
  settings = {},
  distributionHash,
  replicatedVersion,
  isClustered,
}: CreateTableOptions)

Source from the content-addressed store, hash-verified

66 * Handles both clustered and non-clustered scenarios
67 */
68export function createTable({
69 name: tableName,
70 columns,
71 indices = [],
72 engine = 'MergeTree()',
73 orderBy = ['tuple()'],
74 partitionBy,
75 settings = {},
76 distributionHash,
77 replicatedVersion,
78 isClustered,
79}: CreateTableOptions): string[] {
80 const columnDefinitions = [...columns, ...indices].join(',\n ');
81
82 const settingsClause = Object.entries(settings).length
83 ? `SETTINGS ${Object.entries(settings)
84 .map(([key, value]) => `${key} = ${value}`)
85 .join(', ')}`
86 : '';
87
88 const partitionByClause = partitionBy ? `PARTITION BY ${partitionBy}` : '';
89
90 if (!isClustered) {
91 // Non-clustered scenario: single table
92 return [
93 `CREATE TABLE IF NOT EXISTS ${tableName} (
94 ${columnDefinitions}
95)
96ENGINE = ${engine}
97${partitionByClause}
98ORDER BY (${orderBy.join(', ')})
99${settingsClause}`.trim(),
100 ];
101 }
102
103 return [
104 // Local replicated table
105 `CREATE TABLE IF NOT EXISTS ${replicated(tableName)} ON CLUSTER '{cluster}' (
106 ${columnDefinitions}
107)
108ENGINE = Replicated${engine.replace(/^(.+?)\((.+?)?\)/, `$1('${CLUSTER_REPLICA_PATH.replace('{replicatedVersion}', replicatedVersion)}', '{replica}', $2)`).replace(/, \)$/, ')')}
109${partitionByClause}
110ORDER BY (${orderBy.join(', ')})
111${settingsClause}`.trim(),
112 // Distributed table
113 `CREATE TABLE IF NOT EXISTS ${tableName} ON CLUSTER '{cluster}' AS ${replicated(tableName)}
114ENGINE = Distributed('{cluster}', currentDatabase(), ${replicated(tableName)}, ${distributionHash})`,
115 ];
116}
117
118export const modifyTTL = ({
119 tableName,

Callers 9

upFunction · 0.90
upFunction · 0.90
upFunction · 0.90
upFunction · 0.90
upFunction · 0.90
upFunction · 0.90
upFunction · 0.90
upFunction · 0.90
upFunction · 0.90

Calls 4

joinMethod · 0.80
replaceMethod · 0.80
replicatedFunction · 0.70
mapMethod · 0.45

Tested by

no test coverage detected