last fix for get a sql pool with sqlalquemy

This commit is contained in:
Antonio de la Rosa 2016-05-18 00:04:53 +02:00
parent 935e4878ea
commit 05bb9a2082
2 changed files with 24 additions and 18 deletions

View file

@ -1,17 +1,19 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
import sqlalchemy.pool as pool
import MySQLdb
import MySQLdb.cursors import MySQLdb.cursors
import sqlalchemy.pool as pool
class SqlClass: class SqlClass:
connection={} mypool=None
def __init__(self): def __init__(self):
self.max_overflow=30
self.pool_size=15
self.error_connection="" self.error_connection=""
self.connection={}
self.connected=False self.connected=False
self.connection_method=self.connect_to_db_sql self.connection_method=self.connect_to_db_sql
@ -27,19 +29,18 @@ class SqlClass:
def connect_to_db_sql(self, connection, name_connection="default"): def connect_to_db_sql(self, connection, name_connection="default"):
try: try:
def getconn():
def getcoon(): return MySQLdb.connect(connection['host'],
c = MySQLdb.connect(connection['host'],
user=connection['user'], user=connection['user'],
passwd=connection['password'], passwd=connection['password'],
db=connection['db'], db=connection['db'],
charset='utf8mb4', charset='utf8mb4',
cursorclass=MySQLdb.cursors.DictCursor) cursorclass=MySQLdb.cursors.DictCursor)
return c
mypool=pool.QueuePool(getcoon, max_overflow=30, pool_size=15) if SqlClass.mypool==None:
SqlClass.mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size)
self.connection[name_connection]=mypool.connect() self.connection[name_connection]=SqlClass.mypool.connect()
self.connected=True self.connected=True
@ -99,7 +100,7 @@ class SqlClass:
if self.connection[name_connection]: if self.connection[name_connection]:
self.connection[name_connection].close() self.connection[name_connection].close()
#self.connection[name_connection]=False self.connection[name_connection]=False
pass pass

View file

@ -10,11 +10,16 @@ from pathlib import Path
from colorama import init, Fore, Back, Style from colorama import init, Fore, Back, Style
from importlib import import_module, reload from importlib import import_module, reload
from paramecio.cromosoma.webmodel import WebModel from paramecio.cromosoma.webmodel import WebModel
from settings import config
#from models import books #from models import books
def start(): def start():
connection=WebModel.connection()
connection.connect_to_db(WebModel.connections['default'])
parser = argparse.ArgumentParser(description='A tool for create tables in databases using models from Cromosoma') parser = argparse.ArgumentParser(description='A tool for create tables in databases using models from Cromosoma')
parser.add_argument('--model', help='Model python path', required=True) parser.add_argument('--model', help='Model python path', required=True)
@ -58,7 +63,7 @@ def start():
if inspect.isclass(obj): if inspect.isclass(obj):
if obj.__module__==args.model and hasattr(obj, 'webmodel'): if obj.__module__==args.model and hasattr(obj, 'webmodel'):
WebModel.model[name.lower()]=obj() WebModel.model[name.lower()]=obj(connection)
#WebModel.modelobj #WebModel.modelobj
@ -79,7 +84,7 @@ def start():
#load the table of databases #load the table of databases
cursor=WebModel.query(WebModel, "show tables") cursor=connection.query("show tables")
table_exists=[] table_exists=[]
@ -114,7 +119,7 @@ def start():
for table in new_tables: for table in new_tables:
print(Style.NORMAL+"--Creating table "+table+"...") print(Style.NORMAL+"--Creating table "+table+"...")
WebModel.query(WebModel, WebModel.model[table].create_table()) connection.query(WebModel.model[table].create_table())
for table in new_tables: for table in new_tables:
@ -122,12 +127,12 @@ def start():
for k_field, index in WebModel.arr_sql_index[table].items(): for k_field, index in WebModel.arr_sql_index[table].items():
print("---Added index to "+k_field) print("---Added index to "+k_field)
WebModel.query(WebModel, index) connection.query(index)
for k_set, index_set in WebModel.arr_sql_set_index[table].items(): for k_set, index_set in WebModel.arr_sql_set_index[table].items():
if index_set!="": if index_set!="":
WebModel.query(WebModel, index_set) connection.query(index_set)
print("---Added constraint to "+k_set) print("---Added constraint to "+k_set)
print("--Adding uniques elements for the new table") print("--Adding uniques elements for the new table")
@ -144,12 +149,12 @@ def start():
if inspect.isclass(obj): if inspect.isclass(obj):
if obj.__module__=='backups.'+args.model and hasattr(obj, 'webmodel'): if obj.__module__=='backups.'+args.model and hasattr(obj, 'webmodel'):
WebModel.model['old_'+name.lower()]=obj() WebModel.model['old_'+name.lower()]=obj(connection)
print(Style.BRIGHT+"Checking old versions of model for find changes...") print(Style.BRIGHT+"Checking old versions of model for find changes...")
for table in tables: for table in tables:
#WebModel.query(WebModel, "") #connection.query("")
#Check if new table #Check if new table
#fields_to_add, fields_to_modify, fields_to_add_index, fields_to_add_constraint, fields_to_add_unique, fields_to_delete_index, fields_to_delete_unique, fields_to_delete_constraint, fields_to_delete #fields_to_add, fields_to_modify, fields_to_add_index, fields_to_add_constraint, fields_to_add_unique, fields_to_delete_index, fields_to_delete_unique, fields_to_delete_constraint, fields_to_delete