Skip to content

Commit

Permalink
Show create table query on the schema tab (#118)
Browse files Browse the repository at this point in the history
It displays create table query on the schema tab. Query is hidden by
default.

Default view:
<img width="1365" alt="Screenshot 2024-05-15 at 11 22 34"
src="https://github.com/QuesmaOrg/quesma/assets/1474/d76fa38c-9746-439a-a0c9-f11097751901">

Show  query view:

<img width="1368" alt="Screenshot 2024-05-15 at 11 22 43"
src="https://github.com/QuesmaOrg/quesma/assets/1474/77fc29bc-961f-4a7e-bf0b-43ad95c920d7">
  • Loading branch information
nablaone authored May 15, 2024
1 parent 65f8591 commit 03456a7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 13 deletions.
7 changes: 4 additions & 3 deletions quesma/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ func (lm *LogManager) Stop() {
}

type discoveredTable struct {
columnTypes map[string]string
config config.IndexConfiguration
comment string
columnTypes map[string]string
config config.IndexConfiguration
comment string
createTableQuery string
}

func (lm *LogManager) ReloadTables() {
Expand Down
4 changes: 3 additions & 1 deletion quesma/clickhouse/schema_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (sl *schemaLoader) ReloadTables() {
}
}
comment := sl.SchemaManagement.tableComment(databaseName, table)
configuredTables[table] = discoveredTable{columns, indexConfig, comment}
createTableQuery := sl.SchemaManagement.createTableQuery(databaseName, table)
configuredTables[table] = discoveredTable{columns, indexConfig, comment, createTableQuery}
} else {
explicitlyDisabledTables = append(explicitlyDisabledTables, table)
}
Expand Down Expand Up @@ -88,6 +89,7 @@ func (sl *schemaLoader) populateTableDefinitions(configuredTables map[string]dis
castUnsupportedAttrValueTypesToString: true,
preferCastingToOthers: true,
},
CreateTableQuery: resTable.createTableQuery,
}
if containsAttributes(resTable.columnTypes) {
table.Config.attributes = []Attribute{NewDefaultStringAttribute()}
Expand Down
9 changes: 9 additions & 0 deletions quesma/clickhouse/schema_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,12 @@ func (s *SchemaManagement) tableComment(database, table string) (comment string)
}
return comment
}

func (s *SchemaManagement) createTableQuery(database, table string) (ddl string) {
err := s.chDb.QueryRow("SELECT create_table_query FROM system.tables WHERE database = ? and table = ? ", database, table).Scan(&ddl)

if err != nil {
logger.Error().Msgf("could not get table comment: %v", err)
}
return ddl
}
19 changes: 10 additions & 9 deletions quesma/clickhouse/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (
)

type Table struct {
Name string
DatabaseName string `default:""`
Cluster string `default:""`
Cols map[string]*Column
Config *ChTableConfig
Created bool // do we need to create it during first insert
indexes []IndexStatement
aliases map[string]string
Comment string // this human-readable comment
Name string
DatabaseName string `default:""`
Cluster string `default:""`
Cols map[string]*Column
Config *ChTableConfig
Created bool // do we need to create it during first insert
indexes []IndexStatement
aliases map[string]string
Comment string // this human-readable comment
CreateTableQuery string
}

func (t *Table) GetFields() []string {
Expand Down
4 changes: 4 additions & 0 deletions quesma/quesma/ui/asset/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@
font-weight: bold;
}

#schema .create-table-query {
padding: 1em;
}

#schema tr:hover {
background-color: #dddddd;
}
Expand Down
21 changes: 21 additions & 0 deletions quesma/quesma/ui/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ func (qmc *QuesmaManagementConsole) generateSchema() []byte {
buffer.Html(`</tr>`)
}

buffer.Html("<tr>")
buffer.Html(`<td colspan=2 class="create-table-query">`)
query := table.CreateTableQuery
// indent first line
query = strings.Replace(query, "(", "(\n", 1)
query = strings.ReplaceAll(query, "),", "),\n")
query = strings.ReplaceAll(query, ")`,", ")`,\n")

query = strings.ReplaceAll(query, " ENGINE", "\nENGINE")
query = strings.ReplaceAll(query, " SETTINGS", "\nSETTINGS")
query = strings.ReplaceAll(query, " PARTITION BY", "\nPARTITION BY")
query = strings.ReplaceAll(query, " ORDER BY", "\nORDER BY")
query = strings.ReplaceAll(query, " PRIMARY KEY", "\nPRIMARY KEY")
query = strings.ReplaceAll(query, " SAMPLE BY", "\nSAMPLE BY")
query = strings.ReplaceAll(query, " TTL", "\nTTL")

buffer.Html("<details><summary><b>Click to show CREATE TABLE query</b></summary><pre>")
buffer.Text(query)
buffer.Html("</pre></details>")
buffer.Html(`</td>`)
buffer.Html(`</tr>`)
}

buffer.Html("\n</table>")
Expand Down

0 comments on commit 03456a7

Please sign in to comment.