Pyzzel 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,133 @@
1
+ """Read PostgreSQL catalog metadata and build schema snapshots."""
2
+
3
+ import re
4
+
5
+ from pyzzel.core.base import Base
6
+ from .schema import canonical_type
7
+
8
+
9
+ def fetch_catalog(conn_or_none=None, *, schema=None):
10
+ """Return tables, columns, constraints, and index metadata as tuples."""
11
+ def fetch(execute):
12
+ tables = execute(
13
+ "SELECT c.oid, c.relname FROM pg_catalog.pg_class c "
14
+ "JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace "
15
+ "WHERE n.nspname=COALESCE(%s, current_schema()) AND c.relkind='r' "
16
+ "ORDER BY c.relname", (schema,),
17
+ )
18
+ columns = execute(
19
+ "SELECT a.attrelid, a.attnum, a.attname, "
20
+ "pg_catalog.format_type(a.atttypid,a.atttypmod), a.attnotnull, a.attidentity, "
21
+ "pg_catalog.pg_get_expr(d.adbin,d.adrelid) "
22
+ "FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON c.oid=a.attrelid "
23
+ "JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace "
24
+ "LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum "
25
+ "WHERE n.nspname=COALESCE(%s,current_schema()) AND c.relkind='r' "
26
+ "AND a.attnum>0 AND NOT a.attisdropped ORDER BY a.attrelid,a.attnum", (schema,),
27
+ )
28
+ constraints = execute(
29
+ "SELECT co.conrelid, co.conname, co.contype, co.conkey, rc.relname, "
30
+ "ARRAY(SELECT a.attname FROM unnest(co.confkey) WITH ORDINALITY k(attnum,ord) "
31
+ "JOIN pg_catalog.pg_attribute a ON a.attrelid=co.confrelid AND a.attnum=k.attnum "
32
+ "ORDER BY k.ord), co.confdeltype "
33
+ "FROM pg_catalog.pg_constraint co JOIN pg_catalog.pg_class c ON c.oid=co.conrelid "
34
+ "JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace "
35
+ "LEFT JOIN pg_catalog.pg_class rc ON rc.oid=co.confrelid "
36
+ "WHERE n.nspname=COALESCE(%s,current_schema()) AND co.contype IN ('p','u','f') "
37
+ "ORDER BY co.conrelid,co.conname", (schema,),
38
+ )
39
+ indexes = execute(
40
+ "SELECT i.indrelid, ic.relname, i.indisprimary, i.indisunique, "
41
+ "i.indpred IS NOT NULL, i.indexprs IS NOT NULL, i.indnkeyatts, "
42
+ "i.indkey::smallint[], am.amname, co.oid IS NOT NULL "
43
+ "FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class c ON c.oid=i.indrelid "
44
+ "JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace "
45
+ "JOIN pg_catalog.pg_class ic ON ic.oid=i.indexrelid "
46
+ "JOIN pg_catalog.pg_am am ON am.oid=ic.relam "
47
+ "LEFT JOIN pg_catalog.pg_constraint co ON co.conindid=i.indexrelid "
48
+ "AND co.contype IN ('p','u') "
49
+ "WHERE n.nspname=COALESCE(%s,current_schema()) AND c.relkind='r' "
50
+ "ORDER BY i.indrelid,ic.relname", (schema,),
51
+ )
52
+ return {"tables": tables, "columns": columns,
53
+ "constraints": constraints, "indexes": indexes}
54
+ if conn_or_none is None:
55
+ with Base.transaction():
56
+ return fetch(Base.execute)
57
+ with conn_or_none.transaction():
58
+ return fetch(lambda sql, params: conn_or_none.execute(sql, params).fetchall())
59
+
60
+
61
+ _DELETE = {"a": "NO ACTION", "r": "RESTRICT", "c": "CASCADE",
62
+ "n": "SET NULL", "d": "SET DEFAULT"}
63
+ _SUPPORTED = {"integer", "smallint", "bigint", "double precision", "numeric", "text",
64
+ "character varying", "boolean", "date", "time without time zone",
65
+ "timestamp with time zone", "timestamp without time zone", "uuid", "jsonb", "bytea"}
66
+
67
+
68
+ def _supported(sql_type):
69
+ if sql_type in _SUPPORTED:
70
+ return True
71
+ return bool(re.fullmatch(r"numeric\(\d+(?:,\d+)?\)|character varying\(\d+\)", sql_type))
72
+
73
+
74
+ def build_snapshot(catalog, *, ignore=(), exclude=()):
75
+ """Build a snapshot from fetch_catalog tuple rows without database access."""
76
+ skipped = set(ignore) | set(exclude)
77
+ table_names = {oid: name for oid, name in catalog["tables"] if name not in skipped}
78
+ by_oid = {oid: [] for oid in table_names}
79
+ for row in catalog["columns"]:
80
+ if row[0] in by_oid:
81
+ by_oid[row[0]].append(row)
82
+ constraints = {oid: [] for oid in table_names}
83
+ for row in catalog["constraints"]:
84
+ if row[0] in constraints:
85
+ constraints[row[0]].append(row)
86
+ warnings, tables = [], {}
87
+ for oid, table in sorted(table_names.items(), key=lambda item: item[1]):
88
+ cols = by_oid[oid]
89
+ names = {row[1]: row[2] for row in cols}
90
+ pks, uniques, fks = {}, {}, {}
91
+ for _, name, kind, keys, ref_table, ref_cols, delete in constraints[oid]:
92
+ keys = list(keys or ())
93
+ if kind in ("p", "u", "f") and len(keys) > 1:
94
+ label = {"p": "primary key", "u": "unique", "f": "foreign key"}[kind]
95
+ warnings.append(f"composite {label} on {table} is not supported")
96
+ if len(keys) == 1:
97
+ (pks if kind == "p" else uniques if kind == "u" else fks if kind == "f" else {}).update(
98
+ {keys[0]: (ref_table, list(ref_cols or ()), delete)})
99
+ output_columns = {}
100
+ for _, attnum, name, raw_type, notnull, identity, default in sorted(cols, key=lambda row: row[1]):
101
+ sql_type = canonical_type(raw_type)
102
+ if not _supported(sql_type):
103
+ warnings.append(f"unsupported type {sql_type} on {table}.{name}")
104
+ fk = fks.get(attnum)
105
+ ref = [fk[0], fk[1][0]] if fk and len(fk[1]) == 1 else None
106
+ output_columns[name] = {
107
+ "type": sql_type, "nullable": False if attnum in pks else not notnull,
108
+ "primary_key": attnum in pks, "identity": identity in ("a", "d"),
109
+ "unique": attnum in uniques, "default": default, "references": ref,
110
+ "on_delete": _DELETE.get(fk[2]) if ref else None,
111
+ }
112
+ plain_indexes = []
113
+ for row in catalog["indexes"]:
114
+ (table_oid, idx_name, primary, unique, predicate, expressions,
115
+ nkeys, attnums, method, backs_constraint) = row
116
+ if table_oid != oid:
117
+ continue
118
+ attnums = list(attnums or ())[:nkeys]
119
+ if primary or backs_constraint:
120
+ continue
121
+ reason = nkeys != 1 or predicate or expressions or (unique and not backs_constraint) or method != "btree"
122
+ if reason:
123
+ warnings.append(f"ignored index {idx_name} on {table}")
124
+ elif not unique and attnums[0] in names:
125
+ plain_indexes.append((attnums[0], names[attnums[0]]))
126
+ plain_indexes.sort(key=lambda item: item[0])
127
+ tables[table] = {"columns": output_columns,
128
+ "indexes": [name for _attnum, name in plain_indexes]}
129
+ return {"version": 1, "tables": tables}, warnings
130
+
131
+
132
+ def schema_from_database(*, schema=None, ignore=(), exclude=()):
133
+ return build_snapshot(fetch_catalog(schema=schema), ignore=ignore, exclude=exclude)
@@ -0,0 +1,137 @@
1
+ """Offline model schema snapshots."""
2
+
3
+ import json
4
+ import os
5
+ from pathlib import Path
6
+ import re
7
+ import tempfile
8
+
9
+ from pyzzel.errors import MigrationError
10
+
11
+
12
+ _TYPES = {
13
+ "integer": "integer", "int": "integer", "int4": "integer",
14
+ "smallint": "smallint", "int2": "smallint", "bigint": "bigint", "int8": "bigint",
15
+ "double precision": "double precision", "numeric": "numeric", "decimal": "numeric",
16
+ "text": "text", "boolean": "boolean", "bool": "boolean", "date": "date",
17
+ "time": "time without time zone", "time without time zone": "time without time zone",
18
+ "timetz": "time with time zone", "timestamp": "timestamp without time zone",
19
+ "timestamp without time zone": "timestamp without time zone",
20
+ "timestamptz": "timestamp with time zone", "timestamp with time zone": "timestamp with time zone",
21
+ "uuid": "uuid", "jsonb": "jsonb", "bytea": "bytea",
22
+ }
23
+
24
+
25
+ def canonical_type(sql_type):
26
+ value = " ".join(sql_type.strip().lower().split())
27
+ varchar = re.fullmatch(r"(?:varchar|character varying)\s*(\(\s*\d+\s*\))", value)
28
+ if varchar:
29
+ return f"character varying({varchar[1][1:-1].strip()})"
30
+ numeric = re.fullmatch(r"(?:numeric|decimal)\s*(\(\s*\d+\s*(?:,\s*\d+\s*)?\))", value)
31
+ if numeric:
32
+ return "numeric(" + ",".join(part.strip() for part in numeric[1][1:-1].split(",")) + ")"
33
+ return _TYPES.get(value, value)
34
+
35
+
36
+ def normalize_default(expr):
37
+ if expr is None:
38
+ return None
39
+ value = expr.strip()
40
+ while True:
41
+ stripped = re.sub(r"::\s*[a-z_][a-z0-9_ ]*(?:\([^)]*\))?\s*$", "", value, flags=re.I)
42
+ if stripped == value:
43
+ break
44
+ value = stripped
45
+ value = value.strip()
46
+ depth, quoted, closes_at_end, index = 0, False, False, 0
47
+ while index < len(value):
48
+ ch = value[index]
49
+ if ch == "'":
50
+ if quoted and index + 1 < len(value) and value[index + 1] == "'":
51
+ index += 2
52
+ continue
53
+ quoted = not quoted
54
+ elif not quoted and ch == "(":
55
+ depth += 1
56
+ elif not quoted and ch == ")":
57
+ depth -= 1
58
+ if depth == 0:
59
+ closes_at_end = index == len(value) - 1
60
+ if not closes_at_end:
61
+ break
62
+ index += 1
63
+ if len(value) >= 2 and value[0] == "(" and value[-1] == ")" and depth == 0 and closes_at_end:
64
+ value = value[1:-1].strip()
65
+ out, quoted, pending_space, i = [], False, False, 0
66
+ while i < len(value):
67
+ ch = value[i]
68
+ if ch == "'":
69
+ if pending_space and out:
70
+ out.append(" ")
71
+ pending_space = False
72
+ out.append(ch)
73
+ if quoted and i + 1 < len(value) and value[i + 1] == "'":
74
+ out.append("'"); i += 1
75
+ else:
76
+ quoted = not quoted
77
+ elif ch.isspace() and not quoted:
78
+ pending_space = True
79
+ else:
80
+ if pending_space and out:
81
+ out.append(" ")
82
+ pending_space = False
83
+ out.append(ch if quoted else ch.lower())
84
+ i += 1
85
+ result = "".join(out)
86
+ result = "now()" if result == "current_timestamp" else result
87
+ numeric_literal = re.fullmatch(r"'([+-]?(?:\d+(?:\.\d*)?|\.\d+))'", result)
88
+ return numeric_literal[1] if numeric_literal else result
89
+
90
+
91
+ def schema_from_models(models=None):
92
+ if models is None:
93
+ from pyzzel.core.base import Base
94
+ models = Base._registry
95
+ tables = {}
96
+ for table, model in models.items():
97
+ columns = {}
98
+ for spec in model._column_specs():
99
+ sql_type = canonical_type(spec.sql_type)
100
+ columns[spec.name] = {
101
+ "type": sql_type, "nullable": False if spec.primary_key else spec.nullable,
102
+ "primary_key": spec.primary_key, "identity": spec.identity, "unique": spec.unique,
103
+ "default": spec.default,
104
+ "references": list(spec.foreign_key) if spec.foreign_key else None,
105
+ "on_delete": spec.on_delete if spec.foreign_key else None,
106
+ }
107
+ indexes = [column.name for column in model._columns.values() if column.index]
108
+ tables[table] = {"columns": columns, "indexes": indexes}
109
+ return {"version": 1, "tables": tables}
110
+
111
+
112
+ def load_snapshot(path):
113
+ path = Path(path)
114
+ if not path.exists():
115
+ return {"version": 1, "tables": {}}
116
+ with path.open(encoding="utf-8") as stream:
117
+ snapshot = json.load(stream)
118
+ if snapshot.get("version") != 1:
119
+ raise MigrationError(f"Unsupported snapshot version: {snapshot.get('version')!r}")
120
+ return snapshot
121
+
122
+
123
+ def save_snapshot(path, snapshot):
124
+ path = Path(path)
125
+ path.parent.mkdir(parents=True, exist_ok=True)
126
+ content = json.dumps(snapshot, indent=2) + "\n"
127
+ fd, temporary = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.")
128
+ try:
129
+ with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as stream:
130
+ stream.write(content)
131
+ os.replace(temporary, path)
132
+ except BaseException:
133
+ try:
134
+ os.unlink(temporary)
135
+ except FileNotFoundError:
136
+ pass
137
+ raise
@@ -0,0 +1,79 @@
1
+ """Render offline schema operations as PostgreSQL DDL."""
2
+
3
+ from pyzzel.sql import (
4
+ ColumnSpec, check_default_expression, column_definition, create_index,
5
+ create_table, index_name, quote_ident,
6
+ )
7
+ from .diff import (
8
+ AddColumn, AddForeignKey, AddIndex, AddUnique, AlterType, CreateTable,
9
+ DropColumn, DropDefault, DropForeignKey, DropIndex, DropNotNull,
10
+ DropTable, DropUnique, SetDefault, SetNotNull,
11
+ )
12
+
13
+
14
+ def _constraint(table, column, suffix):
15
+ name = f"{table}_{column}_{suffix}"
16
+ return name[:63]
17
+
18
+
19
+ def _spec(name, column):
20
+ return ColumnSpec(
21
+ name, column["type"].upper(), primary_key=column.get("primary_key", False),
22
+ identity=column.get("identity", False), nullable=column.get("nullable", True),
23
+ unique=column.get("unique", False), default=column.get("default"),
24
+ foreign_key=tuple(column["references"]) if column.get("references") else None,
25
+ on_delete=column.get("on_delete"),
26
+ )
27
+
28
+
29
+ def render(ops):
30
+ statements = []
31
+ for op in ops:
32
+ marker = None
33
+ if isinstance(op, CreateTable):
34
+ specs = [_spec(name, column) for name, column in op.columns.items()]
35
+ statements.append(create_table(op.table, specs) + ";"); continue
36
+ if isinstance(op, DropTable):
37
+ marker, sql = "-- pyzzel:destructive", f"DROP TABLE IF EXISTS {quote_ident(op.table)}"
38
+ elif isinstance(op, AddColumn):
39
+ definition = column_definition(_spec(op.name, op.column))
40
+ if not op.column.get("nullable", True) and op.column.get("default") is None:
41
+ marker = "-- pyzzel:warning NOT NULL column without a default fails if the table already has rows"
42
+ sql = f"ALTER TABLE {quote_ident(op.table)} ADD COLUMN IF NOT EXISTS {definition}"
43
+ elif isinstance(op, DropColumn):
44
+ marker, sql = "-- pyzzel:destructive", f"ALTER TABLE {quote_ident(op.table)} DROP COLUMN IF EXISTS {quote_ident(op.name)}"
45
+ elif isinstance(op, AlterType):
46
+ marker = "-- pyzzel:destructive"
47
+ definition = column_definition(ColumnSpec("column", op.new.upper()))
48
+ sql_type = definition[len('"column" '):]
49
+ sql = f"ALTER TABLE {quote_ident(op.table)} ALTER COLUMN {quote_ident(op.name)} TYPE {sql_type}"
50
+ elif isinstance(op, SetNotNull):
51
+ marker = "-- pyzzel:warning fails if the column contains NULL values"
52
+ sql = f"ALTER TABLE {quote_ident(op.table)} ALTER COLUMN {quote_ident(op.name)} SET NOT NULL"
53
+ elif isinstance(op, DropNotNull):
54
+ sql = f"ALTER TABLE {quote_ident(op.table)} ALTER COLUMN {quote_ident(op.name)} DROP NOT NULL"
55
+ elif isinstance(op, SetDefault):
56
+ sql = f"ALTER TABLE {quote_ident(op.table)} ALTER COLUMN {quote_ident(op.name)} SET DEFAULT {check_default_expression(op.default)}"
57
+ elif isinstance(op, DropDefault):
58
+ sql = f"ALTER TABLE {quote_ident(op.table)} ALTER COLUMN {quote_ident(op.name)} DROP DEFAULT"
59
+ elif isinstance(op, AddUnique):
60
+ sql = f"ALTER TABLE {quote_ident(op.table)} ADD CONSTRAINT {quote_ident(_constraint(op.table, op.name, 'key'))} UNIQUE ({quote_ident(op.name)})"
61
+ elif isinstance(op, DropUnique):
62
+ sql = f"ALTER TABLE {quote_ident(op.table)} DROP CONSTRAINT IF EXISTS {quote_ident(_constraint(op.table, op.name, 'key'))}"
63
+ elif isinstance(op, AddForeignKey):
64
+ ref_table, ref_column = op.references
65
+ if op.on_delete not in ("NO ACTION", "RESTRICT", "CASCADE", "SET NULL"):
66
+ raise ValueError(f"Invalid ON DELETE action: {op.on_delete!r}")
67
+ sql = (f"ALTER TABLE {quote_ident(op.table)} ADD CONSTRAINT {quote_ident(_constraint(op.table, op.name, 'fkey'))} "
68
+ f"FOREIGN KEY ({quote_ident(op.name)}) REFERENCES {quote_ident(ref_table)} ({quote_ident(ref_column)})")
69
+ if op.on_delete != "NO ACTION": sql += f" ON DELETE {op.on_delete}"
70
+ elif isinstance(op, DropForeignKey):
71
+ sql = f"ALTER TABLE {quote_ident(op.table)} DROP CONSTRAINT IF EXISTS {quote_ident(_constraint(op.table, op.name, 'fkey'))}"
72
+ elif isinstance(op, AddIndex):
73
+ statements.append(create_index(op.table, op.name) + ";"); continue
74
+ elif isinstance(op, DropIndex):
75
+ sql = f"DROP INDEX IF EXISTS {quote_ident(index_name(op.table, op.name))}"
76
+ else:
77
+ raise TypeError(f"Unsupported migration operation: {op!r}")
78
+ statements.append((marker + "\n" if marker else "") + sql + ";")
79
+ return "\n\n".join(statements)
pyzzel/pool.py ADDED
@@ -0,0 +1,108 @@
1
+ """Connection pool wrapper around psycopg_pool."""
2
+
3
+ from contextlib import contextmanager
4
+ from contextvars import ContextVar
5
+
6
+ from pyzzel.errors import AlreadyConnectedError, NotConnectedError, PyzzelError
7
+
8
+
9
+ _pool = None
10
+ _current = ContextVar("pyzzel_current_connection", default=None)
11
+
12
+
13
+ def connect(
14
+ url: str,
15
+ *,
16
+ min_size: int = 1,
17
+ max_size: int = 10,
18
+ timeout: float = 30.0,
19
+ check: bool = False,
20
+ prepare_threshold: int | None = None,
21
+ ) -> None:
22
+ """Set check=True if your database may restart or drop idle connections; it costs one extra round trip per operation."""
23
+ global _pool
24
+ if _pool is not None:
25
+ raise AlreadyConnectedError()
26
+
27
+ import psycopg
28
+
29
+ try:
30
+ with psycopg.connect(
31
+ url,
32
+ connect_timeout=max(1, int(timeout)),
33
+ prepare_threshold=prepare_threshold,
34
+ ):
35
+ pass
36
+ except (psycopg.Error, OSError) as error:
37
+ raise PyzzelError("Could not connect to the database.") from error
38
+
39
+ from psycopg_pool import ConnectionPool
40
+
41
+ pool_options = {
42
+ "min_size": min_size,
43
+ "max_size": max_size,
44
+ "timeout": timeout,
45
+ "open": True,
46
+ "kwargs": {
47
+ "autocommit": True,
48
+ "prepare_threshold": prepare_threshold,
49
+ },
50
+ }
51
+ if check:
52
+ pool_options["check"] = ConnectionPool.check_connection
53
+ pool = ConnectionPool(url, **pool_options)
54
+ try:
55
+ pool.wait(timeout=timeout)
56
+ except Exception as error:
57
+ try:
58
+ pool.close(timeout=1)
59
+ except Exception:
60
+ pass
61
+ _pool = None
62
+ raise PyzzelError("Could not connect to the database.") from error
63
+ _pool = pool
64
+
65
+
66
+ def disconnect() -> None:
67
+ """Close the process-wide pool if it is open."""
68
+ global _pool
69
+ if _pool is not None:
70
+ pool = _pool
71
+ _pool = None
72
+ pool.close()
73
+
74
+
75
+ @contextmanager
76
+ def connection():
77
+ """Borrow a pooled connection, or reuse the active transaction connection."""
78
+ current = _current.get()
79
+ if current is not None:
80
+ yield current
81
+ return
82
+ if _pool is None:
83
+ raise NotConnectedError()
84
+ with _pool.connection() as conn:
85
+ yield conn
86
+
87
+
88
+ @contextmanager
89
+ def transaction():
90
+ """Run a transaction; nested blocks use savepoints."""
91
+ if _pool is None:
92
+ raise NotConnectedError()
93
+ conn = _current.get()
94
+ if conn is not None:
95
+ with conn.transaction():
96
+ yield
97
+ return
98
+ with _pool.connection() as conn:
99
+ with conn.transaction():
100
+ token = _current.set(conn)
101
+ try:
102
+ yield
103
+ finally:
104
+ _current.reset(token)
105
+
106
+
107
+ def in_transaction() -> bool:
108
+ return _current.get() is not None
pyzzel/sql/__init__.py ADDED
@@ -0,0 +1,50 @@
1
+ """SQL builder. All PostgreSQL-specific SQL lives here."""
2
+
3
+ from .builder import (
4
+ ColumnSpec,
5
+ ColumnRef,
6
+ Join,
7
+ Statement,
8
+ count,
9
+ create_table,
10
+ create_index,
11
+ column_definition,
12
+ index_name,
13
+ check_default_expression,
14
+ delete,
15
+ insert,
16
+ insert_many,
17
+ quote_ident,
18
+ select_by_pk,
19
+ select,
20
+ update,
21
+ update_where,
22
+ delete_where,
23
+ )
24
+ from .conditions import Condition, OrderBy, and_, or_
25
+
26
+ __all__ = [
27
+ "Statement",
28
+ "ColumnSpec",
29
+ "ColumnRef",
30
+ "Join",
31
+ "Condition",
32
+ "OrderBy",
33
+ "and_",
34
+ "or_",
35
+ "select",
36
+ "count",
37
+ "quote_ident",
38
+ "insert",
39
+ "insert_many",
40
+ "select_by_pk",
41
+ "update",
42
+ "update_where",
43
+ "delete_where",
44
+ "delete",
45
+ "create_table",
46
+ "create_index",
47
+ "column_definition",
48
+ "index_name",
49
+ "check_default_expression",
50
+ ]