Skip to content

Commit

Permalink
Schema editor mysql mariadb (#317)
Browse files Browse the repository at this point in the history
adds support for mysql in schema editor
adds support for mariadb in schema editor

 adds formatDefaultValue function to preformat default value depending on dataType
  • Loading branch information
plucik authored Apr 2, 2024
1 parent 7f52b01 commit 254eb15
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 43 deletions.
3 changes: 3 additions & 0 deletions pgmanage/app/include/OmniDatabase/MariaDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,3 +1296,6 @@ def GetDDL(self, p_schema, p_table, p_object, p_type):

def GetAutocompleteValues(self, p_columns, p_filter):
return None

def QueryTableDefinition(self, table=None):
return self.v_connection.Query("SHOW FULL COLUMNS FROM {0}".format(table), True)
3 changes: 3 additions & 0 deletions pgmanage/app/include/OmniDatabase/MySQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,3 +1244,6 @@ def GetDDL(self, p_schema, p_table, p_object, p_type):

def GetAutocompleteValues(self, p_columns, p_filter):
return None

def QueryTableDefinition(self, table=None):
return self.v_connection.Query("SHOW FULL COLUMNS FROM {0}".format(table), True)
3 changes: 3 additions & 0 deletions pgmanage/app/include/Spartacus/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
pass
try:
import pymysql
from pymysql.constants import CLIENT
v_supported_rdbms.append('MySQL')
v_supported_rdbms.append('MariaDB')
except ImportError:
Expand Down Expand Up @@ -1947,6 +1948,7 @@ def Open(self, p_autocommit=True):
password=self.v_password,
autocommit=p_autocommit,
read_default_file='~/.my.cnf',
client_flag=CLIENT.MULTI_STATEMENTS,
**self.connection_params
)
self.v_cur = self.v_con.cursor()
Expand Down Expand Up @@ -2332,6 +2334,7 @@ def Open(self, p_autocommit=True):
password=self.v_password,
autocommit=p_autocommit,
read_default_file='~/.my.cnf',
client_flag=CLIENT.MULTI_STATEMENTS,
**self.connection_params,
)
self.v_cur = self.v_con.cursor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ import axios from 'axios'
import { showToast } from '../notification_control'
import { settingsStore } from '../stores/stores_initializer'
function formatDefaultValue(defaultValue, dataType, table) {
if (!defaultValue) return null
if (defaultValue.trim().toLowerCase() == 'null') return null
let textTypesMap = ['CHAR', 'VARCHAR', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT',
'TEXT', 'CHARACTER', 'NCHAR', 'NVARCHAR',
'CHARACTER VARYING',
]
if (textTypesMap.includes(dataType.toUpperCase())) {
const stringValue = defaultValue.toString()
return stringValue
}
// If no conversion matches, return raw value
return table.client.raw(defaultValue);
}
export default {
name: "SchemaEditor",
props: {
Expand Down Expand Up @@ -252,25 +271,32 @@ export default {
if(changes.drops.length) table.dropColumns(changes.drops)
changes.typeChanges.forEach((coldef) => {
if(coldef.dataType === 'autoincrement') {
if (coldef.dataType === 'autoincrement') {
table.increments(coldef.name).alter()
}else {
table.specificType(coldef.name, coldef.dataType).defaultTo(coldef.defaultValue).alter({alterNullable : false})
} else {
let formattedDefault = formatDefaultValue(coldef.defaultValue, coldef.dataType, table)
table.specificType(coldef.name, coldef.dataType).defaultTo(formattedDefault).alter({ alterNullable: false })
coldef.skipDefaults = true
}
})
changes.defaults.forEach(function(coldef) {
if (coldef.defaultValue !== '') {
table.specificType(coldef.name, coldef.dataType).alter().defaultTo(table.client.raw(coldef.defaultValue)).alter({alterNullable : false, alterType: false})
}
changes.defaults.forEach(function (coldef) {
if (!!coldef?.skipDefaults) return
let formattedDefault = formatDefaultValue(coldef.defaultValue, coldef.dataType, table)
table.specificType(coldef.name, coldef.dataType).alter().defaultTo(formattedDefault).alter({ alterNullable: false, alterType: false })
// FIXME: this does not work, figure out how to do drop default via Knex.
// else {
// table.specificType(coldef.name, coldef.dataType).defaultTo().alter({alterNullable : false, alterType: false})
// }
})
changes.nullableChanges.forEach((coldef) => {
coldef.nullable ? table.setNullable(coldef.name) : table.dropNullable(coldef.name)
if (table.client.dialect === "mysql") {
coldef.nullable ? table.setNullable(coldef) : table.dropNullable(coldef)
} else {
coldef.nullable ? table.setNullable(coldef.name) : table.dropNullable(coldef.name)
}
})
// FIXME: commenting generates drop default - how to avoid this?
Expand All @@ -295,7 +321,7 @@ export default {
tabledef.columns.forEach((coldef) => {
// use Knex's magic to create a proper auto-incrementing column in database-agnostic way
let col = coldef.dataType === 'autoincrement' ?
table.increments(coldef.name) :
table.increments(coldef.name, {primaryKey: false}) :
table.specificType(coldef.name, coldef.dataType)
coldef.nullable ? col.nullable() : col.notNullable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,7 @@ export default {
label: "Create Table",
icon: "fas cm-all fa-edit",
onClick: () => {
tabSQLTemplate(
"Create Table",
this.templates.create_table.replace(
"#schema_name#",
this.getParentNode(this.selectedNode).title
)
);
tabsStore.createSchemaEditorTab(this.selectedNode, "create", "mysql")
},
},
],
Expand Down Expand Up @@ -191,17 +185,10 @@ export default {
icon: "fas cm-all fa-list",
children: [
{
label: "Alter Table (SQL)",
label: "Alter Table",
icon: "fas cm-all fa-edit",
onClick: () => {
tabSQLTemplate(
"Alter Table",
this.templates.alter_table.replace(
"#table_name#",
`${this.getParentNodeDeep(this.selectedNode, 2).title}.${this.selectedNode.title
}`
)
);
tabsStore.createSchemaEditorTab(this.selectedNode, "alter", "mysql")
},
},
{
Expand Down Expand Up @@ -796,30 +783,35 @@ export default {
icon: "fas node-all fa-cog node-procedure-list",
type: "procedure_list",
contextMenu: "cm_procedures",
database: node.data.database
});
this.insertNode(node, "Functions", {
icon: "fas node-all fa-cog node-function-list",
type: "function_list",
contextMenu: "cm_functions",
database: node.data.database
});
this.insertNode(node, "Views", {
icon: "fas node-all fa-eye node-view-list",
type: "view_list",
contextMenu: "cm_views",
database: node.data.database
});
this.insertNode(node, "Sequences", {
icon: "fas node-all fa-sort-numeric-down node-sequence-list",
type: "sequence_list",
contextMenu: "cm_sequences",
database: node.data.database
});
this.insertNode(node, "Tables", {
icon: "fas node-all fa-th node-table-list",
type: "table_list",
contextMenu: "cm_tables",
database: node.data.database
});
},
getTablesMariadb(node) {
Expand All @@ -839,6 +831,7 @@ export default {
icon: "fas node-all fa-table node-table",
type: "table",
contextMenu: "cm_table",
database: node.data.database
});
}, null);
})
Expand All @@ -859,29 +852,34 @@ export default {
icon: "fas node-all fa-thumbtack node-index",
type: "indexes",
contextMenu: "cm_indexes",
database: node.data.database
});
this.insertNode(node, "Uniques", {
icon: "fas node-all fa-key node-unique",
type: "uniques",
contextMenu: "cm_uniques",
database: node.data.database
});
this.insertNode(node, "Foreign Keys", {
icon: "fas node-all fa-key node-fkey",
type: "foreign_keys",
contextMenu: "cm_fks",
database: node.data.database
});
this.insertNode(node, "Primary Key", {
icon: "fas node-all fa-key node-pkey",
type: "primary_key",
contextMenu: "cm_pks",
database: node.data.database
});
this.insertNode(node, `Columns (${resp.data.length})`, {
icon: "fas node-all fa-columns node-column",
type: "column_list",
contextMenu: "cm_columns",
database: node.data.database
});
const columns_node = this.getFirstChildNode(node);
Expand All @@ -890,6 +888,7 @@ export default {
icon: "fas node-all fa-columns node-column",
type: "table_field",
contextMenu: "cm_column",
database: node.data.database
});
const table_field = this.getFirstChildNode(columns_node);
Expand Down Expand Up @@ -931,6 +930,7 @@ export default {
icon: "fas node-all fa-key node-pkey",
type: "pk",
contextMenu: "cm_pk",
database: node.data.database
});
});
})
Expand Down Expand Up @@ -982,6 +982,7 @@ export default {
icon: "fas node-all fa-key node-fkey",
type: "foreign_key",
contextMenu: "cm_fk",
database: node.data.database
});
}, null);
})
Expand Down Expand Up @@ -1055,6 +1056,7 @@ export default {
icon: "fas node-all fa-key node-unique",
type: "unique",
contextMenu: "cm_unique",
database: node.data.database
});
});
})
Expand Down Expand Up @@ -1104,6 +1106,7 @@ export default {
type: "index",
contextMenu: "cm_index",
uniqueness: el.uniqueness,
database: node.data.database
});
});
})
Expand Down Expand Up @@ -1155,6 +1158,7 @@ export default {
icon: "fas node-all fa-sort-numeric-down node-sequence",
type: "sequence",
contextMenu: "cm_sequence",
database: node.data.database
},
true
);
Expand All @@ -1180,6 +1184,7 @@ export default {
icon: "fas node-all fa-eye node-view",
type: "view",
contextMenu: "cm_view",
database: node.data.database
});
});
})
Expand Down Expand Up @@ -1254,6 +1259,7 @@ export default {
type: "function",
id: el.id,
contextMenu: "cm_function",
database: node.data.database
});
});
})
Expand Down Expand Up @@ -1335,6 +1341,7 @@ export default {
type: "procedure",
contextMenu: "cm_procedure",
id: el.id,
database: node.data.database
});
});
})
Expand Down
Loading

0 comments on commit 254eb15

Please sign in to comment.