MCPcopy Index your code
hub / github.com/cloudquery/cloudquery / createTable

Method createTable

plugins/destination/mysql/client/schema.go:145–196  ·  view source on GitHub ↗
(ctx context.Context, table *schema.Table)

Source from the content-addressed store, hash-verified

143}
144
145func (c *Client) createTable(ctx context.Context, table *schema.Table) error {
146 totalColumns := len(table.Columns)
147 primaryKeysIndices := []int{}
148
149 builder := strings.Builder{}
150 builder.WriteString("CREATE TABLE ")
151 builder.WriteString(identifier(table.Name))
152 builder.WriteString(" (\n ")
153 for i, column := range table.Columns {
154 builder.WriteString(identifier(column.Name))
155 builder.WriteString(" ")
156 builder.WriteString(arrowTypeToMySqlStr(column.Type))
157
158 if column.PrimaryKey {
159 primaryKeysIndices = append(primaryKeysIndices, i)
160 } else {
161 // Primary keys are implicitly not null and unique, so we only need to add these constraints if the column is not a primary key
162 if column.Unique {
163 builder.WriteString(" UNIQUE")
164 }
165 if column.NotNull {
166 builder.WriteString(" NOT NULL")
167 }
168 }
169
170 if i < totalColumns-1 {
171 builder.WriteString(",\n ")
172 }
173 }
174 if len(primaryKeysIndices) > 0 {
175 builder.WriteString(",\n ")
176 builder.WriteString(" PRIMARY KEY (")
177 lengthPerPk := c.maxIndexLength / len(primaryKeysIndices)
178 for i, pk := range primaryKeysIndices {
179 column := table.Columns[pk]
180 builder.WriteString(identifier(column.Name))
181 sqlType := arrowTypeToMySqlStr(column.Type)
182 if sqlType == "blob" || sqlType == "text" {
183 // `blob/text` SQL types require specifying prefix length to use for the primary key
184 // https://dev.mysql.com/doc/refman/8.0/en/innodb-limits.html
185 builder.WriteString("(" + strconv.Itoa(lengthPerPk) + ")")
186 }
187 if i < len(primaryKeysIndices)-1 {
188 builder.WriteString(", ")
189 }
190 }
191 builder.WriteString(")\n")
192 }
193 builder.WriteString(") CHARACTER SET utf8mb4;")
194 _, err := c.db.ExecContext(ctx, builder.String())
195 return err
196}
197
198func (c *Client) dropTable(ctx context.Context, table *schema.Table) error {
199 _, err := c.db.ExecContext(ctx, "DROP TABLE "+identifier(table.Name))

Callers 2

recreateTableMethod · 0.95
MigrateTablesMethod · 0.95

Calls 3

arrowTypeToMySqlStrFunction · 0.85
identifierFunction · 0.70
StringMethod · 0.45

Tested by

no test coverage detected