@@ -41,11 +41,13 @@ class Pool(ConnectionInfo):
41
41
def __init__ (self ):
42
42
super (Pool , self ).__init__ ()
43
43
self .all_connections = {}
44
- self ._server_version = {}
45
- self ._mamonsu_bootstrap = {}
46
- self ._mamonsu_bootstrap_cache = 0
47
- self ._in_recovery = {}
48
- self ._in_recovery_cache = 0
44
+ self ._cache = {
45
+ 'server_version' : {'storage' : {}},
46
+ 'bootstrap' : {'storage' : {}, 'counter' : 0 , 'cache' : 10 },
47
+ 'recovery' : {'storage' : {}, 'counter' : 0 , 'cache' : 10 },
48
+ 'pgpro' : {'storage' : {}},
49
+ 'pgproee' : {'storage' : {}}
50
+ }
49
51
50
52
def connection_string (self , db = None ):
51
53
self ._init_conn_ (db )
@@ -58,46 +60,69 @@ def query(self, query, db=None):
58
60
return self .all_connections [db ].query (query )
59
61
60
62
def server_version (self , db = None ):
61
- if db in self ._server_version :
62
- return self ._server_version [db ]
63
+ if db in self ._cache [ 'server_version' ][ 'storage' ] :
64
+ return self ._cache [ 'server_version' ][ 'storage' ] [db ]
63
65
if platform .PY2 :
64
66
result = self .query ('show server_version' , db )[0 ][0 ]
65
67
elif platform .PY3 :
66
68
result = bytes (
67
69
self .query ('show server_version' , db )[0 ][0 ], 'utf-8' )
68
- self ._server_version [db ] = "{0}" .format (result .decode ('ascii' ))
69
- return self ._server_version [db ]
70
-
71
- def in_recovery (self , db = None ):
72
- if (db in self ._in_recovery ) and (self ._in_recovery_cache < 10 ):
73
- self ._in_recovery_cache += 1
74
- return self ._in_recovery [db ]
75
- self ._in_recovery_cache = 0
76
- self ._in_recovery [db ] = self .query (
77
- "select pg_catalog.pg_is_in_recovery()" )[0 ][0 ]
78
- return self ._in_recovery [db ]
70
+ self ._cache ['server_version' ]['storage' ][db ] = '{0}' .format (
71
+ result .decode ('ascii' ))
72
+ return self .self ._cache ['server_version' ]['storage' ][db ]
79
73
80
74
def server_version_greater (self , version , db = None ):
81
75
return self .server_version (db ) >= LooseVersion (version )
82
76
83
77
def server_version_less (self , version , db = None ):
84
78
return self .server_version (db ) <= LooseVersion (version )
85
79
86
- def mamonsu_bootstrap (self , db = None ):
87
- if (db in self ._mamonsu_bootstrap ) and (self ._mamonsu_bootstrap_cache < 10 ):
88
- self ._mamonsu_bootstrap_cache += 1
89
- return self ._mamonsu_bootstrap [db ]
90
- self ._mamonsu_bootstrap_cache = 0
80
+ def in_recovery (self , db = None ):
81
+ if db in self ._cache ['recovery' ]['storage' ]:
82
+ if self ._cache ['recovery' ]['counter' ] < self ._cache ['recovery' ]['cache' ]:
83
+ self ._cache ['recovery' ]['counter' ] += 1
84
+ return self ._cache ['recovery' ]['storage' ][db ]
85
+ self ._cache ['recovery' ]['counter' ] = 0
86
+ self ._cache ['recovery' ]['storage' ][db ] = self .query (
87
+ "select pg_catalog.pg_is_in_recovery()" )[0 ][0 ]
88
+ return self ._cache ['recovery' ]['storage' ][db ]
89
+
90
+ def is_bootstraped (self , db = None ):
91
+ if db in self ._cache ['bootstrap' ]['storage' ]:
92
+ if self .cache ['bootstrap' ]['counter' ] < self ._cache ['bootstrap' ]['cache' ]:
93
+ self .cache ['bootstrap' ]['counter' ] += 1
94
+ return self ._cache ['bootstrap' ]['storage' ][db ]
95
+ self ._cache ['bootstrap' ]['counter' ] = 0
91
96
sql = """select count(*) from pg_catalog.pg_class
92
97
where relname = 'mamonsu_config'"""
93
98
result = int (self .query (sql , db )[0 ][0 ])
94
- self ._mamonsu_bootstrap [db ] = (result == 1 )
95
- if self ._mamonsu_bootstrap [db ]:
96
- self .all_connections [db ].log .info (" Found mamonsu bootstrap" )
99
+ self ._cache [ 'bootstrap' ][ 'storage' ] [db ] = (result == 1 )
100
+ if self ._cache [ 'bootstrap' ][ 'storage' ] [db ]:
101
+ self .all_connections [db ].log .info (' Found mamonsu bootstrap' )
97
102
else :
98
- self .all_connections [db ].log .info ("Can't found mamonsu bootstrap" )
99
- self .all_connections [db ].log .info ("hint: run `mamonsu bootstrap` if you want to run without superuser rights" )
100
- return self ._mamonsu_bootstrap [db ]
103
+ self .all_connections [db ].log .info ('Can\' t found mamonsu bootstrap' )
104
+ self .all_connections [db ].log .info ('hint: run `mamonsu bootstrap` if you want to run without superuser rights' )
105
+ return self ._cache ['bootstrap' ]['storage' ][db ]
106
+
107
+ def is_pgpro (self , db = None ):
108
+ if db in self ._cache ['pgpro' ]:
109
+ return self ._cache ['pgpro' ][db ]
110
+ try :
111
+ self .query ('select pgpro_version()' )
112
+ self ._cache ['pgpro' ][db ] = True
113
+ except :
114
+ self ._cache ['pgpro' ][db ] = False
115
+ return self ._cache ['pgpro' ][db ]
116
+
117
+ def is_pgpro_ee (self , db = None ):
118
+ if not self .is_pgpro (self , db ):
119
+ return False
120
+ if db in self ._cache ['pgproee' ]:
121
+ return self ._cache ['pgproee' ][db ]
122
+ self ._cache ['pgproee' ][db ] = (
123
+ self .query ('select pgpro_edition()' )[0 ][0 ].lower () == 'enterprise'
124
+ )
125
+ return self ._cache ['pgproee' ][db ]
101
126
102
127
def extension_installed (self , ext , db = None ):
103
128
result = self .query ('select count(*) from pg_catalog.pg_extension\
@@ -116,7 +141,7 @@ def get_sql(self, typ, db=None):
116
141
if typ not in self .SQL :
117
142
raise LookupError ("Unknown SQL type: '{0}'" .format (typ ))
118
143
result = self .SQL [typ ]
119
- if self .mamonsu_bootstrap (db ):
144
+ if self .is_bootstraped (db ):
120
145
return result [1 ]
121
146
else :
122
147
return result [0 ]
0 commit comments