codecop 0.1.0__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.
- codecop/__init__.py +7 -0
- codecop/access.py +140 -0
- codecop/access_postgres.py +284 -0
- codecop/advisor.py +1512 -0
- codecop/advisor_bench.py +493 -0
- codecop/advisor_ch.py +2080 -0
- codecop/advisor_view.py +381 -0
- codecop/advisors.py +184 -0
- codecop/agent.py +221 -0
- codecop/agentfacts.py +745 -0
- codecop/apply/__init__.py +327 -0
- codecop/apply/check.py +344 -0
- codecop/apply/cli.py +162 -0
- codecop/apply/clickhouse.py +379 -0
- codecop/apply/common.py +238 -0
- codecop/apply/html.py +264 -0
- codecop/apply/patch.py +213 -0
- codecop/apply/postgres.py +102 -0
- codecop/apply/trial.py +166 -0
- codecop/assemble.py +104 -0
- codecop/baseline.py +1043 -0
- codecop/bench.py +2464 -0
- codecop/blockers.py +935 -0
- codecop/budget.py +980 -0
- codecop/catalog_profile.py +525 -0
- codecop/changed.py +261 -0
- codecop/cicmd.py +1002 -0
- codecop/cli.py +5421 -0
- codecop/client.py +729 -0
- codecop/coverage.py +1067 -0
- codecop/coverage_reasons.py +116 -0
- codecop/crashlog.py +70 -0
- codecop/datagen.py +766 -0
- codecop/datagen_postgres.py +630 -0
- codecop/datagen_sqlite.py +323 -0
- codecop/dbcli.py +885 -0
- codecop/definitions.py +457 -0
- codecop/discover/__init__.py +702 -0
- codecop/discover/arms.py +160 -0
- codecop/discover/base.py +234 -0
- codecop/discover/dialect.py +296 -0
- codecop/discover/elixir.py +64 -0
- codecop/discover/engine_go.py +320 -0
- codecop/discover/go.py +1947 -0
- codecop/discover/java.py +1528 -0
- codecop/discover/loops.py +1671 -0
- codecop/discover/named_go.py +258 -0
- codecop/discover/origins.py +4565 -0
- codecop/discover/origins_ts.py +1584 -0
- codecop/discover/orm_django.py +2772 -0
- codecop/discover/orm_django_models.py +814 -0
- codecop/discover/orm_django_sql.py +662 -0
- codecop/discover/orm_ent.py +565 -0
- codecop/discover/orm_ex_ecto.py +2138 -0
- codecop/discover/orm_go.py +2134 -0
- codecop/discover/orm_go_bun.py +602 -0
- codecop/discover/orm_go_run.py +636 -0
- codecop/discover/orm_go_types.py +1082 -0
- codecop/discover/orm_java.py +1450 -0
- codecop/discover/orm_jdbi.py +743 -0
- codecop/discover/orm_jooq.py +806 -0
- codecop/discover/orm_jpa.py +1307 -0
- codecop/discover/orm_mybatis.py +730 -0
- codecop/discover/orm_py.py +557 -0
- codecop/discover/orm_py_peewee.py +1266 -0
- codecop/discover/orm_py_prisma.py +298 -0
- codecop/discover/orm_rb_activerecord.py +1906 -0
- codecop/discover/orm_rust.py +1248 -0
- codecop/discover/orm_rust_seaorm.py +734 -0
- codecop/discover/orm_sqlalchemy.py +2475 -0
- codecop/discover/orm_stats.py +307 -0
- codecop/discover/orm_tables.py +29 -0
- codecop/discover/orm_ts.py +503 -0
- codecop/discover/orm_ts_drizzle.py +697 -0
- codecop/discover/orm_ts_knex.py +1117 -0
- codecop/discover/orm_ts_kysely.py +861 -0
- codecop/discover/orm_ts_prisma.py +1045 -0
- codecop/discover/orm_ts_schema.py +169 -0
- codecop/discover/orm_ts_sequelize.py +1401 -0
- codecop/discover/orm_ts_typeorm.py +1067 -0
- codecop/discover/probes.py +323 -0
- codecop/discover/python.py +595 -0
- codecop/discover/ruby.py +793 -0
- codecop/discover/rust.py +2378 -0
- codecop/discover/schema.py +520 -0
- codecop/discover/schema_ent.py +145 -0
- codecop/discover/schema_ts.py +420 -0
- codecop/discover/tsresolve.py +257 -0
- codecop/discover/typescript.py +1728 -0
- codecop/discover/xml_mapper.py +526 -0
- codecop/engines/__init__.py +515 -0
- codecop/engines/base.py +498 -0
- codecop/engines/clickhouse.py +1558 -0
- codecop/engines/pg_ddl.py +444 -0
- codecop/engines/pg_hints.py +137 -0
- codecop/engines/postgres.py +1060 -0
- codecop/engines/sqlite.py +742 -0
- codecop/families/__init__.py +3 -0
- codecop/families/build.py +280 -0
- codecop/families/cli.py +177 -0
- codecop/families/coverage_hook.py +62 -0
- codecop/families/driver.py +221 -0
- codecop/families/goslice.py +229 -0
- codecop/families/goslice_tool/go.mod +3 -0
- codecop/families/goslice_tool/main.go +403 -0
- codecop/families/holes.py +451 -0
- codecop/families/images/Dockerfile.go +5 -0
- codecop/families/images/Dockerfile.python +4 -0
- codecop/families/images/Dockerfile.rust +4 -0
- codecop/families/isolate.py +171 -0
- codecop/families/locate.py +270 -0
- codecop/families/model.py +301 -0
- codecop/families/plangate.py +271 -0
- codecop/families/tagpatch.py +174 -0
- codecop/fidelity.py +671 -0
- codecop/fixgroups.py +130 -0
- codecop/formerly.py +91 -0
- codecop/grantscript.py +254 -0
- codecop/groupbench.py +1375 -0
- codecop/groups.py +594 -0
- codecop/groups_view.py +186 -0
- codecop/heat.py +1405 -0
- codecop/heatcli.py +219 -0
- codecop/joins.py +1749 -0
- codecop/joins_view.py +127 -0
- codecop/kinds/__init__.py +507 -0
- codecop/kinds/calls/__init__.py +501 -0
- codecop/kinds/calls/extract.py +1097 -0
- codecop/kinds/calls/harness.py +408 -0
- codecop/kinds/calls/measure.py +342 -0
- codecop/kinds/calls/rules.py +139 -0
- codecop/kinds/calls/runners/RunCalls.java +167 -0
- codecop/kinds/calls/runners/run_go.go +141 -0
- codecop/kinds/calls/runners/run_node.js +186 -0
- codecop/kinds/calls/runners/run_python.py +319 -0
- codecop/kinds/calls/runners/run_rust.rs +200 -0
- codecop/kinds/calls/standin.py +340 -0
- codecop/kinds/pipeline.py +288 -0
- codecop/kinds/regex/__init__.py +1167 -0
- codecop/kinds/regex/engines/RegexHarness.java +129 -0
- codecop/kinds/regex/engines/run_go.go +294 -0
- codecop/kinds/regex/engines/run_node.js +99 -0
- codecop/kinds/regex/engines/run_python.py +295 -0
- codecop/kinds/regex/engines/run_rust.rs +149 -0
- codecop/kinds/regex/extract.py +1438 -0
- codecop/kinds/regex/harness.py +235 -0
- codecop/kinds/regex/measure.py +270 -0
- codecop/kinds/regex/shapes.py +418 -0
- codecop/kinds/regex/syntax.py +1721 -0
- codecop/kinds/testkind.py +107 -0
- codecop/kinds/transaction/__init__.py +228 -0
- codecop/kinds/transaction/bind.py +355 -0
- codecop/kinds/transaction/measure.py +264 -0
- codecop/kinds/transaction/plan.py +462 -0
- codecop/kinds/transaction/rules.py +540 -0
- codecop/kinds/transaction/sandbox.py +94 -0
- codecop/kinds/transaction/scenarios.py +520 -0
- codecop/kinds/transaction/schedule.py +483 -0
- codecop/kinds/transaction/shapes.py +418 -0
- codecop/kinds/transaction/verify.py +293 -0
- codecop/live.py +237 -0
- codecop/loopbench.py +2071 -0
- codecop/loopschema.py +591 -0
- codecop/mcpserver.py +120 -0
- codecop/mechanism.py +214 -0
- codecop/members.py +200 -0
- codecop/multi.py +443 -0
- codecop/observed.py +1868 -0
- codecop/parts.py +102 -0
- codecop/patterns.py +830 -0
- codecop/patterns_view.py +559 -0
- codecop/plangate.py +242 -0
- codecop/profile.py +439 -0
- codecop/prompts/areas.md +19 -0
- codecop/prompts/observed_variants.md +20 -0
- codecop/prompts/profile.md +60 -0
- codecop/prompts/profile_postgres.md +68 -0
- codecop/prompts/profile_sizes.md +26 -0
- codecop/prompts/profile_sqlite.md +69 -0
- codecop/prompts/rank.md +84 -0
- codecop/prompts/rank_postgres.md +96 -0
- codecop/prompts/rank_sqlite.md +94 -0
- codecop/prompts/scenario_variants.md +20 -0
- codecop/rank.py +1473 -0
- codecop/realdb/__init__.py +688 -0
- codecop/realdb/clickhouse.py +1223 -0
- codecop/realdb/facts.py +815 -0
- codecop/realdb/locality.py +964 -0
- codecop/realdb/locks.py +355 -0
- codecop/realdb/match.py +531 -0
- codecop/realdb/postgres.py +643 -0
- codecop/realdb/ratios.py +77 -0
- codecop/realdb/sample.py +126 -0
- codecop/relations.py +227 -0
- codecop/report.py +1854 -0
- codecop/resources.py +332 -0
- codecop/resources_view.py +266 -0
- codecop/rules/entry_points.toml +1013 -0
- codecop/rules/sinks.toml +362 -0
- codecop/runsonce.py +8 -0
- codecop/sandboxschema.py +555 -0
- codecop/scenario_agent.py +172 -0
- codecop/scenario_bind.py +507 -0
- codecop/scenario_sql.py +971 -0
- codecop/scenarios.py +675 -0
- codecop/scenarios_view.py +274 -0
- codecop/schemacli.py +67 -0
- codecop/schemas/areas_output.schema.json +21 -0
- codecop/schemas/profile_output.schema.json +362 -0
- codecop/schemas/rank_output.schema.json +385 -0
- codecop/sizes.py +200 -0
- codecop/sqlinfo.py +558 -0
- codecop/typedparams.py +576 -0
- codecop/verdicts.py +212 -0
- codecop/writebench.py +720 -0
- codecop/writecost.py +440 -0
- codecop/writes.py +1208 -0
- codecop-0.1.0.dist-info/METADATA +42 -0
- codecop-0.1.0.dist-info/RECORD +222 -0
- codecop-0.1.0.dist-info/WHEEL +4 -0
- codecop-0.1.0.dist-info/entry_points.txt +2 -0
- codecop-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""Tables that TypeScript projects define through a builder: Kysely's `db.schema.createTable(...)`,
|
|
2
|
+
`alterTable(...).addColumn(...)` and `createIndex(...).on(...)` (migrations written in TypeScript).
|
|
3
|
+
|
|
4
|
+
`schema_ts.find_tables` offers every source file that mentions one of those calls; `tables()` returns the
|
|
5
|
+
result with the columns added by later `alterTable` calls and the indexes attached. A `down()` function undoes
|
|
6
|
+
a migration and is not read.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from tree_sitter import Node, Parser
|
|
12
|
+
|
|
13
|
+
from . import orm_ts as o
|
|
14
|
+
from .orm_ts import txt, unwrap
|
|
15
|
+
|
|
16
|
+
HEADS = ("createTable", "alterTable", "createIndex")
|
|
17
|
+
TRIGGER = (b"createTable(", b"alterTable(", b"createIndex(")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def spine(node: Node | None) -> tuple[Node | None, list[tuple[str, list[Node], Node]]]:
|
|
21
|
+
"""A call chain `base.a(x).b(y)` as (base, [(a, [x], call), (b, [y], call)])."""
|
|
22
|
+
steps: list[tuple[str, list[Node], Node]] = []
|
|
23
|
+
cur = unwrap(node)
|
|
24
|
+
while cur is not None and cur.type == "call_expression":
|
|
25
|
+
fn = unwrap(cur.child_by_field_name("function"))
|
|
26
|
+
args = cur.child_by_field_name("arguments")
|
|
27
|
+
if fn is None or fn.type != "member_expression" or args is None or args.type != "arguments":
|
|
28
|
+
break
|
|
29
|
+
steps.append((txt(fn.child_by_field_name("property")), [a for a in args.named_children if a.type != "comment"], cur))
|
|
30
|
+
cur = unwrap(fn.child_by_field_name("object"))
|
|
31
|
+
steps.reverse()
|
|
32
|
+
return cur, steps
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def climb(call: Node) -> Node:
|
|
36
|
+
"""The outermost call of the chain a call starts: `x.a()` -> `x.a().b().c()`."""
|
|
37
|
+
cur = call
|
|
38
|
+
while True:
|
|
39
|
+
p = cur.parent
|
|
40
|
+
while p is not None and p.type in ("parenthesized_expression", "non_null_expression"):
|
|
41
|
+
cur, p = p, p.parent
|
|
42
|
+
if p is None or p.type != "member_expression" or p.child_by_field_name("object") is None or \
|
|
43
|
+
p.child_by_field_name("object").id != cur.id:
|
|
44
|
+
return cur
|
|
45
|
+
pp = p.parent
|
|
46
|
+
if pp is None or pp.type != "call_expression" or pp.child_by_field_name("function").id != p.id:
|
|
47
|
+
return cur
|
|
48
|
+
cur = pp
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def in_down(node: Node) -> bool:
|
|
52
|
+
n = node.parent
|
|
53
|
+
while n is not None:
|
|
54
|
+
if n.type in o.FUNC_TYPES:
|
|
55
|
+
from .typescript import func_name
|
|
56
|
+
if func_name(n) in ("down", "rollback"):
|
|
57
|
+
return True
|
|
58
|
+
n = n.parent
|
|
59
|
+
return False
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class KyselySchema:
|
|
63
|
+
def __init__(self):
|
|
64
|
+
self.created: list[tuple[str, int, str, list[dict], list[str], list[dict]]] = [] # file, line, table, columns, pk, indexes
|
|
65
|
+
self.altered: list[tuple[str, str, list[dict], list[str]]] = [] # file, table, added columns, dropped
|
|
66
|
+
self.indexes: list[tuple[str, str, dict]] = [] # file, table, index
|
|
67
|
+
|
|
68
|
+
@staticmethod
|
|
69
|
+
def wants(src: bytes) -> bool:
|
|
70
|
+
return any(t in src for t in TRIGGER) and (b"schema" in src)
|
|
71
|
+
|
|
72
|
+
def add(self, rel: str, src: bytes) -> None:
|
|
73
|
+
from .typescript import language_for
|
|
74
|
+
tree = Parser(language_for(rel)).parse(src)
|
|
75
|
+
stack = [tree.root_node]
|
|
76
|
+
while stack:
|
|
77
|
+
n = stack.pop()
|
|
78
|
+
stack.extend(n.children)
|
|
79
|
+
if n.type != "call_expression":
|
|
80
|
+
continue
|
|
81
|
+
fn = unwrap(n.child_by_field_name("function"))
|
|
82
|
+
if fn is None or fn.type != "member_expression" or txt(fn.child_by_field_name("property")) not in HEADS:
|
|
83
|
+
continue
|
|
84
|
+
head = txt(fn.child_by_field_name("property"))
|
|
85
|
+
args = n.child_by_field_name("arguments")
|
|
86
|
+
first = args.named_children[0] if args is not None and args.named_children else None
|
|
87
|
+
name = o.string_value(first)
|
|
88
|
+
if name is None or in_down(n):
|
|
89
|
+
continue
|
|
90
|
+
_base, steps = spine(climb(n))
|
|
91
|
+
names = [s[0] for s in steps]
|
|
92
|
+
at = names.index(head) if head in names else 0
|
|
93
|
+
steps = steps[at + 1:]
|
|
94
|
+
if head == "createIndex":
|
|
95
|
+
table, cols, unique = None, [], False
|
|
96
|
+
for sname, sargs, _ in steps:
|
|
97
|
+
if sname == "on" and sargs:
|
|
98
|
+
table = o.string_value(sargs[0])
|
|
99
|
+
elif sname == "column" and sargs:
|
|
100
|
+
c = o.string_value(sargs[0])
|
|
101
|
+
cols += [c] if c else []
|
|
102
|
+
elif sname == "columns" and sargs:
|
|
103
|
+
cols += [c for c in (o.string_value(i) for i in (o.array_items(sargs[0]) or [])) if c]
|
|
104
|
+
elif sname == "unique":
|
|
105
|
+
unique = True
|
|
106
|
+
if table and cols:
|
|
107
|
+
self.indexes.append((rel, table, {"name": name, "columns": [c.split(" ")[0] for c in cols], "unique": unique}))
|
|
108
|
+
continue
|
|
109
|
+
columns, pk, indexes, dropped = [], [], [], []
|
|
110
|
+
for sname, sargs, _ in steps:
|
|
111
|
+
if sname == "addColumn" and sargs:
|
|
112
|
+
cname = o.string_value(sargs[0])
|
|
113
|
+
if not cname:
|
|
114
|
+
continue
|
|
115
|
+
ctype = o.string_value(sargs[1]) if len(sargs) > 1 else None
|
|
116
|
+
if ctype is None and len(sargs) > 1:
|
|
117
|
+
ctype = o.norm_expr(txt(sargs[1]))[:60]
|
|
118
|
+
mods = txt(sargs[2]) if len(sargs) > 2 else ""
|
|
119
|
+
columns.append({"name": cname, "type": (ctype or "") + (" NOT NULL" if "notNull" in mods or "primaryKey" in mods else "")})
|
|
120
|
+
if "primaryKey" in mods:
|
|
121
|
+
pk.append(cname)
|
|
122
|
+
if ".unique(" in mods:
|
|
123
|
+
indexes.append({"name": "", "columns": [cname], "unique": True})
|
|
124
|
+
elif sname in ("addPrimaryKeyConstraint", "addUniqueConstraint") and len(sargs) > 1:
|
|
125
|
+
cols = [c for c in (o.string_value(i) for i in (o.array_items(sargs[1]) or [])) if c]
|
|
126
|
+
if sname == "addPrimaryKeyConstraint":
|
|
127
|
+
pk = cols
|
|
128
|
+
elif cols:
|
|
129
|
+
indexes.append({"name": o.string_value(sargs[0]) or "", "columns": cols, "unique": True})
|
|
130
|
+
elif sname == "dropColumn" and sargs:
|
|
131
|
+
c = o.string_value(sargs[0])
|
|
132
|
+
dropped += [c] if c else []
|
|
133
|
+
if head == "createTable" and columns:
|
|
134
|
+
self.created.append((rel, n.start_point[0] + 1, name, columns, pk, indexes))
|
|
135
|
+
elif head == "alterTable" and (columns or dropped):
|
|
136
|
+
self.altered.append((rel, name, columns, dropped))
|
|
137
|
+
|
|
138
|
+
def tables(self) -> list:
|
|
139
|
+
from .schema_ts import TsTable
|
|
140
|
+
|
|
141
|
+
def near(rel: str, cands: list) -> object | None:
|
|
142
|
+
"""The table of that name defined nearest to a file (most path in common)."""
|
|
143
|
+
best, score = None, -1
|
|
144
|
+
for t, trel in cands:
|
|
145
|
+
a, b = rel.split("/"), trel.split("/")
|
|
146
|
+
common = next((i for i, (x, y) in enumerate(zip(a, b)) if x != y), min(len(a), len(b)))
|
|
147
|
+
if common > score:
|
|
148
|
+
best, score = t, common
|
|
149
|
+
return best
|
|
150
|
+
|
|
151
|
+
by_name: dict[str, list] = {}
|
|
152
|
+
out = []
|
|
153
|
+
for rel, line, name, columns, pk, indexes in self.created:
|
|
154
|
+
t = TsTable(name=name, defined_at=[f"{rel}:{line}"], columns=[dict(c) for c in columns],
|
|
155
|
+
primary_key=", ".join(pk), indexes=[dict(i) for i in indexes])
|
|
156
|
+
by_name.setdefault(name, []).append((t, rel))
|
|
157
|
+
out.append(t)
|
|
158
|
+
for rel, name, columns, dropped in sorted(self.altered, key=lambda a: a[0]):
|
|
159
|
+
t = near(rel, by_name.get(name, []))
|
|
160
|
+
if t is None:
|
|
161
|
+
continue
|
|
162
|
+
have = {c["name"] for c in t.columns}
|
|
163
|
+
t.columns += [dict(c) for c in columns if c["name"] not in have]
|
|
164
|
+
t.columns = [c for c in t.columns if c["name"] not in dropped]
|
|
165
|
+
for rel, name, index in self.indexes:
|
|
166
|
+
t = near(rel, by_name.get(name, []))
|
|
167
|
+
if t is not None and index not in t.indexes:
|
|
168
|
+
t.indexes.append(index)
|
|
169
|
+
return out
|