-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb_tools.py
57 lines (47 loc) · 1.86 KB
/
db_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (C) 2024 - Michel Perrocheau (https://github.com/myrrkel).
# License LGPL-3.0 or later (https://www.gnu.org/licenses/algpl.html).
import docker_manager
class DatabaseManager(object):
def __init__(self, version=14, odoo_db='odoo_14', docker_manager=None):
self.version = version
self.odoo_version = '%s.0' % version
self.odoo_db = odoo_db
self.docker_manager = docker_manager or docker_manager.DockerManager(version, self.odoo_db)
self.host = self.docker_manager.get_postgres_ip()
self.connection = None
def connect(self, database="template1", user="odoo", pwd="odoo"):
if not self.host:
self.connection = None
connection = psycopg2.connect(
host=self.host,
database=database,
user=user,
password=pwd)
if connection:
self.connection = connection
def get_databse_list(self):
cur = self.connection.cursor()
query = '''
select datname from pg_database
where datdba=(select usesysid from pg_user where usename=current_user)
and not datistemplate and datallowconn
and datname not ilike 'template%' and datname not in ('postgres')
order by datname
'''
cur.execute(query)
rows = cur.fetchall()
return rows
def get_database_version(self, database_name):
connection = psycopg2.connect(host=self.host, database=database_name)
cur = connection.cursor()
try:
cur.execute("SELECT latest_version FROM ir_module_module WHERE name=%s", ('base',))
base_version = cur.fetchone()
return base_version
except Exception as e:
return 0
if __name__ == "__main__":
import psycopg2
db_manager = DatabaseManager()
db_manager.connect()
db_manager.get_databse_list()