Skip to content

Commit 67fb26a

Browse files
Merge pull request #18 from risclog-solution/fix
Fix
2 parents 6f82c14 + f99b6ba commit 67fb26a

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

src/sqlacodegen/cli.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
parse_policy_row,
3333
parse_publication_row,
3434
parse_trigger_row,
35+
parse_sequence_row,
3536
)
3637
from sqlacodegen.seed_export import export_pgdata_py
3738

@@ -253,6 +254,14 @@ class ExportDict(TypedDict, total=False):
253254
"parse_row_func": parse_publication_row,
254255
"file": "pg_publications.py",
255256
},
257+
{
258+
"title": "Sequences",
259+
"entities_varname": "all_sequences",
260+
"template": "ALEMBIC_SEQUENCE_TEMPLATE",
261+
"statement": "ALEMBIC_STANDALONE_SEQUENCE_STATEMENT",
262+
"parse_row_func": parse_sequence_row,
263+
"file": "pg_sequence.py",
264+
},
256265
]
257266

258267
# ----------- Export-Loop ------------
@@ -283,8 +292,9 @@ class ExportDict(TypedDict, total=False):
283292
orm_views, pg_alembic = gen_func(generator_views) # type: ignore[operator]
284293
with open(dest_orm_path, "w", encoding="utf-8") as f:
285294
f.write(orm_views)
286-
with open(dest_pg_path, "w", encoding="utf-8") as f:
287-
f.write("\n".join(pg_alembic))
295+
if pg_alembic:
296+
with open(dest_pg_path, "w", encoding="utf-8") as f:
297+
f.write("\n".join(pg_alembic))
288298
print(f"{title} geschrieben nach: {dest_orm_path.as_posix()}")
289299
else:
290300
generator_views = generator_class(metadata_views, engine, options)

src/sqlacodegen/risclog_generators.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -239,34 +239,47 @@ class {classname}(PortalObject): # type: ignore[misc]
239239
ORDER BY extname;
240240
"""
241241

242-
ALEMBIC_SEQUENCE_STATEMENT = """
242+
ALEMBIC_STANDALONE_SEQUENCE_STATEMENT = """
243+
WITH seqs AS (
244+
SELECT c.oid AS seq_oid,
245+
n.nspname AS schema,
246+
c.relname AS sequence_name
247+
FROM pg_class c
248+
JOIN pg_namespace n ON n.oid = c.relnamespace
249+
WHERE c.relkind = 'S'
250+
AND n.nspname NOT IN ('pg_catalog','information_schema')
251+
AND n.nspname NOT LIKE 'pg_toast%'
252+
AND n.nspname NOT LIKE 'pg_temp_%'
253+
)
243254
SELECT
244-
s.sequence_schema AS schema,
245-
s.sequence_name,
246-
s.data_type,
247-
s.start_value,
248-
s.minimum_value,
249-
s.maximum_value,
250-
s.increment,
251-
s.cycle_option AS cycle,
252-
ps.cache_size
253-
FROM information_schema.sequences s
255+
s.schema,
256+
s.sequence_name,
257+
ps.data_type,
258+
ps.start_value,
259+
ps.min_value AS minimum_value,
260+
ps.max_value AS maximum_value,
261+
ps.increment_by AS increment,
262+
ps.cycle,
263+
ps.cache_size
264+
FROM seqs s
254265
JOIN pg_catalog.pg_sequences ps
255-
ON s.sequence_schema = ps.schemaname
256-
AND s.sequence_name = ps.sequencename
257-
JOIN pg_class t
258-
ON t.relkind = 'r' AND t.relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = s.sequence_schema)
259-
JOIN pg_attribute a
260-
ON a.attrelid = t.oid
261-
JOIN pg_attrdef d
262-
ON d.adrelid = t.oid AND d.adnum = a.attnum
263-
WHERE
264-
s.sequence_schema NOT IN ('pg_catalog', 'information_schema')
265-
AND pg_get_expr(d.adbin, d.adrelid) LIKE '%nextval(%' || s.sequence_name || '%'
266-
ORDER BY s.sequence_schema, s.sequence_name;
266+
ON ps.schemaname = s.schema
267+
AND ps.sequencename = s.sequence_name
268+
LEFT JOIN pg_depend owned
269+
ON owned.classid = 'pg_class'::regclass
270+
AND owned.objid = s.seq_oid
271+
AND owned.deptype IN ('a','i') -- serial/identity ownership
272+
LEFT JOIN pg_depend used
273+
ON used.refclassid = 'pg_class'::regclass
274+
AND used.refobjid = s.seq_oid
275+
AND used.classid = 'pg_attrdef'::regclass -- used in a column DEFAULT
276+
WHERE owned.objid IS NULL
277+
AND used.objid IS NULL
278+
ORDER BY s.schema, s.sequence_name;
267279
"""
268280

269281

282+
270283
def parse_publication_row(
271284
row: dict[str, Any],
272285
template_def: str,

0 commit comments

Comments
 (0)