last fix for get a sql pool with sqlalquemy
This commit is contained in:
parent
935e4878ea
commit
05bb9a2082
2 changed files with 24 additions and 18 deletions
|
|
@ -1,17 +1,19 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import sqlalchemy.pool as pool
|
||||
import MySQLdb
|
||||
import MySQLdb.cursors
|
||||
import sqlalchemy.pool as pool
|
||||
|
||||
class SqlClass:
|
||||
|
||||
connection={}
|
||||
mypool=None
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.max_overflow=30
|
||||
self.pool_size=15
|
||||
self.error_connection=""
|
||||
self.connection={}
|
||||
self.connected=False
|
||||
self.connection_method=self.connect_to_db_sql
|
||||
|
||||
|
|
@ -23,23 +25,22 @@ class SqlClass:
|
|||
self.connection_method(connection, name_connection)
|
||||
|
||||
self.connection_method=self.dummy_connect
|
||||
|
||||
|
||||
def connect_to_db_sql(self, connection, name_connection="default"):
|
||||
|
||||
try:
|
||||
|
||||
def getcoon():
|
||||
c = MySQLdb.connect(connection['host'],
|
||||
def getconn():
|
||||
return MySQLdb.connect(connection['host'],
|
||||
user=connection['user'],
|
||||
passwd=connection['password'],
|
||||
db=connection['db'],
|
||||
charset='utf8mb4',
|
||||
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
|
||||
|
||||
|
|
@ -99,7 +100,7 @@ class SqlClass:
|
|||
if self.connection[name_connection]:
|
||||
|
||||
self.connection[name_connection].close()
|
||||
#self.connection[name_connection]=False
|
||||
self.connection[name_connection]=False
|
||||
|
||||
pass
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,16 @@ from pathlib import Path
|
|||
from colorama import init, Fore, Back, Style
|
||||
from importlib import import_module, reload
|
||||
from paramecio.cromosoma.webmodel import WebModel
|
||||
from settings import config
|
||||
|
||||
#from models import books
|
||||
|
||||
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.add_argument('--model', help='Model python path', required=True)
|
||||
|
|
@ -58,7 +63,7 @@ def start():
|
|||
if inspect.isclass(obj):
|
||||
if obj.__module__==args.model and hasattr(obj, 'webmodel'):
|
||||
|
||||
WebModel.model[name.lower()]=obj()
|
||||
WebModel.model[name.lower()]=obj(connection)
|
||||
|
||||
|
||||
#WebModel.modelobj
|
||||
|
|
@ -79,7 +84,7 @@ def start():
|
|||
|
||||
#load the table of databases
|
||||
|
||||
cursor=WebModel.query(WebModel, "show tables")
|
||||
cursor=connection.query("show tables")
|
||||
|
||||
table_exists=[]
|
||||
|
||||
|
|
@ -114,7 +119,7 @@ def start():
|
|||
|
||||
for table in new_tables:
|
||||
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:
|
||||
|
||||
|
|
@ -122,12 +127,12 @@ def start():
|
|||
|
||||
for k_field, index in WebModel.arr_sql_index[table].items():
|
||||
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():
|
||||
|
||||
if index_set!="":
|
||||
WebModel.query(WebModel, index_set)
|
||||
connection.query(index_set)
|
||||
print("---Added constraint to "+k_set)
|
||||
|
||||
print("--Adding uniques elements for the new table")
|
||||
|
|
@ -144,12 +149,12 @@ def start():
|
|||
if inspect.isclass(obj):
|
||||
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...")
|
||||
|
||||
for table in tables:
|
||||
#WebModel.query(WebModel, "")
|
||||
#connection.query("")
|
||||
#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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue