77
88Hint: use docker to upgrade types from a new version in isolation. Run:
99
10- docker run --rm -p 11111:5432 --name pg -e POSTGRES_PASSWORD =password postgres :TAG
10+ docker run --rm -p 11111:5432 --name pg -e GAUSSDB_PASSWORD =password gaussdb :TAG
1111
1212with a specified version tag, and then query it using:
1313
14- %(prog)s "host=localhost port=11111 user=postgres password=password"
14+ %(prog)s "host=localhost port=11111 user=root password=password"
1515"""
1616
1717from __future__ import annotations
@@ -42,17 +42,15 @@ def main() -> None:
4242 else :
4343 update_python_oids (conn )
4444 update_python_types (conn )
45- update_cython_oids (conn )
4645
4746
4847def update_python_types (conn : Connection ) -> None :
49- fn = ROOT / "gaussdb/gaussdb/postgres .py"
48+ fn = ROOT / "gaussdb/gaussdb/gaussdb .py"
5049
5150 lines = []
5251 lines .extend (get_version_comment (conn ))
5352 lines .extend (get_py_types (conn ))
5453 lines .extend (get_py_ranges (conn ))
55- lines .extend (get_py_multiranges (conn ))
5654
5755 update_file (fn , lines )
5856 sp .check_call (["black" , "-q" , fn ])
@@ -69,16 +67,6 @@ def update_python_oids(conn: Connection) -> None:
6967 sp .check_call (["black" , "-q" , fn ])
7068
7169
72- def update_cython_oids (conn : Connection ) -> None :
73- fn = ROOT / "gaussdb_c/gaussdb_c/_gaussdb/oids.pxd"
74-
75- lines = []
76- lines .extend (get_version_comment (conn ))
77- lines .extend (get_cython_oids (conn ))
78-
79- update_file (fn , lines )
80-
81-
8270def update_crdb_python_oids (conn : Connection ) -> None :
8371 fn = ROOT / "gaussdb/gaussdb/crdb/_types.py"
8472
@@ -92,7 +80,22 @@ def update_crdb_python_oids(conn: Connection) -> None:
9280
9381def get_version_comment (conn : Connection ) -> list [str ]:
9482 if conn .info .vendor == "PostgreSQL" :
95- version = version_pretty (conn .info .server_version )
83+ # version = version_pretty(conn.info.server_version)
84+ raw_version = conn .info .server_version
85+
86+ if isinstance (raw_version , str ):
87+ # such as '505.2.0' → [505, 2, 0]
88+ parts = [int (x ) for x in re .findall (r"\d+" , raw_version )]
89+ if len (parts ) >= 2 :
90+ major , minor = parts [0 ], parts [1 ]
91+ patch = parts [2 ] if len (parts ) >= 3 else 0
92+ version_int = major * 10000 + minor * 100 + patch
93+ else :
94+ version_int = 0 # fallback
95+ else :
96+ version_int = raw_version
97+
98+ version = version_pretty (version_int )
9699 elif conn .info .vendor == "CockroachDB" :
97100 assert isinstance (conn , CrdbConnection )
98101 version = version_pretty (conn .info .server_version )
@@ -143,7 +146,7 @@ def get_py_types(conn: Connection) -> list[str]:
143146 """
144147select typname, oid, typarray,
145148 -- CRDB might have quotes in the regtype representation
146- replace(typname::regtype::text , '''', '') as regtype,
149+ replace(CAST( typname AS TEXT) , '''', '') as regtype,
147150 typdelim
148151from pg_type t
149152where
@@ -156,7 +159,7 @@ def get_py_types(conn: Connection) -> list[str]:
156159 ):
157160 typemod = typemods .get (typname )
158161
159- # Weird legacy type in postgres catalog
162+ # Weird legacy type in gaussdb catalog
160163 if typname == "char" :
161164 typname = regtype = '"char"'
162165
@@ -180,14 +183,14 @@ def get_py_ranges(conn: Connection) -> list[str]:
180183 lines = []
181184 for typname , oid , typarray , rngsubtype in conn .execute (
182185 """
183- select typname, oid, typarray, rngsubtype
186+ select t. typname, t. oid, t. typarray, r. rngsubtype
184187from
185188 pg_type t
186- join pg_range r on t.oid = rngtypid
189+ join pg_range r on t.oid = r. rngtypid
187190where
188- oid < 10000
189- and typtype = 'r'
190- order by typname
191+ t. oid < 10000
192+ and t. typtype = 'r'
193+ order by t. typname
191194"""
192195 ):
193196 params = [f"{ typname !r} , { oid } , { typarray } , subtype_oid={ rngsubtype } " ]
@@ -196,48 +199,6 @@ def get_py_ranges(conn: Connection) -> list[str]:
196199 return lines
197200
198201
199- def get_py_multiranges (conn : Connection ) -> list [str ]:
200- lines = []
201- for typname , oid , typarray , rngtypid , rngsubtype in conn .execute (
202- """
203- select typname, oid, typarray, rngtypid, rngsubtype
204- from
205- pg_type t
206- join pg_range r on t.oid = rngmultitypid
207- where
208- oid < 10000
209- and typtype = 'm'
210- order by typname
211- """
212- ):
213- params = [
214- f"{ typname !r} , { oid } , { typarray } ,"
215- f" range_oid={ rngtypid } , subtype_oid={ rngsubtype } "
216- ]
217- lines .append (f"MultirangeInfo({ ',' .join (params )} )," )
218-
219- return lines
220-
221-
222- def get_cython_oids (conn : Connection ) -> list [str ]:
223- lines = []
224- for typname , oid in conn .execute (
225- """
226- select typname, oid
227- from pg_type
228- where
229- oid < 10000
230- and (typtype = any('{b,r,m}') or typname = 'record')
231- and (typname !~ '^(_|pg_)' or typname = 'pg_lsn')
232- order by typname
233- """
234- ):
235- const_name = typname .upper () + "_OID"
236- lines .append (f" { const_name } = { oid } " )
237-
238- return lines
239-
240-
241202def update_file (fn : Path , new : list [str ]) -> None :
242203 with fn .open ("r" ) as f :
243204 lines = f .read ().splitlines ()
0 commit comments