* [start] postgres * [wip] started refactoring db_schema * Add psycopg2 to requirements.txt * Add support for Postgres SQL - Separate frameworkSQL, database, schema, setup_db file for mariaDB and postgres - WIP * Remove quotes from sql to make it compatible with postgres as well * Moved some code from db_schema to database.py * Move code from db_schema to schema.py Add other required refactoring * Add schema chages * Remove redundant code in file * Add invalid column name exception class to exceptions.py * Add back tick in query wherever needed and replace ifnull with coalesce * Update get_column_description code in database.py file * Remove a print statement * Add keys to get on_duplicate query * Add bactick wherever necessary - Remove db_schema.py file * Remove DATE_SUB as it is incompatible with postgres - Fix prepare_filter_condition * Add backtick and quotes wherever necessary - Move get_database_size to frappe.db namespace - fix some left out bugs and errors * Add code to create key and unique index - added mysql and posgres in their respective database.py * Add more bacticks in queries and fix some errors - Pass keys to on_duplicate_update method - Replace MONTH with EXTRACT function - Remove DATEDIFF and CURDATE usage * Cast state value to int in toggle_two_factor_auth - since two_factor_auth has the datatype of Int * Refactor - Replace Timediff with normal arithmetic operator - Add MAX_COLUMN_LENGTH - Remove Redundant code - Add regexp character constant - Move create_help_table to database.py - Add get_full_text_search_condition method - Inherit MariaDBTable from DBTable * Replace Database instance with get_db method * Move db_manager to separate file * Refactor - Remove some unwanted code - Separate alter table code for postgres and mysql - Replace data_type with column_type in database.py * Make fulltext search changes in global_search.py * Add empty string check * Add root_password to site config * Create cli command for postgres console * Move setup of help database to setup_db.py * Add get_database_list method * Fix exception handling - Replace bad_field handler with missing_column handler * Fix tests and sql queries * Fix import error * Fix typo db -> database * Fix error with make_table in help.py * Try test for postgres * Remove pyhton 2.7 version to try postgres travis test * Add test fixes * Add db_type to the config of test_site_postgres * Enable query debug to check the reason for travis fail * Add backticks to check if the test passes * Update travis.yml - Add postgres addon * Try appending 'd_' to hash for db_name - since postgres does not support dbname starting with a number * Try adding db_type for global help to make travis work * Add print statements to debug travis failure * Enable transaction and remove debug flag * Fix help table creation query (postgres) * Fix import issue * Add some checks to prevent errors - Some doctypes used to get called even before they are created * Try fixes * Update travis config * Fix create index for help table * Remove unused code * Fix queries and update travis config * Fix ifnull replace logic (regex) * Add query fixes and code cleanup * Fix typo - get_column_description -> get_table_columns_description * Fix tests - Replace double quotes in query with single quote * Replace psycopg2 with psycopg2-binary to avoid warnings - http://initd.org/psycopg/docs/install.html#binary-install-from-pypi * Add multisql api * Add few multisql queries * Remove print statements * Remove get_fulltext_search_condition method and replace with multi query * Remove text slicing in create user * Set default for 'values' argument in multisql * Fix incorrect queries and remove few debug flags - Fix multisql bug * Force delete user to fix test - Fix Import error - Fix incorrect query * Fix query builder bug * Fix bad query * Fix query (minor) * Convert boolean text to int since is_private has datatype of int - Some query changes like removed double quotes and replace with interpolated string to pass multiple value pass in one of the query * Extend database class from an object to support python 2 * Fix query - Add quotes around value passed to the query for variable comparision * Try setting host_name for each test site - To avoid "RemoteDisconnected" error while testing data migration test - Update travis.yml to add hosts - Remove unwanted commit in setup_help_database * Set site hostname to data migration connector (in test file) - To connect the same site host * Fix duplicate entry issue - the problem is in naming series file. In previous commits I unknowingly changed a part of a series query due to which series were not getting reset * Replace few sql queries with orm methods * Fix codacy * Fix 'Doctype Sessions not found' issue * Fix bugs induced during codacy fixes * Fix Notification Test - Use ORM instead of raw sql * Set Date fallback value to 0001-01-01 - 0000-00-00 is invalid date in Postgres - 0001-01-01 works in both * Fix date filter method * Replace double quotes with single quote for literal value * Remove print statement * Replace double quotes with single * Fix tests - Replace few raw sql with ORM * Separate query for postgres - update_fields_to_fetch_query * Fix tests - replace locate with strpos for postgres * Fix tests - Skip test for datediff - convert bytes to str in escape method * Remove TestBot * Skip fieldname extraction * Replace docshare raw sql with ORM * Fix typo * Fix ancestor query test * Fix test data migration * Remove hardcoded hostname * Add default option and option list for db_type * Remove frappe.async module * Remove a debug flag from test * Fix codacy * fix import issue * Convert classmethod to static method * Convert few instance methods to static methods * Remove some unused imports * Fix codacy - Add exception type - Replace few instance methods with static methods - Remove unsued import * Fix codacy * Remove unused code * Remove some unused codes - Convert some instance methods to static function * Fix a issue with query modification * Fix add_index query * Fix query * Fix update_auth patch * Fix a issue with exception handling * Add try catch to a reload_doc * Add try-catch to file_manager_hook patch * import update_gravatar to set_user_gravatar patch * Undo all the wrong patch fixes * Fix db_setup code 😪 - previously it was not restoring db from source SQL which is why few old patched were breaking (because they were getting different schema structure) * Fix typo ! * Fix exception(is_missing_column) handling * Add deleted code - This code is only used in a erpnext patch. Can be moved to that patch file * Fix codacy * Replace a mariadb specific function in a query used in validate_series * Remove a debug flag * Revert changes (rename_parent_and_child) * Fix validate_one_root method * Fix date format issue * Fix codacy - Disable a pylint for variable argument warning - Convert an instance method to static method * Add bandit.yml The Codacy seems to use Bandit which generates warning for every subprocess import and its usage during pytest Since we have carefully used subprocess (avoided user input), warnings needs to be avoided. This can be removed if we have any alternative for subprocess usage. * Skip start_process_with_partial_path check * Fix typo * Add python 2.7 test * Move python versions in travis.yml * Add python versions to jobs * Overwrite python version inheritance for postgres in travis.yml * Add quotes around python version in .travis.yml * Add quotes around the name of the job * Try a travis fix * Try .travis.yml fix * Import missing subprocess * Refactor travis.yml * Refactor travis.yml - move install and tests commands to separate files - Use matrix to build combination of python version and db type * Make install.sh and run-tests.sh executable * Add sudo required to travis.yml to allow sudo cmmands in shell files * Load nvm * Remove verbose flag from scripts * Remove command-trace-print flag * Change to build dir in before script * Add absolute path for scripts * Fix tests * Fix typo * Fix codacy - fixes - "echo won't expand escape sequences." warning * Append (_) underscore instead of 'd' for db_name * Remove printf and use mysql execute flag
339 lines
9.7 KiB
Python
339 lines
9.7 KiB
Python
from __future__ import unicode_literals
|
|
|
|
import re
|
|
import frappe
|
|
|
|
from frappe import _
|
|
from frappe.utils import cstr, cint, flt
|
|
|
|
|
|
class InvalidColumnName(frappe.ValidationError): pass
|
|
|
|
class DBTable:
|
|
def __init__(self, doctype, meta=None):
|
|
self.doctype = doctype
|
|
self.table_name = 'tab{}'.format(doctype)
|
|
self.meta = meta or frappe.get_meta(doctype)
|
|
self.columns = {}
|
|
self.current_columns = {}
|
|
|
|
# lists for change
|
|
self.add_column = []
|
|
self.change_type = []
|
|
self.change_name = []
|
|
self.add_unique = []
|
|
self.add_index = []
|
|
self.drop_index = []
|
|
self.set_default = []
|
|
|
|
# load
|
|
self.get_columns_from_docfields()
|
|
|
|
def sync(self):
|
|
if self.is_new():
|
|
self.create()
|
|
else:
|
|
self.alter()
|
|
|
|
def create(self):
|
|
pass
|
|
|
|
def get_column_definitions(self):
|
|
column_list = [] + frappe.db.DEFAULT_COLUMNS
|
|
ret = []
|
|
for k in list(self.columns):
|
|
if k not in column_list:
|
|
d = self.columns[k].get_definition()
|
|
if d:
|
|
ret.append('`'+ k + '` ' + d)
|
|
column_list.append(k)
|
|
return ret
|
|
|
|
def get_index_definitions(self):
|
|
ret = []
|
|
for key, col in self.columns.items():
|
|
if (col.set_index
|
|
and not col.unique
|
|
and col.fieldtype in frappe.db.type_map
|
|
and frappe.db.type_map.get(col.fieldtype)[0]
|
|
not in ('text', 'longtext')):
|
|
ret.append('index `' + key + '`(`' + key + '`)')
|
|
return ret
|
|
|
|
def get_columns_from_docfields(self):
|
|
"""
|
|
get columns from docfields and custom fields
|
|
"""
|
|
fl = frappe.db.sql("SELECT * FROM `tabDocField` WHERE parent = %s", self.doctype, as_dict = 1)
|
|
lengths = {}
|
|
precisions = {}
|
|
uniques = {}
|
|
|
|
# optional fields like _comments
|
|
if not self.meta.istable:
|
|
for fieldname in frappe.db.OPTIONAL_COLUMNS:
|
|
fl.append({
|
|
"fieldname": fieldname,
|
|
"fieldtype": "Text"
|
|
})
|
|
|
|
# add _seen column if track_seen
|
|
if getattr(self.meta, 'track_seen', False):
|
|
fl.append({
|
|
'fieldname': '_seen',
|
|
'fieldtype': 'Text'
|
|
})
|
|
|
|
if (not frappe.flags.in_install_db
|
|
and (frappe.flags.in_install != "frappe"
|
|
or frappe.flags.ignore_in_install)):
|
|
custom_fl = frappe.db.sql("""
|
|
SELECT * FROM `tabCustom Field`
|
|
WHERE dt = %s AND docstatus < 2
|
|
""", (self.doctype,), as_dict=1)
|
|
if custom_fl: fl += custom_fl
|
|
|
|
# apply length, precision and unique from property setters
|
|
for ps in frappe.get_all("Property Setter",
|
|
fields=["field_name", "property", "value"],
|
|
filters={
|
|
"doc_type": self.doctype,
|
|
"doctype_or_field": "DocField",
|
|
"property": ["in", ["precision", "length", "unique"]]
|
|
}):
|
|
|
|
if ps.property=="length":
|
|
lengths[ps.field_name] = cint(ps.value)
|
|
|
|
elif ps.property=="precision":
|
|
precisions[ps.field_name] = cint(ps.value)
|
|
|
|
elif ps.property=="unique":
|
|
uniques[ps.field_name] = cint(ps.value)
|
|
|
|
for f in fl:
|
|
self.columns[f['fieldname']] = DbColumn(self,
|
|
f['fieldname'],
|
|
f['fieldtype'],
|
|
lengths.get(f["fieldname"]) or f.get('length'),
|
|
f.get('default'),
|
|
f.get('search_index'),
|
|
f.get('options'),
|
|
uniques.get(f["fieldname"],
|
|
f.get('unique')),
|
|
precisions.get(f['fieldname']) or f.get('precision'))
|
|
|
|
def validate(self):
|
|
"""Check if change in varchar length isn't truncating the columns"""
|
|
if self.is_new():
|
|
return
|
|
|
|
self.setup_table_columns()
|
|
|
|
columns = [frappe._dict({"fieldname": f, "fieldtype": "Data"}) for f in
|
|
frappe.db.STANDARD_VARCHAR_COLUMNS]
|
|
columns += self.columns.values()
|
|
|
|
for col in columns:
|
|
if len(col.fieldname) >= 64:
|
|
frappe.throw(_("Fieldname is limited to 64 characters ({0})")
|
|
.format(frappe.bold(col.fieldname)))
|
|
|
|
if 'varchar' in frappe.db.type_map.get(col.fieldtype, ()):
|
|
|
|
# validate length range
|
|
new_length = cint(col.length) or cint(frappe.db.VARCHAR_LEN)
|
|
if not (1 <= new_length <= 1000):
|
|
frappe.throw(_("Length of {0} should be between 1 and 1000").format(col.fieldname))
|
|
|
|
current_col = self.current_columns.get(col.fieldname, {})
|
|
if not current_col:
|
|
continue
|
|
current_type = self.current_columns[col.fieldname]["type"]
|
|
current_length = re.findall(r'varchar\(([\d]+)\)', current_type)
|
|
if not current_length:
|
|
# case when the field is no longer a varchar
|
|
continue
|
|
current_length = current_length[0]
|
|
if cint(current_length) != cint(new_length):
|
|
try:
|
|
# check for truncation
|
|
max_length = frappe.db.sql("""SELECT MAX(CHAR_LENGTH(`{fieldname}`)) FROM `tab{doctype}`"""
|
|
.format(fieldname=col.fieldname, doctype=self.doctype))
|
|
|
|
except frappe.db.InternalError as e:
|
|
if frappe.db.is_missing_column(e):
|
|
# Unknown column 'column_name' in 'field list'
|
|
continue
|
|
else:
|
|
raise
|
|
|
|
if max_length and max_length[0][0] and max_length[0][0] > new_length:
|
|
if col.fieldname in self.columns:
|
|
self.columns[col.fieldname].length = current_length
|
|
|
|
frappe.msgprint(_("""Reverting length to {0} for '{1}' in '{2}';
|
|
Setting the length as {3} will cause truncation of data.""")
|
|
.format(current_length, col.fieldname, self.doctype, new_length))
|
|
|
|
def is_new(self):
|
|
return self.table_name not in frappe.db.get_tables()
|
|
|
|
def setup_table_columns(self):
|
|
# TODO: figure out a way to get key data
|
|
for c in frappe.db.get_table_columns_description(self.table_name):
|
|
self.current_columns[c.name.lower()] = c
|
|
|
|
def alter(self):
|
|
pass
|
|
|
|
|
|
class DbColumn:
|
|
def __init__(self, table, fieldname, fieldtype, length, default,
|
|
set_index, options, unique, precision):
|
|
self.table = table
|
|
self.fieldname = fieldname
|
|
self.fieldtype = fieldtype
|
|
self.length = length
|
|
self.set_index = set_index
|
|
self.default = default
|
|
self.options = options
|
|
self.unique = unique
|
|
self.precision = precision
|
|
|
|
def get_definition(self, with_default=1):
|
|
column_def = get_definition(self.fieldtype, precision=self.precision, length=self.length)
|
|
|
|
if not column_def:
|
|
return column_def
|
|
|
|
if self.fieldtype in ("Check", "Int"):
|
|
default_value = cint(self.default) or 0
|
|
column_def += ' not null default {0}'.format(default_value)
|
|
|
|
elif self.fieldtype in ("Currency", "Float", "Percent"):
|
|
default_value = flt(self.default) or 0
|
|
column_def += ' not null default {0}'.format(default_value)
|
|
|
|
elif self.default and (self.default not in frappe.db.DEFAULT_SHORTCUTS) \
|
|
and not self.default.startswith(":") and column_def not in ('text', 'longtext'):
|
|
column_def += " default {}".format(frappe.db.escape(self.default))
|
|
|
|
if self.unique and (column_def not in ('text', 'longtext')):
|
|
column_def += ' unique'
|
|
|
|
return column_def
|
|
|
|
def build_for_alter_table(self, current_def):
|
|
column_type = get_definition(self.fieldtype, self.precision, self.length)
|
|
|
|
# no columns
|
|
if not column_type:
|
|
return
|
|
|
|
# to add?
|
|
if not current_def:
|
|
self.fieldname = validate_column_name(self.fieldname)
|
|
self.table.add_column.append(self)
|
|
return
|
|
|
|
# type
|
|
if ((current_def['type']) != column_type):
|
|
self.table.change_type.append(self)
|
|
|
|
# unique
|
|
if((self.unique and not current_def['unique']) and column_type not in ('text', 'longtext')):
|
|
self.table.add_unique.append(self)
|
|
|
|
# default
|
|
if (self.default_changed(current_def)
|
|
and (self.default not in frappe.db.DEFAULT_SHORTCUTS)
|
|
and not cstr(self.default).startswith(":")
|
|
and not (column_type in ['text','longtext'])):
|
|
self.table.set_default.append(self)
|
|
|
|
# index should be applied or dropped irrespective of type change
|
|
if ((current_def['index'] and not self.set_index and not self.unique)
|
|
or (current_def['unique'] and not self.unique)):
|
|
# to drop unique you have to drop index
|
|
self.table.drop_index.append(self)
|
|
|
|
elif (not current_def['index'] and self.set_index) and not (column_type in ('text', 'longtext')):
|
|
self.table.add_index.append(self)
|
|
|
|
def default_changed(self, current_def):
|
|
if "decimal" in current_def['type']:
|
|
return self.default_changed_for_decimal(current_def)
|
|
else:
|
|
return current_def['default'] != self.default
|
|
|
|
def default_changed_for_decimal(self, current_def):
|
|
try:
|
|
if current_def['default'] in ("", None) and self.default in ("", None):
|
|
# both none, empty
|
|
return False
|
|
|
|
elif current_def['default'] in ("", None):
|
|
try:
|
|
# check if new default value is valid
|
|
float(self.default)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
elif self.default in ("", None):
|
|
# new default value is empty
|
|
return True
|
|
|
|
else:
|
|
# NOTE float() raise ValueError when "" or None is passed
|
|
return float(current_def['default'])!=float(self.default)
|
|
except TypeError:
|
|
return True
|
|
|
|
def validate_column_name(n):
|
|
special_characters = re.findall(r"[\W]", n, re.UNICODE)
|
|
if special_characters:
|
|
special_characters = ", ".join('"{0}"'.format(c) for c in special_characters)
|
|
frappe.throw(_("Fieldname {0} cannot have special characters like {1}").format(
|
|
frappe.bold(cstr(n)), special_characters), frappe.db.InvalidColumnName)
|
|
return n
|
|
|
|
def validate_column_length(fieldname):
|
|
if len(fieldname) > frappe.db.MAX_COLUMN_LENGTH:
|
|
frappe.throw(_("Fieldname is limited to 64 characters ({0})").format(fieldname))
|
|
|
|
def get_definition(fieldtype, precision=None, length=None):
|
|
d = frappe.db.type_map.get(fieldtype)
|
|
|
|
# convert int to long int if the length of the int is greater than 11
|
|
if fieldtype == "Int" and length and length > 11:
|
|
d = frappe.db.type_map.get("Long Int")
|
|
|
|
if not d: return
|
|
|
|
coltype = d[0]
|
|
size = d[1] if d[1] else None
|
|
|
|
if size:
|
|
if fieldtype in ["Float", "Currency", "Percent"] and cint(precision) > 6:
|
|
size = '21,9'
|
|
|
|
if coltype == "varchar" and length:
|
|
size = length
|
|
|
|
if size is not None:
|
|
coltype = "{coltype}({size})".format(coltype=coltype, size=size)
|
|
|
|
return coltype
|
|
|
|
def add_column(doctype, column_name, fieldtype, precision=None):
|
|
if column_name in frappe.db.get_table_columns(doctype):
|
|
# already exists
|
|
return
|
|
|
|
frappe.db.commit()
|
|
frappe.db.sql("alter table `tab%s` add column %s %s" % (doctype,
|
|
column_name, get_definition(fieldtype, precision)))
|
|
|
|
|