@neralabs/rerail-pack-alembic 0.0.0-stage → 0.1.0
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.
- package/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +448 -2
- package/pack.json +192 -0
- package/package.json +36 -4
- package/rules/alembic-practice-concurrent-index-outside-autocommit.rerail +134 -0
- package/rules/alembic-practice-empty-downgrade.rerail +181 -0
- package/rules/alembic-practice-model-import-in-migration.rerail +213 -0
- package/rules/alembic-practice-multiple-heads.rerail +242 -0
- package/rules/lib/alembic.rerail +814 -0
|
@@ -0,0 +1,814 @@
|
|
|
1
|
+
// lib/alembic: Alembic migrations, the SQLAlchemy models they change, and the env.py that runs them, as the
|
|
2
|
+
// alembic-practice rules read them. Names resolve through imports (python/names' sym_of), so `from alembic import op`
|
|
3
|
+
// makes `op.create_index(...)` an Alembic operation and `import sqlalchemy as sa` makes `sa.Column(...)` a Column.
|
|
4
|
+
// What crosses files is read by symbol, path or document, never by another file's node (decisions 0677, 0691): a
|
|
5
|
+
// module's constants, types, test functions and classes are stated in their own file under their qualified names,
|
|
6
|
+
// joined across files by those names (the cross_file relations below), and a rule reads the answer back at its own
|
|
7
|
+
// node by that node's term (al_term). A model's ancestry is python/names' class_anc (the phase py_names), so a column
|
|
8
|
+
// a mixin in another module declares still belongs to the model.
|
|
9
|
+
//
|
|
10
|
+
// Interface
|
|
11
|
+
// Files: al_migration_doc(d) document d is an Alembic revision: module-level `revision = ...` and
|
|
12
|
+
// `down_revision = ...`
|
|
13
|
+
// al_env_doc(d) document d is an env.py that reads alembic.context
|
|
14
|
+
// al_test_doc(d) document d is test code (tests/test/testing directories, test_*.py,
|
|
15
|
+
// *_test.py, conftest.py)
|
|
16
|
+
// al_const_def(d, q, v) document d binds module-level name q (`pkg.mod.NAME`) to v
|
|
17
|
+
// al_term(n, d, t) node n of document d is known by the term t across files (pt_tm)
|
|
18
|
+
// Revisions: al_revision(d, r) revision d's own id r (the `revision` string)
|
|
19
|
+
// al_down_revision(d, r) revision d revises r: `down_revision`'s string, or each string of a
|
|
20
|
+
// merge revision's tuple
|
|
21
|
+
// al_branch_labeled(d) revision d sets `branch_labels` to something other than None
|
|
22
|
+
// al_parent(n, m) revision n (outside test code) names revision m in its down_revision:
|
|
23
|
+
// m in n's directory first, else m anywhere in the run (trees that reuse
|
|
24
|
+
// ids stay apart); cross_file, by revision id and directory
|
|
25
|
+
// al_ancestor(n, m) revision m is a strict ancestor of n (n descends from m)
|
|
26
|
+
// al_head(h) revision h (outside test code) is no other revision's parent
|
|
27
|
+
// al_top_assign(d, x, v) document d's module level assigns name x the value v
|
|
28
|
+
// Steps: al_step(f, s) f is the migration's module-level `def upgrade()` (s "upgrade") or
|
|
29
|
+
// `def downgrade()` (s "downgrade")
|
|
30
|
+
// al_in_step(n, f) n runs when step f runs: inside f, or inside a module-level function of
|
|
31
|
+
// the same file that f calls by name (one level)
|
|
32
|
+
// Operations: al_call(c, m, form) call c is Alembic operation m: `op.m(...)` (form "op") or `b.m(...)` on
|
|
33
|
+
// the handle b of a `with op.batch_alter_table(...) as b:` around it
|
|
34
|
+
// (form "batch")
|
|
35
|
+
// al_role(c, role, e) e is operation c's argument for role "table", "name", "column",
|
|
36
|
+
// "column_name", "columns", "referent" or "new_table", positional or by
|
|
37
|
+
// keyword, as each operation's signature places it (al_role_at)
|
|
38
|
+
// al_table(c, t) operation c acts on the table named t: its table argument, or the
|
|
39
|
+
// batch_alter_table's (a literal, `op.f("...")`, or a module constant)
|
|
40
|
+
// al_batch_with(w, v, tn, b) with-statement w opens `op.batch_alter_table(tn, ...)` as v; b its body
|
|
41
|
+
// al_creates_table(f, t) step f creates table t: `op.create_table("t", ...)`, a project's own
|
|
42
|
+
// `create_table("t", ...)` wrapper, or `op.execute("CREATE TABLE t ...")`
|
|
43
|
+
// al_empties_table(f, t) step f deletes every row of t (`DELETE FROM t` without WHERE, TRUNCATE)
|
|
44
|
+
// al_on_existing(c, m, t, f) operation m (c) in upgrade step f acts on table t, which f neither
|
|
45
|
+
// creates nor empties: a table that may hold rows in production
|
|
46
|
+
// al_forwarded(c, m, t, ci, f) call c in upgrade step f calls a function of the same file that runs
|
|
47
|
+
// operation m (ci) on the table its parameter names; c passes existing
|
|
48
|
+
// table t (one call deep). Another module's: al_fwd_call (the call) and
|
|
49
|
+
// al_fwd_op_at (the function, by qualified name)
|
|
50
|
+
// al_added_ddl(c, t, k, f) add_column c makes Alembic emit a second statement on existing table t:
|
|
51
|
+
// k "unique", "index", "foreign_key" or "check" (Alembic 1.20 renders all);
|
|
52
|
+
// al_added_ddl_if(c, t, k, f, q) when imported constant q is True
|
|
53
|
+
// al_exec_sql(f, y) step f runs op.execute with the literal SQL y, normalised
|
|
54
|
+
// al_kw(c, k, v) call c passes keyword k=v
|
|
55
|
+
// al_bool(v, b) v is statically b ("true", "false", "none") in its own file: a literal or
|
|
56
|
+
// a module constant of the same file
|
|
57
|
+
// al_true_kw(c, k), al_false_kw(c, k) call c passes k= True / False by value, in its own file
|
|
58
|
+
// al_const_bool(q, b) module constant q is statically b (cross_file, by qualified name)
|
|
59
|
+
// al_kw_at(d, t, k), al_flag_t(d, t, k, b) the operation, Column or configure() call of term t
|
|
60
|
+
// passes k=, and k= something statically b, of its file or imported
|
|
61
|
+
// (al_kw_ext(d, t, k, b): imported only)
|
|
62
|
+
// Transactions: al_autocommit_with(w, b) with-statement w opens `...autocommit_block()`; b is its body
|
|
63
|
+
// al_in_autocommit(n) n runs inside an autocommit block (outside the migration transaction)
|
|
64
|
+
// Dialects: al_non_pg(n) n sits in a branch that runs only on a database other than PostgreSQL,
|
|
65
|
+
// read by the test's structure (al_db_if): comparisons with a database
|
|
66
|
+
// name literal, isinstance() on SQLAlchemy dialect classes, not/and/or,
|
|
67
|
+
// test functions of the same file, names assigned a test; the else of a
|
|
68
|
+
// PostgreSQL test, the body of another database's test, and code after
|
|
69
|
+
// `if not _is_mysql(): return`
|
|
70
|
+
// al_np_ext(d, t) the call of term t is in such a branch by a test function of another
|
|
71
|
+
// module (al_fn_db, cross_file by qualified name)
|
|
72
|
+
// Types: al_type_class_t(d, t, n) type expression t's class, lowercased, resolved through imports,
|
|
73
|
+
// aliases, TypeDecorator `impl`, SQLAlchemy subclasses and functions that
|
|
74
|
+
// return a type (`Text().with_variant(...)`), of its file or another
|
|
75
|
+
// module (al_qtype, cross_file by qualified name)
|
|
76
|
+
// al_pg_type_t(d, t, k) the PostgreSQL type k t renders ("varchar", "text", "integer", "bigint",
|
|
77
|
+
// "timestamp", "timestamptz", "uuid", "jsonb", ...); al_mysql_type_t(d, t)
|
|
78
|
+
// Columns: al_column_call(c) call c builds a sqlalchemy Column (`sa.Column`, `Column`, sqlmodel's)
|
|
79
|
+
// al_column_name(c, x) Column c's name: its first positional argument
|
|
80
|
+
// al_added_column(c, col) add_column operation c adds the Column call col (inline, or a name
|
|
81
|
+
// bound to it in the same scope)
|
|
82
|
+
// al_name_value(e, s) expression e names the string s: a literal, `op.f("s")`, or a module
|
|
83
|
+
// constant bound to a literal
|
|
84
|
+
// Models: al_model(k, t) class k maps table t: `__tablename__ = "t"` in its body, or a sqlmodel
|
|
85
|
+
// `class K(..., table=True)` without one (table: the lowercased name)
|
|
86
|
+
// al_model_q(q) class q of the run is an al_model (cross_file, by qualified name)
|
|
87
|
+
// al_class_column(k, x, s) class k's own body maps column x at statement s: `a = Column(...)`,
|
|
88
|
+
// `a = mapped_column(...)`, `a: Mapped[T] = mapped_column(...)`, a bare
|
|
89
|
+
// `a: Mapped[T]`, or (sqlmodel) an annotated field of a table class; x is
|
|
90
|
+
// the Column's explicit name when it has one, else the attribute
|
|
91
|
+
// al_sqlmodel_field_at(d, q, x) class q's annotated field x, which a sqlmodel table subclass maps
|
|
92
|
+
// env.py: al_env_doc(d) document d is an env.py naming alembic.context (a name or an attribute)
|
|
93
|
+
// al_env_configure(c) call c is `context.configure(...)` in env.py
|
|
94
|
+
// al_env_tree(d, e) revision d sits under the directory of env.py e (a repository can hold
|
|
95
|
+
// several Alembic trees, each with its own env.py: dispatch's core and
|
|
96
|
+
// tenant schemas); cross_file, by document
|
|
97
|
+
// al_in_some_tree(d) revision d sits under some env.py's directory
|
|
98
|
+
// al_env_reads(e, d) env.py e reads its settings from d: itself, a module of the run it
|
|
99
|
+
// imports a name from (`from app.db import engine`), or a module it
|
|
100
|
+
// imports whole (`from app import db`, then `db.engine`); cross_file
|
|
101
|
+
//
|
|
102
|
+
// Alembic background
|
|
103
|
+
// - Each revision runs `upgrade()` in one transaction on PostgreSQL (transactional DDL) unless env.py turns it off;
|
|
104
|
+
// `with op.get_context().autocommit_block():` commits what ran before it and runs its body outside any transaction,
|
|
105
|
+
// which CREATE/DROP INDEX CONCURRENTLY and (before PG 12) ALTER TYPE ... ADD VALUE need.
|
|
106
|
+
// - `with op.batch_alter_table("t") as batch_op:` names the table once; `batch_op.create_index("ix", ["c"])` acts on t.
|
|
107
|
+
// On PostgreSQL a batch emits the same ALTER statements as `op.*` (recreate="auto" only copies tables on SQLite).
|
|
108
|
+
// - A table the same `upgrade()` creates is empty, so locks on it cost nothing: every "existing table" rule leaves
|
|
109
|
+
// its operations alone. A table created by an earlier revision of the same release is not seen as new (gap).
|
|
110
|
+
// - Projects that run on several databases branch on the dialect; only PostgreSQL's branch is this pack's concern.
|
|
111
|
+
// e.g. from alembic import op
|
|
112
|
+
// import sqlalchemy as sa
|
|
113
|
+
// def upgrade(): al_step(<upgrade>, "upgrade")
|
|
114
|
+
// op.create_table("tags", sa.Column("id", ...)) al_creates_table(<upgrade>, "tags")
|
|
115
|
+
// op.create_index("ix_tags_id", "tags", ["id"]) al_call(_, "create_index", "op"), al_table(_, "tags"),
|
|
116
|
+
// not al_on_existing (tags is new)
|
|
117
|
+
// with op.batch_alter_table("users") as b: al_batch_with(_, "b", <"users">, _)
|
|
118
|
+
// b.add_column(sa.Column("x", sa.Integer())) al_call(_, "add_column", "batch"), al_table(_, "users"),
|
|
119
|
+
// al_on_existing(_, "add_column", "users", <upgrade>),
|
|
120
|
+
// al_added_column(_, <sa.Column(...)>)
|
|
121
|
+
// class User(Base): al_model(<User>, "users")
|
|
122
|
+
// __tablename__ = "users"
|
|
123
|
+
// email: Mapped[str] = mapped_column(String(255)) al_class_column(<User>, "email", _)
|
|
124
|
+
#include "python/ast.rerail"
|
|
125
|
+
#include "python/calls.rerail" // call_arg, posarg: its syntax helpers only
|
|
126
|
+
|
|
127
|
+
// ---- files -------------------------------------------------------------------------------------------------------
|
|
128
|
+
// al_top_assign(d, x, v): `x = v` (or `x: T = v`) at module level of document d.
|
|
129
|
+
.decl al_top_assign(d: number, x: symbol, v: node)
|
|
130
|
+
al_top_assign(d, x, v) :- kind(a, "assignment"), field(a, "left", l), kind(l, "identifier"), text(l, x),
|
|
131
|
+
field(a, "right", v), nearest(a, m, scope), kind(m, "module"), docof(a, d).
|
|
132
|
+
.decl al_migration_doc(d: number)
|
|
133
|
+
al_migration_doc(d) :- al_top_assign(d, "revision", _), al_top_assign(d, "down_revision", _).
|
|
134
|
+
.decl al_env_doc(d: number)
|
|
135
|
+
// An env.py names alembic.context through a name (`from alembic import context`) or an attribute (`import
|
|
136
|
+
// alembic.context` then `alembic.context.configure(...)`, open-webui's).
|
|
137
|
+
al_env_doc(d) :- file_name(d, "env.py"), (kind(e, "identifier") ; kind(e, "attribute")), sym_of(e, q),
|
|
138
|
+
prefix("alembic.context", q), docof(e, d).
|
|
139
|
+
// al_env_tree(d, e): revision d sits under the directory of env.py e. It pairs two documents by their paths, so it
|
|
140
|
+
// spans files on purpose (cross_file), keyed by documents alone: no node.
|
|
141
|
+
.decl al_env_tree(d: number, e: number) cross_file
|
|
142
|
+
al_env_tree(d, e) :- al_env_dir(e, r), al_migration_doc(d), doc(d, p, _), prefix(r, p).
|
|
143
|
+
.decl al_env_dir(e: number, r: symbol)
|
|
144
|
+
al_env_dir(e, r) :- al_env_doc(e), doc(e, ep, _), r = @replace_all(ep, "env\\.py$", "").
|
|
145
|
+
.decl al_in_some_tree(d: number)
|
|
146
|
+
al_in_some_tree(d) :- al_env_tree(d, _).
|
|
147
|
+
// al_env_reads(e, d): env.py e's settings come from document d: e itself, a module of the run e imports a name from
|
|
148
|
+
// (`from app.db import engine`), or a module e imports whole (`from app import db`), whose engine options (isolation
|
|
149
|
+
// level, connect_args) then apply to the migrations. It joins env.py's names to the documents that bind them, by
|
|
150
|
+
// qualified name, so it spans files on purpose (cross_file), keyed by documents alone. Each side is stated in its own
|
|
151
|
+
// file: al_env_name (what env.py names) and al_doc_binds (what a module binds at its top level).
|
|
152
|
+
.decl al_env_reads(e: number, d: number) cross_file
|
|
153
|
+
al_env_reads(e, e) :- al_env_doc(e).
|
|
154
|
+
al_env_reads(e, d) :- al_env_name(e, q), al_doc_binds(d, q), d != e.
|
|
155
|
+
al_env_reads(e, d) :- al_env_name(e, q), doc(d, _, q), d != e.
|
|
156
|
+
// al_env_name(e, q): an identifier of env.py e names q (sym_of).
|
|
157
|
+
.decl al_env_name(e: number, q: symbol)
|
|
158
|
+
al_env_name(e, q) :- al_env_doc(e), kind(u, "identifier"), docof(u, e), sym_of(u, q).
|
|
159
|
+
// al_doc_binds(d, q): document d, module mq, binds q = mq.x at its top level: an assignment, a def or a class.
|
|
160
|
+
.decl al_doc_binds(d: number, q: symbol)
|
|
161
|
+
al_doc_binds(d, q) :- al_const_def(d, q, _).
|
|
162
|
+
al_doc_binds(d, q) :- qualname(g, q), nearest(g, s, scope), kind(s, "module"), docof(g, d).
|
|
163
|
+
// al_const_def(d, q, v): document d, module mq, binds module-level name q = mq.x to the value v. Each file's own: a
|
|
164
|
+
// rule that reads another module's constant reads what this file states of it, by q (al_const_bool, al_qtype).
|
|
165
|
+
.decl al_const_def(d: number, q: symbol, v: node)
|
|
166
|
+
al_const_def(d, q, v) :- al_top_assign(d, x, v), modnode(d, m), modname(m, mq), q = @dot(mq, x).
|
|
167
|
+
.decl al_test_doc(d: number)
|
|
168
|
+
al_test_doc(d) :- doc(d, _, _), (path_part(d, "tests") ; path_part(d, "test") ; path_part(d, "testing")).
|
|
169
|
+
al_test_doc(d) :- file_name(d, s), (glob("test_*.py", s) ; glob("*_tests.py", s) ; glob("*_test.py", s) ;
|
|
170
|
+
s = "conftest.py").
|
|
171
|
+
|
|
172
|
+
// ---- revisions ---------------------------------------------------------------------------------------------------
|
|
173
|
+
.decl al_revision(d: number, r: symbol)
|
|
174
|
+
al_revision(d, r) :- al_migration_doc(d), al_top_assign(d, "revision", v), str(v, r).
|
|
175
|
+
// A merge revision revises each revision of its tuple (or list).
|
|
176
|
+
.decl al_down_revision(d: number, r: symbol)
|
|
177
|
+
al_down_revision(d, r) :- al_migration_doc(d), al_top_assign(d, "down_revision", v), str(v, r).
|
|
178
|
+
al_down_revision(d, r) :- al_migration_doc(d), al_top_assign(d, "down_revision", v), (kind(v, "tuple") ;
|
|
179
|
+
kind(v, "list")), child(v, e, _), str(e, r).
|
|
180
|
+
.decl al_branch_labeled(d: number)
|
|
181
|
+
al_branch_labeled(d) :- al_migration_doc(d), al_top_assign(d, "branch_labels", v), !kind(v, "none").
|
|
182
|
+
// al_rev_dir(d, p): revision d's directory; al_dir_has(p, r): directory p holds a revision with id r.
|
|
183
|
+
.decl al_rev_dir(d: number, p: symbol)
|
|
184
|
+
al_rev_dir(d, p) :- al_revision(d, _), !al_test_doc(d), doc(d, f, _), p = @replace_all(cat("/", f), "/[^/]*$", "").
|
|
185
|
+
// The revision graph spans the files of a tree on purpose: each revision names its parent by the parent's revision id
|
|
186
|
+
// string, in its own file. al_dir_has and al_parent are cross_file, keyed by directory paths, revision ids and
|
|
187
|
+
// documents: no node, no position. A revision's own facts (al_revision, al_down_revision, al_rev_dir) are its file's.
|
|
188
|
+
.decl al_dir_has(p: symbol, r: symbol) cross_file
|
|
189
|
+
al_dir_has(p, r) :- al_revision(d, r), al_rev_dir(d, p).
|
|
190
|
+
.decl al_parent(n: number, m: number) cross_file
|
|
191
|
+
al_parent(n, m) :- al_rev_dir(n, p), al_down_revision(n, r), al_revision(m, r), al_rev_dir(m, p), m != n.
|
|
192
|
+
al_parent(n, m) :- al_rev_dir(n, p), al_down_revision(n, r), !al_dir_has(p, r), al_revision(m, r), al_rev_dir(m, _),
|
|
193
|
+
m != n.
|
|
194
|
+
.decl al_ancestor(n: number, m: number)
|
|
195
|
+
al_ancestor(n, m) :- al_parent(n, m).
|
|
196
|
+
al_ancestor(n, m) :- al_ancestor(n, k), al_parent(k, m).
|
|
197
|
+
.decl al_has_child(m: number)
|
|
198
|
+
al_has_child(m) :- al_parent(_, m).
|
|
199
|
+
.decl al_head(h: number)
|
|
200
|
+
al_head(h) :- al_rev_dir(h, _), !al_has_child(h).
|
|
201
|
+
|
|
202
|
+
// ---- steps -------------------------------------------------------------------------------------------------------
|
|
203
|
+
.decl al_step(f: node, s: symbol)
|
|
204
|
+
al_step(f, s) :- kind(f, "function_definition"), def_name(f, s), (s = "upgrade" ; s = "downgrade"), nearest(f, m, scope),
|
|
205
|
+
kind(m, "module"), docof(f, d), al_migration_doc(d).
|
|
206
|
+
// al_helper(f, g): step f calls g, a module-level function of the same file, by its bare name.
|
|
207
|
+
.decl al_helper(f: node, g: node)
|
|
208
|
+
al_helper(f, g) :- al_step(f, _), kind(c, "call"), within(c, f), field(c, "function", fn), kind(fn, "identifier"),
|
|
209
|
+
text(fn, x), kind(g, "function_definition"), def_name(g, x), nearest(g, m, scope), kind(m, "module"), docof(g, d),
|
|
210
|
+
docof(f, d), g != f.
|
|
211
|
+
.decl al_in_step(n: node, f: node)
|
|
212
|
+
al_in_step(n, f) :- al_step(f, _), within(n, f).
|
|
213
|
+
al_in_step(n, f) :- al_helper(f, g), within(n, g).
|
|
214
|
+
|
|
215
|
+
// ---- operations --------------------------------------------------------------------------------------------------
|
|
216
|
+
.decl al_op_call(c: node, m: symbol)
|
|
217
|
+
al_op_call(c, m) :- kind(c, "call"), field(c, "function", f), attr_of(f, o, m), unparen(o, u), sym_of(u, "alembic.op").
|
|
218
|
+
// al_batch_with(w, v, tn, b): `with op.batch_alter_table(tn, ...) as v:` (tn positional or `table_name=`).
|
|
219
|
+
.decl al_batch_with(w: node, v: symbol, tn: node, b: node)
|
|
220
|
+
al_batch_with(w, v, tn, b) :- kind(w, "with_statement"), child(w, wc, _), kind(wc, "with_clause"), child(wc, wi, _),
|
|
221
|
+
kind(wi, "with_item"), field(wi, "value", ap), kind(ap, "as_pattern"), child(ap, e, 0), unparen(e, c),
|
|
222
|
+
al_op_call(c, "batch_alter_table"), (call_arg(c, 0, "", tn) ; call_arg(c, -1, "table_name", tn)),
|
|
223
|
+
field(ap, "alias", t), text(t, v), field(w, "body", b).
|
|
224
|
+
.decl al_batch_call(c: node, m: symbol, w: node)
|
|
225
|
+
al_batch_call(c, m, w) :- al_batch_with(w, v, _, b), kind(c, "call"), within(c, b), field(c, "function", f),
|
|
226
|
+
attr_of(f, o, m), kind(o, "identifier"), text(o, v).
|
|
227
|
+
.decl al_call(c: node, m: symbol, form: symbol)
|
|
228
|
+
al_call(c, m, "op") :- al_op_call(c, m).
|
|
229
|
+
al_call(c, m, "batch") :- al_batch_call(c, m, _).
|
|
230
|
+
// al_role_at(m, form, role, i, k): operation m's argument for role is positional i or keyword k (Alembic 1.x
|
|
231
|
+
// signatures; a batch operation leaves out the table argument, which its batch_alter_table gives).
|
|
232
|
+
.decl al_role_at(m: symbol, form: symbol, role: symbol, i: number, k: symbol)
|
|
233
|
+
al_role_at("create_table", "op", "table", 0, "table_name").
|
|
234
|
+
al_role_at("drop_table", "op", "table", 0, "table_name").
|
|
235
|
+
al_role_at("rename_table", "op", "table", 0, "old_table_name").
|
|
236
|
+
al_role_at("rename_table", "op", "new_table", 1, "new_table_name").
|
|
237
|
+
al_role_at("add_column", "op", "table", 0, "table_name").
|
|
238
|
+
al_role_at("add_column", "op", "column", 1, "column").
|
|
239
|
+
al_role_at("add_column", "batch", "column", 0, "column").
|
|
240
|
+
al_role_at("drop_column", "op", "table", 0, "table_name").
|
|
241
|
+
al_role_at("drop_column", "op", "column_name", 1, "column_name").
|
|
242
|
+
al_role_at("drop_column", "batch", "column_name", 0, "column_name").
|
|
243
|
+
al_role_at("alter_column", "op", "table", 0, "table_name").
|
|
244
|
+
al_role_at("alter_column", "op", "column_name", 1, "column_name").
|
|
245
|
+
al_role_at("alter_column", "batch", "column_name", 0, "column_name").
|
|
246
|
+
al_role_at("create_index", "op", "name", 0, "index_name").
|
|
247
|
+
al_role_at("create_index", "op", "table", 1, "table_name").
|
|
248
|
+
al_role_at("create_index", "op", "columns", 2, "columns").
|
|
249
|
+
al_role_at("create_index", "batch", "name", 0, "index_name").
|
|
250
|
+
al_role_at("create_index", "batch", "columns", 1, "columns").
|
|
251
|
+
al_role_at("drop_index", "op", "name", 0, "index_name").
|
|
252
|
+
al_role_at("drop_index", "op", "table", 1, "table_name").
|
|
253
|
+
al_role_at("drop_index", "batch", "name", 0, "index_name").
|
|
254
|
+
al_role_at("create_unique_constraint", "op", "name", 0, "constraint_name").
|
|
255
|
+
al_role_at("create_unique_constraint", "op", "table", 1, "table_name").
|
|
256
|
+
al_role_at("create_unique_constraint", "op", "columns", 2, "columns").
|
|
257
|
+
al_role_at("create_unique_constraint", "batch", "name", 0, "constraint_name").
|
|
258
|
+
al_role_at("create_unique_constraint", "batch", "columns", 1, "columns").
|
|
259
|
+
al_role_at("create_foreign_key", "op", "name", 0, "constraint_name").
|
|
260
|
+
al_role_at("create_foreign_key", "op", "table", 1, "source_table").
|
|
261
|
+
al_role_at("create_foreign_key", "op", "referent", 2, "referent_table").
|
|
262
|
+
al_role_at("create_foreign_key", "batch", "name", 0, "constraint_name").
|
|
263
|
+
al_role_at("create_foreign_key", "batch", "referent", 1, "referent_table").
|
|
264
|
+
al_role_at("create_check_constraint", "op", "name", 0, "constraint_name").
|
|
265
|
+
al_role_at("create_check_constraint", "op", "table", 1, "table_name").
|
|
266
|
+
al_role_at("create_check_constraint", "batch", "name", 0, "constraint_name").
|
|
267
|
+
al_role_at("create_primary_key", "op", "name", 0, "constraint_name").
|
|
268
|
+
al_role_at("create_primary_key", "op", "table", 1, "table_name").
|
|
269
|
+
al_role_at("drop_constraint", "op", "name", 0, "constraint_name").
|
|
270
|
+
al_role_at("drop_constraint", "op", "table", 1, "table_name").
|
|
271
|
+
al_role_at("drop_constraint", "batch", "name", 0, "constraint_name").
|
|
272
|
+
.decl al_role(c: node, role: symbol, e: node)
|
|
273
|
+
al_role(c, r, e) :- al_call(c, m, fm), al_role_at(m, fm, r, i, _), call_arg(c, i, "", e).
|
|
274
|
+
al_role(c, r, e) :- al_call(c, m, fm), al_role_at(m, fm, r, _, k), call_arg(c, -1, k, e).
|
|
275
|
+
// al_name_value(e, s): e names the string s: a literal, Alembic's naming wrapper `op.f("s")` (or a batch's
|
|
276
|
+
// `b.f("s")`), or an identifier bound to a literal in the same file (a module constant: `INDEX = "ix_files_path"`).
|
|
277
|
+
.decl al_name_value(e: node, s: symbol)
|
|
278
|
+
al_name_value(e, s) :- unparen(e, u), str(u, s).
|
|
279
|
+
al_name_value(e, s) :- unparen(e, u), kind(u, "identifier"), static_value(u, s).
|
|
280
|
+
al_name_value(e, s) :- unparen(e, u), al_call(u, "f", _), call_arg(u, 0, "", a), unparen(a, v), str(v, s).
|
|
281
|
+
.decl al_table(c: node, t: symbol)
|
|
282
|
+
al_table(c, t) :- al_call(c, _, "op"), al_role(c, "table", e), al_name_value(e, t).
|
|
283
|
+
al_table(c, t) :- al_batch_call(c, _, w), al_batch_with(w, _, tn, _), al_name_value(tn, t).
|
|
284
|
+
// al_creates_table(f, t): step f creates table t: `op.create_table("t", ...)`, a project's own wrapper called
|
|
285
|
+
// `create_table` (a bare name or an attribute, such as superset's `migrations.shared.utils.create_table("t", ...)`)
|
|
286
|
+
// whose first argument names t, or `op.execute("CREATE TABLE t ...")` (also IF NOT EXISTS, quoted names) when another
|
|
287
|
+
// operation of f acts on t.
|
|
288
|
+
.decl al_creates_table(f: node, t: symbol)
|
|
289
|
+
al_creates_table(f, t) :- al_call(c, "create_table", "op"), al_in_step(c, f), al_table(c, t).
|
|
290
|
+
al_creates_table(f, t) :- al_create_table_wrapper(c), (call_arg(c, 0, "", a) ; call_arg(c, -1, "table_name", a)),
|
|
291
|
+
al_name_value(a, t), al_in_step(c, f).
|
|
292
|
+
al_creates_table(f, t) :- al_exec_sql(f, y), al_call(c, _, _), al_table(c, t), al_in_step(c, f), u = @lower(t),
|
|
293
|
+
(contains(cat(cat("create table ", u), " "), y) ; contains(cat(cat("create table ", u), "("), y) ;
|
|
294
|
+
contains(cat(cat("create table if not exists ", u), " "), y) ;
|
|
295
|
+
contains(cat(cat("create table if not exists ", u), "("), y)).
|
|
296
|
+
// al_exec_sql(f, y): step f runs `op.execute("...")` with the literal SQL y, lowercased, quotes dropped, whitespace
|
|
297
|
+
// runs as one space, and a space at the end.
|
|
298
|
+
.decl al_exec_sql(f: node, y: symbol)
|
|
299
|
+
al_exec_sql(f, y) :- al_call(c, "execute", "op"), al_in_step(c, f), call_arg(c, 0, "", a), unparen(a, s),
|
|
300
|
+
kind(s, "string"), text(s, x), x1 = @replace_all(@lower(x), "[\"`']", ""), x2 = @replace_all(x1, "\\s+", " "),
|
|
301
|
+
y = cat(x2, " ").
|
|
302
|
+
.decl al_create_table_wrapper(c: node)
|
|
303
|
+
al_create_table_wrapper(c) :- kind(c, "call"), field(c, "function", fn), kind(fn, "identifier"), text(fn, "create_table").
|
|
304
|
+
al_create_table_wrapper(c) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, "create_table"),
|
|
305
|
+
!al_call(c, "create_table", _).
|
|
306
|
+
// al_empties_table(f, t): step f deletes every row of t first: `op.execute("DELETE FROM t")` without WHERE, or
|
|
307
|
+
// TRUNCATE.
|
|
308
|
+
.decl al_empties_table(f: node, t: symbol)
|
|
309
|
+
al_empties_table(f, t) :- al_exec_sql(f, y), al_call(c, _, _), al_table(c, t), al_in_step(c, f), u = @lower(t),
|
|
310
|
+
(contains(cat(cat("delete from ", u), " "), y) ; contains(cat(cat("truncate ", u), " "), y) ;
|
|
311
|
+
contains(cat(cat("truncate table ", u), " "), y)), !contains(" where ", y).
|
|
312
|
+
// al_on_existing(c, m, t, f): operation m (c) in upgrade step f acts on table t, which f neither creates nor empties:
|
|
313
|
+
// a table that may hold rows in production.
|
|
314
|
+
.decl al_on_existing(c: node, m: symbol, t: symbol, f: node)
|
|
315
|
+
al_on_existing(c, m, t, f) :- al_call(c, m, _), al_table(c, t), al_in_step(c, f), al_step(f, "upgrade"),
|
|
316
|
+
!al_creates_table(f, t), !al_empties_table(f, t).
|
|
317
|
+
.decl al_kw(c: node, k: symbol, v: node)
|
|
318
|
+
al_kw(c, k, v) :- call_arg(c, -1, k, v).
|
|
319
|
+
// al_bool(v, b): expression v is statically b ("true", "false" or "none") in its own file: the literal, or a name
|
|
320
|
+
// bound to it at module level of the same file (`CONCURRENT = True`).
|
|
321
|
+
.decl al_bool(v: node, b: symbol)
|
|
322
|
+
al_bool(v, b) :- unparen(v, u), kind(u, k), al_bool_kind(k, b).
|
|
323
|
+
al_bool(v, b) :- unparen(v, u), kind(u, "identifier"), text(u, x), docof(u, d), al_top_assign(d, x, w), al_bool(w, b).
|
|
324
|
+
.decl al_bool_kind(k: symbol, b: symbol)
|
|
325
|
+
al_bool_kind("true", "true"). al_bool_kind("false", "false"). al_bool_kind("none", "none").
|
|
326
|
+
// A name imported from another module is read by its symbol: al_bool_ref(v, q), v is a name sym_of resolves to q;
|
|
327
|
+
// al_const_bool(q, b), module-level name q is statically b, stated in q's own file (al_const_bool_at: its value is a
|
|
328
|
+
// literal or a same-file constant; al_const_ref_at: its value is a name of another, `CONCURRENT = settings.FLAG`).
|
|
329
|
+
// al_const_bool spans files on purpose (cross_file), keyed by qualified names and values alone: a rule reads it in
|
|
330
|
+
// its output, or by a call's term (al_kw_ext), never at a node of the per-file part.
|
|
331
|
+
.decl al_bool_ref(v: node, q: symbol)
|
|
332
|
+
al_bool_ref(v, q) :- unparen(v, u), kind(u, "identifier"), sym_of(u, q).
|
|
333
|
+
.decl al_const_bool_at(d: number, q: symbol, b: symbol)
|
|
334
|
+
al_const_bool_at(d, q, b) :- al_const_def(d, q, w), al_bool(w, b).
|
|
335
|
+
.decl al_const_ref_at(d: number, q: symbol, r: symbol)
|
|
336
|
+
al_const_ref_at(d, q, r) :- al_const_def(d, q, w), al_bool_ref(w, r).
|
|
337
|
+
.decl al_const_bool(q: symbol, b: symbol) cross_file
|
|
338
|
+
al_const_bool(q, b) :- al_const_bool_at(_, q, b).
|
|
339
|
+
al_const_bool(q, b) :- al_const_ref_at(_, q, r), al_const_bool(r, b).
|
|
340
|
+
// al_true_kw(c, k), al_false_kw(c, k): call c passes k= True / False by value, in its own file (al_bool).
|
|
341
|
+
.decl al_true_kw(c: node, k: symbol)
|
|
342
|
+
al_true_kw(c, k) :- call_arg(c, -1, k, v), al_bool(v, "true").
|
|
343
|
+
.decl al_false_kw(c: node, k: symbol)
|
|
344
|
+
al_false_kw(c, k) :- call_arg(c, -1, k, v), al_bool(v, "false").
|
|
345
|
+
// al_kw_ref(c, k, q): call c passes k= a name sym_of resolves to q. al_kw_ext(d, t, k, b): the Alembic operation,
|
|
346
|
+
// Column or env.py configure() of document d whose term is t (al_term) passes k= an imported constant that is
|
|
347
|
+
// statically b. A rule's output reads it with al_term(c, d, t): `al_true_kw(c, k)` or `al_term(c, d, t),
|
|
348
|
+
// al_kw_ext(d, t, k, "true")`.
|
|
349
|
+
.decl al_kw_ref(c: node, k: symbol, q: symbol)
|
|
350
|
+
al_kw_ref(c, k, q) :- call_arg(c, -1, k, v), al_bool_ref(v, q).
|
|
351
|
+
.decl al_flag_call(c: node)
|
|
352
|
+
al_flag_call(c) :- al_call(c, _, _).
|
|
353
|
+
al_flag_call(c) :- al_column_call(c).
|
|
354
|
+
al_flag_call(c) :- al_env_configure(c).
|
|
355
|
+
.decl al_kw_ref_at(d: number, t: symbol, k: symbol, q: symbol)
|
|
356
|
+
al_kw_ref_at(d, t, k, q) :- al_flag_call(c), al_kw_ref(c, k, q), al_term(c, d, t).
|
|
357
|
+
.decl al_kw_ext(d: number, t: symbol, k: symbol, b: symbol)
|
|
358
|
+
al_kw_ext(d, t, k, b) :- al_kw_ref_at(d, t, k, q), al_const_bool(q, b).
|
|
359
|
+
// The same by term, for a rule whose own logic runs on the answers: al_kw_at(d, t, k), the call passes k= (its own
|
|
360
|
+
// file's); al_flag_t(d, t, k, b), it passes k= something statically b, in its own file (al_bool) or an imported
|
|
361
|
+
// constant (al_kw_ext).
|
|
362
|
+
.decl al_kw_at(d: number, t: symbol, k: symbol)
|
|
363
|
+
al_kw_at(d, t, k) :- al_flag_call(c), call_arg(c, -1, k, _), al_term(c, d, t).
|
|
364
|
+
.decl al_flag_at(d: number, t: symbol, k: symbol, b: symbol)
|
|
365
|
+
al_flag_at(d, t, k, b) :- al_flag_call(c), call_arg(c, -1, k, v), al_bool(v, b), al_term(c, d, t).
|
|
366
|
+
.decl al_flag_t(d: number, t: symbol, k: symbol, b: symbol)
|
|
367
|
+
al_flag_t(d, t, k, b) :- al_flag_at(d, t, k, b).
|
|
368
|
+
al_flag_t(d, t, k, b) :- al_kw_ext(d, t, k, b).
|
|
369
|
+
// al_term(n, d, t): node n of document d is known by the term t (python/points-to-file's pt_tm: its place in its
|
|
370
|
+
// nearest definition), the key a cross-file answer about one node is stated by, and a finding reads it back by.
|
|
371
|
+
.decl al_term(n: node, d: number, t: symbol)
|
|
372
|
+
al_term(n, d, t) :- pt_tm(n, t), docof(n, d).
|
|
373
|
+
|
|
374
|
+
// ---- types -------------------------------------------------------------------------------------------------------
|
|
375
|
+
// A type is read by the class it resolves to, not by its spelling: `sa.INTEGER()`, `Integer`, `from sqlalchemy
|
|
376
|
+
// import Integer as Int`, a module alias `BigInt = sa.BigInteger`, a project TypeDecorator (`impl = TEXT`), a project
|
|
377
|
+
// subclass of a SQLAlchemy type, and a project function that returns a type (superset's `MediumText()` returns
|
|
378
|
+
// `Text().with_variant(MEDIUMTEXT(), "mysql")`) all resolve to the SQLAlchemy class whose DDL PostgreSQL runs. Only a
|
|
379
|
+
// name that resolves to nothing (a class outside the run that is not SQLAlchemy's) falls back to its last name.
|
|
380
|
+
// Each file resolves what it defines itself (al_type_q); a name of another module of the run is read by its symbol
|
|
381
|
+
// (al_type_far_of), and the module that defines it states what that name is a type of (al_qtype_at), so no node
|
|
382
|
+
// crosses files. A rule reads the answer by the type expression's term: al_type_class_t, al_pg_type_t,
|
|
383
|
+
// al_mysql_type_t, for the expressions al_type_use names.
|
|
384
|
+
// al_type_site(e): e is used as a type: alter_column's type_/existing_type, a Column's second positional argument or
|
|
385
|
+
// type_ (al_type_use), what a name of the file makes a type of (al_type_def: a class's `impl`, a function's returned
|
|
386
|
+
// call, a module constant's value, whether its own file or another uses it), or the object of a variant.
|
|
387
|
+
.decl al_type_site(e: node)
|
|
388
|
+
al_type_site(e) :- al_type_use(e).
|
|
389
|
+
al_type_site(e) :- al_type_def(_, _, e).
|
|
390
|
+
al_type_site(o) :- al_type_site(e), al_variant_of(e, o).
|
|
391
|
+
// al_type_use(e): e is a column's type: alter_column's type_/existing_type, a Column's second positional argument or
|
|
392
|
+
// type_. These are the expressions the rules ask about, by term.
|
|
393
|
+
.decl al_type_use(e: node)
|
|
394
|
+
al_type_use(e) :- al_call(c, "alter_column", _), (al_kw(c, "type_", v) ; al_kw(c, "existing_type", v)), unparen(v, e).
|
|
395
|
+
al_type_use(e) :- al_column_call(col), (call_arg(col, 1, "", v) ; call_arg(col, -1, "type_", v)), unparen(v, e).
|
|
396
|
+
// al_variant_of(e, o): e is `o.with_variant(...)` (or another method call on a type instance o).
|
|
397
|
+
.decl al_variant_of(e: node, o: node)
|
|
398
|
+
al_variant_of(e, o) :- kind(e, "call"), field(e, "function", f), attr_of(f, x, m), (m = "with_variant" ;
|
|
399
|
+
m = "evaluates_none" ; m = "copy"), unparen(x, o), kind(o, "call").
|
|
400
|
+
// al_type_head(e, h): the class expression of type e: the callee of `sa.String(255)`, or e itself.
|
|
401
|
+
.decl al_type_head(e: node, h: node)
|
|
402
|
+
al_type_head(e, h) :- al_type_site(e), kind(e, "call"), !al_variant_of(e, _), field(e, "function", h).
|
|
403
|
+
al_type_head(e, e) :- al_type_site(e), (kind(e, "identifier") ; kind(e, "attribute")).
|
|
404
|
+
// al_type_ref(h, g): type head h names g, a class or function of its own file.
|
|
405
|
+
.decl al_type_ref(h: node, g: node)
|
|
406
|
+
al_type_ref(h, g) :- al_type_head(_, h), sym_of(h, q), docof(h, d), qualname(g, q), docof(g, d).
|
|
407
|
+
// al_type_alias(h, v): identifier h names a module-level binding of its own file to v (`BigInt = sa.BigInteger`).
|
|
408
|
+
.decl al_type_alias(h: node, v: node)
|
|
409
|
+
al_type_alias(h, v) :- al_type_head(_, h), kind(h, "identifier"), text(h, x), docof(h, d), al_top_assign(d, x, v).
|
|
410
|
+
al_type_alias(h, v) :- al_type_head(_, h), kind(h, "identifier"), sym_of(h, q), docof(h, d), al_const_def(d, q, v).
|
|
411
|
+
// al_type_far(h, q): type head h names q, a name of another module of the run (one h's file reads, py_names), which
|
|
412
|
+
// its own file does not define (`from app.types import Int32`).
|
|
413
|
+
.decl al_type_far(h: node, q: symbol)
|
|
414
|
+
al_type_far(h, q) :- al_type_head(_, h), sym_of(h, q), !al_type_ref(h, _), !al_type_alias(h, _), docof(h, d),
|
|
415
|
+
doc(d, p, _), py_names__reads_at(p, m), prefix(cat(m, "."), q).
|
|
416
|
+
// al_type_q(e, q): type e resolves, in its own file, to the class with qualified name q outside the run
|
|
417
|
+
// (SQLAlchemy's, a dialect's, sqlmodel's).
|
|
418
|
+
.decl al_type_q(e: node, q: symbol)
|
|
419
|
+
al_type_q(e, q) :- al_type_head(e, h), sym_of(h, q), !al_type_ref(h, _), !al_type_alias(h, _), !al_type_far(h, _).
|
|
420
|
+
al_type_q(e, q) :- al_type_head(e, h), al_type_ref(h, g), al_impl(g, i), al_type_q(i, q).
|
|
421
|
+
al_type_q(e, q) :- al_type_head(e, h), al_type_ref(h, g), al_type_base(g, q).
|
|
422
|
+
al_type_q(e, q) :- al_type_head(e, h), al_type_ref(h, g), al_returns_type(g, i), al_type_q(i, q).
|
|
423
|
+
al_type_q(e, q) :- al_type_head(e, h), al_type_alias(h, v), unparen(v, i), al_type_q(i, q).
|
|
424
|
+
al_type_q(e, q) :- al_variant_of(e, o), al_type_q(o, q).
|
|
425
|
+
// al_type_far_of(e, q0): type e resolves, in its own file, to the name q0 of another module (directly, or through an
|
|
426
|
+
// alias, impl, function or variant of its own file): the class q0's module states (al_qtype).
|
|
427
|
+
.decl al_type_far_of(e: node, q0: symbol)
|
|
428
|
+
al_type_far_of(e, q0) :- al_type_head(e, h), al_type_far(h, q0).
|
|
429
|
+
al_type_far_of(e, q0) :- al_type_head(e, h), al_type_ref(h, g), al_impl(g, i), al_type_far_of(i, q0).
|
|
430
|
+
al_type_far_of(e, q0) :- al_type_head(e, h), al_type_ref(h, g), al_returns_type(g, i), al_type_far_of(i, q0).
|
|
431
|
+
al_type_far_of(e, q0) :- al_type_head(e, h), al_type_alias(h, v), unparen(v, i), al_type_far_of(i, q0).
|
|
432
|
+
al_type_far_of(e, q0) :- al_variant_of(e, o), al_type_far_of(o, q0).
|
|
433
|
+
// al_impl(g, i): class g's body says `impl = i`. al_type_base(g, q): class g has no impl and subclasses SQLAlchemy's
|
|
434
|
+
// type q (not TypeDecorator). al_returns_type(g, i): function g returns the call i.
|
|
435
|
+
.decl al_impl(g: node, i: node)
|
|
436
|
+
al_impl(g, i) :- kind(g, "class_definition"), field(g, "body", b), body_stmt(b, s, _), child(s, a, 0),
|
|
437
|
+
kind(a, "assignment"), field(a, "left", l), text(l, "impl"), field(a, "right", v), unparen(v, i).
|
|
438
|
+
.decl al_has_impl(g: node)
|
|
439
|
+
al_has_impl(g) :- al_impl(g, _).
|
|
440
|
+
.decl al_type_base(g: node, q: symbol)
|
|
441
|
+
al_type_base(g, q) :- kind(g, "class_definition"), !al_has_impl(g), class_base(g, q), prefix("sqlalchemy.", q),
|
|
442
|
+
!suffix(".TypeDecorator", q).
|
|
443
|
+
.decl al_returns_type(g: node, i: node)
|
|
444
|
+
al_returns_type(g, i) :- kind(g, "function_definition"), kind(r, "return_statement"), within(r, g), child(r, v, _),
|
|
445
|
+
kind(v, "call"), unparen(v, i).
|
|
446
|
+
// What a module's names are types of, stated in the module's own file, by qualified name: al_type_def(d, q0, i), i is
|
|
447
|
+
// what name q0 of document d makes a type of (a class's impl, a function's returned call, a module constant's value);
|
|
448
|
+
// al_qtype_at(d, q0, q): q0 is SQLAlchemy's class q; al_qtype_via_at(d, q0, r): q0 is what the name r of another
|
|
449
|
+
// module is. al_qtype(q0, q) follows them across modules: it spans files on purpose (cross_file), keyed by qualified
|
|
450
|
+
// names alone. A name of another module that is no definition of the run (al_far_undef_at: py_names defines no
|
|
451
|
+
// def or class of it; al_const_name: no module binds it) is itself the class, as a library's is.
|
|
452
|
+
.decl al_type_def(d: number, q0: symbol, i: node)
|
|
453
|
+
al_type_def(d, q0, i) :- al_impl(g, i), qualname(g, q0), docof(g, d).
|
|
454
|
+
al_type_def(d, q0, i) :- al_returns_type(g, i), qualname(g, q0), docof(g, d).
|
|
455
|
+
al_type_def(d, q0, i) :- al_const_def(d, q0, v), unparen(v, i).
|
|
456
|
+
.decl al_qtype_at(d: number, q0: symbol, q: symbol)
|
|
457
|
+
al_qtype_at(d, q0, q) :- al_type_def(d, q0, i), al_type_q(i, q).
|
|
458
|
+
al_qtype_at(d, q0, q) :- al_type_base(g, q), qualname(g, q0), docof(g, d).
|
|
459
|
+
.decl al_qtype_via_at(d: number, q0: symbol, r: symbol)
|
|
460
|
+
al_qtype_via_at(d, q0, r) :- al_type_def(d, q0, i), al_type_far_of(i, r).
|
|
461
|
+
.decl al_far_undef_at(d: number, q: symbol)
|
|
462
|
+
al_far_undef_at(d, q) :- al_type_far(h, q), docof(h, d), doc(d, p, _), !al_far_defined(p, q).
|
|
463
|
+
.decl al_far_defined(p: symbol, q: symbol)
|
|
464
|
+
al_far_defined(p, q) :- al_type_far(h, q), docof(h, d), doc(d, p, _), py_names__reads_at(p, m), py_names__defined(m, q).
|
|
465
|
+
.decl al_const_name_at(d: number, q: symbol)
|
|
466
|
+
al_const_name_at(d, q) :- al_const_def(d, q, _).
|
|
467
|
+
.decl al_const_name(q: symbol) cross_file
|
|
468
|
+
al_const_name(q) :- al_const_name_at(_, q).
|
|
469
|
+
.decl al_qtype(q0: symbol, q: symbol) cross_file
|
|
470
|
+
al_qtype(q0, q) :- al_qtype_at(_, q0, q).
|
|
471
|
+
al_qtype(q0, q) :- al_qtype_via_at(_, q0, r), al_qtype(r, q).
|
|
472
|
+
al_qtype(q0, r) :- al_qtype_via_at(d, q0, r), al_far_undef_at(d, r), !al_const_name(r).
|
|
473
|
+
// A column type's answers, by the type expression's term (al_term). al_type_q_t(d, t, q): it resolves to class q, in
|
|
474
|
+
// its own file or through al_qtype. al_type_class_t(d, t, n): its class name, lowercased (`...postgresql.UUID` ->
|
|
475
|
+
// "uuid"); for a name that resolves to nothing, its last name. al_mysql_type_t(d, t): a MySQL dialect type
|
|
476
|
+
// (`sqlalchemy.dialects.mysql.LONGTEXT`), which PostgreSQL never runs. al_pg_type_t(d, t, k): the PostgreSQL type k it
|
|
477
|
+
// renders, for the classes whose DDL is known.
|
|
478
|
+
.decl al_type_q_at(d: number, t: symbol, q: symbol)
|
|
479
|
+
al_type_q_at(d, t, q) :- al_type_use(e), al_type_q(e, q), al_term(e, d, t).
|
|
480
|
+
.decl al_type_far_at(d: number, t: symbol, q0: symbol)
|
|
481
|
+
al_type_far_at(d, t, q0) :- al_type_use(e), al_type_far_of(e, q0), al_term(e, d, t).
|
|
482
|
+
.decl al_type_q_t(d: number, t: symbol, q: symbol)
|
|
483
|
+
al_type_q_t(d, t, q) :- al_type_q_at(d, t, q).
|
|
484
|
+
al_type_q_t(d, t, q) :- al_type_far_at(d, t, q0), al_qtype(q0, q).
|
|
485
|
+
al_type_q_t(d, t, q0) :- al_type_far_at(d, t, q0), al_far_undef_at(d, q0), !al_const_name(q0).
|
|
486
|
+
// al_type_fallback_at(d, t, n): a type head its file resolves to nothing at all: its last name.
|
|
487
|
+
.decl al_type_fallback_at(d: number, t: symbol, n: symbol)
|
|
488
|
+
al_type_fallback_at(d, t, n) :- al_type_use(e), al_type_head(e, h), !al_type_q(e, _), !al_type_far_of(e, _),
|
|
489
|
+
!al_type_ref(h, _), !al_type_alias(h, _), name(h, s), n = @lower(@replace_all(s, "^.*\\.", "")), al_term(e, d, t).
|
|
490
|
+
.decl al_type_class_t(d: number, t: symbol, n: symbol)
|
|
491
|
+
al_type_class_t(d, t, n) :- al_type_q_t(d, t, q), n = @lower(@replace_all(q, "^.*\\.", "")).
|
|
492
|
+
al_type_class_t(d, t, n) :- al_type_fallback_at(d, t, n).
|
|
493
|
+
.decl al_mysql_type_t(d: number, t: symbol)
|
|
494
|
+
al_mysql_type_t(d, t) :- al_type_q_t(d, t, q), prefix("sqlalchemy.dialects.mysql", q).
|
|
495
|
+
.decl al_pg_type_t(d: number, t: symbol, k: symbol)
|
|
496
|
+
al_pg_type_t(d, t, k) :- al_type_class_t(d, t, n), al_pg_class(n, k), !al_mysql_type_t(d, t).
|
|
497
|
+
al_pg_type_t(d, t, "timestamp") :- al_type_class_t(d, t, n), (n = "datetime" ; n = "timestamp"), !al_tz_t(d, t).
|
|
498
|
+
al_pg_type_t(d, t, "timestamptz") :- al_type_class_t(d, t, n), (n = "datetime" ; n = "timestamp"), al_tz_t(d, t).
|
|
499
|
+
// al_tz_t(d, t): the type call passes timezone=True (or True first), a literal or a constant (al_bool, al_const_bool).
|
|
500
|
+
.decl al_tz_at(d: number, t: symbol, q: symbol)
|
|
501
|
+
al_tz_at(d, t, "") :- al_type_use(e), al_tz_arg(e, v), al_bool(v, "true"), al_term(e, d, t).
|
|
502
|
+
al_tz_at(d, t, q) :- al_type_use(e), al_tz_arg(e, v), al_bool_ref(v, q), al_term(e, d, t).
|
|
503
|
+
.decl al_tz_t(d: number, t: symbol)
|
|
504
|
+
al_tz_t(d, t) :- al_tz_at(d, t, "").
|
|
505
|
+
al_tz_t(d, t) :- al_tz_at(d, t, q), al_const_bool(q, "true").
|
|
506
|
+
.decl al_tz_arg(e: node, v: node)
|
|
507
|
+
al_tz_arg(e, v) :- kind(e, "call"), (call_arg(e, -1, "timezone", v) ; call_arg(e, 0, "", v)).
|
|
508
|
+
.decl al_pg_class(n: symbol, k: symbol)
|
|
509
|
+
al_pg_class("string", "varchar"). al_pg_class("varchar", "varchar"). al_pg_class("unicode", "varchar").
|
|
510
|
+
al_pg_class("nvarchar", "varchar"). al_pg_class("autostring", "varchar"). al_pg_class("text", "text").
|
|
511
|
+
al_pg_class("unicodetext", "text"). al_pg_class("integer", "integer"). al_pg_class("int", "integer").
|
|
512
|
+
al_pg_class("biginteger", "bigint"). al_pg_class("bigint", "bigint"). al_pg_class("smallinteger", "smallint").
|
|
513
|
+
al_pg_class("smallint", "smallint"). al_pg_class("boolean", "boolean"). al_pg_class("bool", "boolean").
|
|
514
|
+
al_pg_class("date", "date"). al_pg_class("time", "time"). al_pg_class("interval", "interval").
|
|
515
|
+
al_pg_class("uuid", "uuid"). al_pg_class("json", "json"). al_pg_class("jsonb", "jsonb").
|
|
516
|
+
al_pg_class("numeric", "numeric"). al_pg_class("decimal", "numeric"). al_pg_class("float", "float").
|
|
517
|
+
al_pg_class("double", "float"). al_pg_class("double_precision", "float"). al_pg_class("real", "float").
|
|
518
|
+
al_pg_class("largebinary", "bytea"). al_pg_class("bytea", "bytea"). al_pg_class("enum", "enum").
|
|
519
|
+
al_pg_class("char", "char").
|
|
520
|
+
|
|
521
|
+
// ---- what add_column emits beyond ADD COLUMN ---------------------------------------------------------------------
|
|
522
|
+
// al_added_ddl(c, t, kind, f): add_column c, in upgrade step f, adds to existing table t a Column whose options make
|
|
523
|
+
// Alembic emit a second statement (Alembic 1.20, offline render): kind "unique" (ADD UNIQUE), "index" (CREATE INDEX),
|
|
524
|
+
// "foreign_key" (ADD FOREIGN KEY, unless `inline_references=True` renders it inside the ADD COLUMN) or "check" (ADD
|
|
525
|
+
// CHECK). The constraint classes resolve through imports. Flags are read in c's own file (al_true_kw); a flag that is
|
|
526
|
+
// an imported constant is al_added_ddl_if's (unique, index), or al_kw_ext's (inline_references), read in the output.
|
|
527
|
+
.decl al_added_ddl(c: node, t: symbol, kind: symbol, f: node)
|
|
528
|
+
al_added_ddl(c, t, "unique", f) :- al_on_existing(c, "add_column", t, f), al_added_column(c, col), al_true_kw(col, "unique").
|
|
529
|
+
al_added_ddl(c, t, "index", f) :- al_on_existing(c, "add_column", t, f), al_added_column(c, col), al_true_kw(col, "index").
|
|
530
|
+
al_added_ddl(c, t, k, f) :- al_column_part(c, t, k, f), k = "check".
|
|
531
|
+
al_added_ddl(c, t, k, f) :- al_column_part(c, t, k, f), k = "foreign_key", !al_true_kw(c, "inline_references").
|
|
532
|
+
// al_added_ddl_if(c, t, k, f, q): add_column c would be al_added_ddl's kind k ("unique", "index") if the constant q
|
|
533
|
+
// its Column passes as k= is True (al_const_bool).
|
|
534
|
+
.decl al_added_ddl_if(c: node, t: symbol, kind: symbol, f: node, q: symbol)
|
|
535
|
+
al_added_ddl_if(c, t, k, f, q) :- al_on_existing(c, "add_column", t, f), al_added_column(c, col), (k = "unique" ;
|
|
536
|
+
k = "index"), al_kw_ref(col, k, q).
|
|
537
|
+
// al_column_part(c, t, k, f): the added Column passes SQLAlchemy's (or sqlmodel's re-export of) ForeignKey (k
|
|
538
|
+
// "foreign_key") or CheckConstraint (k "check").
|
|
539
|
+
.decl al_column_part(c: node, t: symbol, k: symbol, f: node)
|
|
540
|
+
al_column_part(c, t, k, f) :- al_on_existing(c, "add_column", t, f), al_added_column(c, col), call_arg(col, _, "", a),
|
|
541
|
+
unparen(a, u), kind(u, "call"), field(u, "function", fn), sym_of(fn, q), (prefix("sqlalchemy.", q) ;
|
|
542
|
+
prefix("sqlmodel.", q)), ((suffix(".ForeignKey", q), k = "foreign_key") ; (suffix(".CheckConstraint", q),
|
|
543
|
+
k = "check")).
|
|
544
|
+
|
|
545
|
+
// al_forwarded(c, m, t, ci, f): call c, in upgrade step f, calls a function of the same file whose body runs Alembic
|
|
546
|
+
// operation m (ci) on the table one of its parameters names; c passes table t there, positionally or by keyword, and
|
|
547
|
+
// f does not create t. One call deep. A function of another module (superset's
|
|
548
|
+
// `migrations.shared.utils.create_index(table, name, cols)`, through its import) is read by its symbol: the module
|
|
549
|
+
// states what its functions forward (al_fwd_op_at, by the function's qualified name and the operation's term), the
|
|
550
|
+
// call's file states what each call passes (al_fwd_call), and a rule joins them by the function's qualified name.
|
|
551
|
+
// al_fwd_op(g, m, x, k, ci): function g's body runs operation m (ci) on the table its parameter x, at position k,
|
|
552
|
+
// names.
|
|
553
|
+
.decl al_fwd_op(g: node, m: symbol, x: symbol, k: number, ci: node)
|
|
554
|
+
al_fwd_op(g, m, x, k, ci) :- al_call(ci, m, "op"), al_role(ci, "table", e), unparen(e, u), kind(u, "identifier"),
|
|
555
|
+
text(u, x), nearest(ci, g, fdef), kind(g, "function_definition"), param(g, x, i), !al_step(g, _),
|
|
556
|
+
k = count : { param(g, _, j), j < i }.
|
|
557
|
+
.decl al_fwd_target(c: node, g: node)
|
|
558
|
+
al_fwd_target(c, g) :- kind(c, "call"), field(c, "function", fn), sym_of(fn, q), docof(c, d), al_fwd_op(g, _, _, _, _),
|
|
559
|
+
qualname(g, q), docof(g, d).
|
|
560
|
+
.decl al_forwarded(c: node, m: symbol, t: symbol, ci: node, f: node)
|
|
561
|
+
al_forwarded(c, m, t, ci, f) :- al_fwd_target(c, g), al_fwd_op(g, m, x, k, ci), (call_arg(c, k, "", a) ;
|
|
562
|
+
call_arg(c, -1, x, a)), al_name_value(a, t), al_in_step(c, f), al_step(f, "upgrade"), !al_creates_table(f, t).
|
|
563
|
+
// al_fwd_op_at(d, g, m, x, k, ci): function g (qualified name) of document d runs operation m, the call of term ci,
|
|
564
|
+
// on the table its parameter x, at position k, names.
|
|
565
|
+
.decl al_fwd_op_at(d: number, g: symbol, m: symbol, x: symbol, k: number, ci: symbol)
|
|
566
|
+
al_fwd_op_at(d, gq, m, x, k, cit) :- al_fwd_op(g, m, x, k, ci), qualname(g, gq), al_term(ci, d, cit).
|
|
567
|
+
// al_fwd_call(c, g, i, x, t, f): call c, in upgrade step f, calls g, a function of another module of the run (sym_of;
|
|
568
|
+
// a module c's file reads, py_names), and passes the name t as its argument at position i (x "") or as keyword x
|
|
569
|
+
// (i -1); f does not create t.
|
|
570
|
+
.decl al_fwd_call(c: node, g: symbol, i: number, x: symbol, t: symbol, f: node)
|
|
571
|
+
al_fwd_call(c, gq, i, x, t, f) :- kind(c, "call"), field(c, "function", fn), sym_of(fn, gq), !al_fwd_target(c, _),
|
|
572
|
+
call_arg(c, i, x, a), al_name_value(a, t), al_in_step(c, f), al_step(f, "upgrade"), !al_creates_table(f, t),
|
|
573
|
+
docof(c, d), doc(d, p, _), py_names__reads_at(p, m), prefix(cat(m, "."), gq).
|
|
574
|
+
|
|
575
|
+
// ---- transactions ------------------------------------------------------------------------------------------------
|
|
576
|
+
// al_autocommit_with(w, b): `with op.get_context().autocommit_block():` (or any `<x>.autocommit_block()`, such as a
|
|
577
|
+
// MigrationContext held in a local).
|
|
578
|
+
.decl al_autocommit_with(w: node, b: node)
|
|
579
|
+
al_autocommit_with(w, b) :- kind(w, "with_statement"), child(w, wc, _), kind(wc, "with_clause"), child(wc, wi, _),
|
|
580
|
+
kind(wi, "with_item"), field(wi, "value", v), unparen(v, c), kind(c, "call"), field(c, "function", f),
|
|
581
|
+
attr_of(f, _, "autocommit_block"), field(w, "body", b).
|
|
582
|
+
.decl al_in_autocommit(n: node)
|
|
583
|
+
al_in_autocommit(n) :- al_autocommit_with(_, b), within(n, b).
|
|
584
|
+
|
|
585
|
+
// ---- dialects ----------------------------------------------------------------------------------------------------
|
|
586
|
+
// A dialect test is read by its structure, not its spelling: a comparison of anything with a database name literal
|
|
587
|
+
// (`bind.dialect.name == "postgresql"`, `dialect_name in {"mysql", "mariadb"}`, `"sqlite" in DATABASE_URI`), an
|
|
588
|
+
// isinstance() test against a class SQLAlchemy's dialect packages define (resolved through imports: `SQLiteDialect`,
|
|
589
|
+
// `PGDialect`, `mysql.dialect`), `not`, `and`, `or`, a call of a function of the run whose body returns a test
|
|
590
|
+
// (`_is_pg(conn)`, `is_mysql()` imported from another module), and a name assigned a test in the same scope
|
|
591
|
+
// (`is_mysql = bind.dialect.name == "mysql"`). A name or call that is no such test (`settings.USE_SQLITE`,
|
|
592
|
+
// `_postgres_enabled()` that reads a setting) tells nothing.
|
|
593
|
+
// A test function of the same file is read there (al_test_fn); one of another module is read by its symbol: the
|
|
594
|
+
// file that defines it states what its returned test says (al_fn_db_at, al_fn_via_at), al_fn_db follows them across
|
|
595
|
+
// modules, and a node under a call of it is answered by its term (al_np_ext).
|
|
596
|
+
// al_db_of(e, db): string e, compared in a test, names database db ("pg", "sqlite", "mysql", "mssql", "oracle"):
|
|
597
|
+
// its lowercased prefix.
|
|
598
|
+
.decl al_db_of(e: node, db: symbol)
|
|
599
|
+
al_db_of(e, db) :- al_db_str(e, s), y = @lower(s), ((prefix("postgres", y), db = "pg") ; (prefix("sqlite", y),
|
|
600
|
+
db = "sqlite") ; (prefix("mysql", y), db = "mysql") ; (prefix("mariadb", y), db = "mysql") ; (prefix("mssql", y),
|
|
601
|
+
db = "mssql") ; (prefix("oracle", y), db = "oracle")).
|
|
602
|
+
.decl al_db_str(e: node, s: symbol)
|
|
603
|
+
al_db_str(e, s) :- al_cond(c), kind(c, "comparison_operator"), child(c, e, _), unparen(e, u), str(u, s).
|
|
604
|
+
al_db_str(e, s) :- al_cond(c), kind(c, "comparison_operator"), child(c, l, _), unparen(l, u), (kind(u, "set") ;
|
|
605
|
+
kind(u, "tuple") ; kind(u, "list")), child(u, e, _), str(e, s).
|
|
606
|
+
// al_cond(x): x is a test the dialect reading looks at: an if/elif condition, a part of one, the value a test
|
|
607
|
+
// function returns (any function whose body is one return: another file may call it), or the value a tested name
|
|
608
|
+
// was assigned.
|
|
609
|
+
.decl al_cond(x: node)
|
|
610
|
+
al_cond(x) :- (kind(s, "if_statement") ; kind(s, "elif_clause")), field(s, "condition", c), unparen(c, x).
|
|
611
|
+
al_cond(x) :- al_cond(c), kind(c, "not_operator"), field(c, "argument", a), unparen(a, x).
|
|
612
|
+
al_cond(x) :- al_cond(c), kind(c, "boolean_operator"), (field(c, "left", a) ; field(c, "right", a)), unparen(a, x).
|
|
613
|
+
al_cond(x) :- al_returns(_, x).
|
|
614
|
+
al_cond(x) :- al_cond(c), kind(c, "identifier"), text(c, v), nearest(c, s, scope), rasg(s, v, r, _), unparen(r, x).
|
|
615
|
+
// al_test_fn(c, g): call c calls g, a function of its own file (by name, or through sym_of).
|
|
616
|
+
.decl al_test_fn(c: node, g: node)
|
|
617
|
+
al_test_fn(c, g) :- al_cond(c), kind(c, "call"), field(c, "function", fn), sym_of(fn, q), docof(c, d), qualname(g, q),
|
|
618
|
+
kind(g, "function_definition"), docof(g, d).
|
|
619
|
+
al_test_fn(c, g) :- al_cond(c), kind(c, "call"), field(c, "function", fn), kind(fn, "identifier"), text(fn, x),
|
|
620
|
+
docof(c, d), kind(g, "function_definition"), def_name(g, x), nearest(g, m, scope), kind(m, "module"), docof(g, d).
|
|
621
|
+
// al_test_ext(c, q): call c calls q, a function of another module of the run (one c's file reads, py_names).
|
|
622
|
+
.decl al_test_ext(c: node, q: symbol)
|
|
623
|
+
al_test_ext(c, q) :- al_cond(c), kind(c, "call"), field(c, "function", fn), sym_of(fn, q), !al_test_fn(c, _),
|
|
624
|
+
docof(c, d), doc(d, p, _), py_names__reads_at(p, m), prefix(cat(m, "."), q).
|
|
625
|
+
// al_returns(g, x): g's body is one `return x` (after any docstring).
|
|
626
|
+
.decl al_returns(g: node, x: node)
|
|
627
|
+
al_returns(g, x) :- kind(g, "function_definition"), field(g, "body", b), body_stmt(b, r, _), kind(r, "return_statement"),
|
|
628
|
+
!al_other_stmt(b), child(r, v, _), unparen(v, x).
|
|
629
|
+
.decl al_other_stmt(b: node)
|
|
630
|
+
al_other_stmt(b) :- body_stmt(b, s, _), !kind(s, "return_statement"), !doc_stmt(s, _).
|
|
631
|
+
// al_db_if(x, when, db, pol): when x is `when` ("true" or "false"), the database is db (pol "is") or is not (pol "not").
|
|
632
|
+
.decl al_db_if(x: node, when: symbol, db: symbol, pol: symbol)
|
|
633
|
+
al_db_if(x, "true", db, "is") :- al_cond(x), cmp(x, "==", l, r), (al_db_of(l, db) ; al_db_of(r, db)).
|
|
634
|
+
al_db_if(x, "false", db, "not") :- al_cond(x), cmp(x, "==", l, r), (al_db_of(l, db) ; al_db_of(r, db)).
|
|
635
|
+
al_db_if(x, "true", db, "not") :- al_cond(x), cmp(x, "!=", l, r), (al_db_of(l, db) ; al_db_of(r, db)).
|
|
636
|
+
al_db_if(x, "false", db, "is") :- al_cond(x), cmp(x, "!=", l, r), (al_db_of(l, db) ; al_db_of(r, db)).
|
|
637
|
+
al_db_if(x, "true", db, "is") :- al_cond(x), cmp(x, "in", l, _), al_db_of(l, db).
|
|
638
|
+
al_db_if(x, "true", db, "is") :- al_cond(x), cmp(x, "in", _, r), unparen(r, u), al_db_set(u, db).
|
|
639
|
+
al_db_if(x, "false", db, "not") :- al_cond(x), cmp(x, "in", _, r), unparen(r, u), al_db_set(u, db).
|
|
640
|
+
al_db_if(x, "true", db, "not") :- al_cond(x), cmp(x, "not in", _, r), unparen(r, u), al_db_set(u, db).
|
|
641
|
+
al_db_if(x, "true", db, "is") :- al_cond(x), al_isinstance_db(x, db).
|
|
642
|
+
al_db_if(x, "false", db, "not") :- al_cond(x), al_isinstance_db(x, db).
|
|
643
|
+
al_db_if(x, "true", db, p) :- kind(x, "not_operator"), field(x, "argument", a), unparen(a, y), al_db_if(y, "false", db, p).
|
|
644
|
+
al_db_if(x, "false", db, p) :- kind(x, "not_operator"), field(x, "argument", a), unparen(a, y), al_db_if(y, "true", db, p).
|
|
645
|
+
al_db_if(x, "true", db, p) :- kind(x, "boolean_operator"), op(x, "and"), (field(x, "left", a) ; field(x, "right", a)),
|
|
646
|
+
unparen(a, y), al_db_if(y, "true", db, p).
|
|
647
|
+
al_db_if(x, "false", db, p) :- kind(x, "boolean_operator"), op(x, "or"), (field(x, "left", a) ; field(x, "right", a)),
|
|
648
|
+
unparen(a, y), al_db_if(y, "false", db, p).
|
|
649
|
+
al_db_if(x, w, db, p) :- al_test_fn(x, g), al_returns(g, y), al_db_if(y, w, db, p).
|
|
650
|
+
al_db_if(x, w, db, p) :- al_cond(x), kind(x, "identifier"), text(x, v), nearest(x, s, scope), rasg(s, v, r, _),
|
|
651
|
+
unparen(r, y), al_db_if(y, w, db, p).
|
|
652
|
+
// al_db_if_ext(x, when, q, w): when x is `when`, the call of function q of another module (al_test_ext) is w: the
|
|
653
|
+
// same structure as al_db_if, with that call in place of a test it reads.
|
|
654
|
+
.decl al_db_if_ext(x: node, when: symbol, q: symbol, w: symbol)
|
|
655
|
+
al_db_if_ext(x, "true", q, "true") :- al_test_ext(x, q).
|
|
656
|
+
al_db_if_ext(x, "false", q, "false") :- al_test_ext(x, q).
|
|
657
|
+
al_db_if_ext(x, "true", q, w) :- kind(x, "not_operator"), field(x, "argument", a), unparen(a, y),
|
|
658
|
+
al_db_if_ext(y, "false", q, w).
|
|
659
|
+
al_db_if_ext(x, "false", q, w) :- kind(x, "not_operator"), field(x, "argument", a), unparen(a, y),
|
|
660
|
+
al_db_if_ext(y, "true", q, w).
|
|
661
|
+
al_db_if_ext(x, "true", q, w) :- kind(x, "boolean_operator"), op(x, "and"), (field(x, "left", a) ;
|
|
662
|
+
field(x, "right", a)), unparen(a, y), al_db_if_ext(y, "true", q, w).
|
|
663
|
+
al_db_if_ext(x, "false", q, w) :- kind(x, "boolean_operator"), op(x, "or"), (field(x, "left", a) ;
|
|
664
|
+
field(x, "right", a)), unparen(a, y), al_db_if_ext(y, "false", q, w).
|
|
665
|
+
al_db_if_ext(x, wx, q, w) :- al_test_fn(x, g), al_returns(g, y), al_db_if_ext(y, wx, q, w).
|
|
666
|
+
al_db_if_ext(x, wx, q, w) :- al_cond(x), kind(x, "identifier"), text(x, v), nearest(x, s, scope), rasg(s, v, r, _),
|
|
667
|
+
unparen(r, y), al_db_if_ext(y, wx, q, w).
|
|
668
|
+
// A test function's answers, stated in its own file by its qualified name: al_fn_db_at(d, q, w, db, pol), when
|
|
669
|
+
// function q's returned test is w, the database is db (pol "is") or is not; al_fn_via_at(d, q, w, r, w2), when it is
|
|
670
|
+
// w, the call of r of another module is w2. al_fn_db follows them across modules: it spans files on purpose
|
|
671
|
+
// (cross_file), keyed by qualified names and values alone. al_fn_non_pg(q, w): when q's test is w, the database is
|
|
672
|
+
// not PostgreSQL.
|
|
673
|
+
.decl al_fn_db_at(d: number, q: symbol, w: symbol, db: symbol, pol: symbol)
|
|
674
|
+
al_fn_db_at(d, q, w, db, p) :- al_returns(g, y), al_db_if(y, w, db, p), qualname(g, q), docof(g, d).
|
|
675
|
+
.decl al_fn_via_at(d: number, q: symbol, w: symbol, r: symbol, w2: symbol)
|
|
676
|
+
al_fn_via_at(d, q, w, r, w2) :- al_returns(g, y), al_db_if_ext(y, w, r, w2), qualname(g, q), docof(g, d).
|
|
677
|
+
.decl al_fn_db(q: symbol, w: symbol, db: symbol, pol: symbol) cross_file
|
|
678
|
+
al_fn_db(q, w, db, p) :- al_fn_db_at(_, q, w, db, p).
|
|
679
|
+
al_fn_db(q, w, db, p) :- al_fn_via_at(_, q, w, r, w2), al_fn_db(r, w2, db, p).
|
|
680
|
+
.decl al_fn_non_pg(q: symbol, w: symbol)
|
|
681
|
+
al_fn_non_pg(q, w) :- al_fn_db(q, w, "pg", "not").
|
|
682
|
+
al_fn_non_pg(q, w) :- al_fn_db(q, w, db, "is"), db != "pg".
|
|
683
|
+
// al_db_set(u, db): a set, tuple or list literal whose every element names db (a group of other databases counts as
|
|
684
|
+
// "other": `{"mysql", "mariadb"}` is mysql, `("sqlite", "mysql")` is other).
|
|
685
|
+
.decl al_db_set(u: node, db: symbol)
|
|
686
|
+
al_db_set(u, db) :- al_db_set_one(u, db).
|
|
687
|
+
al_db_set(u, "other") :- al_db_str(e, _), child(u, e, _), n = count : { child(u, _, _) },
|
|
688
|
+
n = count : { child(u, f, _), al_db_of(f, b), b != "pg" }, !al_db_set_one(u, _).
|
|
689
|
+
.decl al_db_set_one(u: node, db: symbol)
|
|
690
|
+
al_db_set_one(u, db) :- al_db_of(e, db), child(u, e, _), n = count : { child(u, _, _) },
|
|
691
|
+
n = count : { child(u, f, _), al_db_of(f, db) }.
|
|
692
|
+
// al_isinstance_db(x, db): `isinstance(_, K)` with K a class of SQLAlchemy's dialect package for db.
|
|
693
|
+
.decl al_isinstance_db(x: node, db: symbol)
|
|
694
|
+
al_isinstance_db(x, db) :- al_cond(x), kind(x, "call"), field(x, "function", fn), text(fn, "isinstance"),
|
|
695
|
+
call_arg(x, 1, "", k), sym_of(k, q), al_dialect_pkg(p, db), prefix(cat(cat("sqlalchemy.dialects.", p), "."), q).
|
|
696
|
+
.decl al_dialect_pkg(p: symbol, db: symbol)
|
|
697
|
+
al_dialect_pkg("postgresql", "pg"). al_dialect_pkg("sqlite", "sqlite"). al_dialect_pkg("mysql", "mysql").
|
|
698
|
+
al_dialect_pkg("mssql", "mssql"). al_dialect_pkg("oracle", "oracle").
|
|
699
|
+
// al_non_pg_when(x, w): when x is w, the database is not PostgreSQL.
|
|
700
|
+
.decl al_non_pg_when(x: node, w: symbol)
|
|
701
|
+
al_non_pg_when(x, w) :- al_db_if(x, w, "pg", "not").
|
|
702
|
+
al_non_pg_when(x, w) :- al_db_if(x, w, db, "is"), db != "pg".
|
|
703
|
+
// al_non_pg(n): n runs only on another database, by the tests of its own file: the body of an if/elif whose test,
|
|
704
|
+
// when true, rules PostgreSQL out; the elif/else part of an if whose test, when false, rules it out (`if
|
|
705
|
+
// _is_pg(conn): ... else:`, `if not isinstance(d, SQLiteDialect): ... else:`); or code after a guard `if <test>:
|
|
706
|
+
// return` whose test, when false, rules it out (`if not _is_mysql(): return`, `if _is_pg(c): return`). `if dialect
|
|
707
|
+
// != "postgresql": return` leaves what follows on PostgreSQL. A call under a test function of another module is
|
|
708
|
+
// al_np_ext's: a rule asks `!al_non_pg(c), al_term(c, d, t), !al_np_ext(d, t)`.
|
|
709
|
+
.decl al_non_pg(n: node)
|
|
710
|
+
al_non_pg(n) :- (kind(s, "if_statement") ; kind(s, "elif_clause")), field(s, "condition", c), unparen(c, x),
|
|
711
|
+
al_non_pg_when(x, "true"), field(s, "consequence", b), within(n, b).
|
|
712
|
+
al_non_pg(n) :- kind(s, "if_statement"), field(s, "condition", c), unparen(c, x), al_non_pg_when(x, "false"),
|
|
713
|
+
field(s, "alternative", e), within(n, e).
|
|
714
|
+
al_non_pg(n) :- kind(g, "function_definition"), field(g, "body", fb), body_stmt(fb, s, i), al_non_pg_guard(s),
|
|
715
|
+
body_stmt(fb, s2, j), j > i, within(n, s2).
|
|
716
|
+
al_non_pg(n) :- kind(g, "function_definition"), field(g, "body", fb), body_stmt(fb, s, i), al_non_pg_guard(s),
|
|
717
|
+
body_stmt(fb, n, j), j > i.
|
|
718
|
+
.decl al_non_pg_guard(s: node)
|
|
719
|
+
al_non_pg_guard(s) :- kind(s, "if_statement"), field(s, "condition", c), unparen(c, x), al_non_pg_when(x, "false"),
|
|
720
|
+
al_return_only(s).
|
|
721
|
+
.decl al_return_only(s: node)
|
|
722
|
+
al_return_only(s) :- !field(s, "alternative", _), field(s, "consequence", b), body_len(b, 1), body_stmt(b, r, 0),
|
|
723
|
+
kind(r, "return_statement").
|
|
724
|
+
// al_np_dep(n, q, w): call n sits where al_non_pg would hold if function q of another module, when its test is w,
|
|
725
|
+
// ruled PostgreSQL out: the same branches and guards, with the test read through al_db_if_ext.
|
|
726
|
+
.decl al_np_dep(n: node, q: symbol, w: symbol)
|
|
727
|
+
al_np_dep(n, q, w) :- (kind(s, "if_statement") ; kind(s, "elif_clause")), field(s, "condition", c), unparen(c, x),
|
|
728
|
+
al_db_if_ext(x, "true", q, w), field(s, "consequence", b), within(n, b), kind(n, "call").
|
|
729
|
+
al_np_dep(n, q, w) :- kind(s, "if_statement"), field(s, "condition", c), unparen(c, x), al_db_if_ext(x, "false", q, w),
|
|
730
|
+
field(s, "alternative", e), within(n, e), kind(n, "call").
|
|
731
|
+
al_np_dep(n, q, w) :- kind(g, "function_definition"), field(g, "body", fb), body_stmt(fb, s, i), kind(s, "if_statement"),
|
|
732
|
+
field(s, "condition", c), unparen(c, x), al_db_if_ext(x, "false", q, w), al_return_only(s), body_stmt(fb, s2, j),
|
|
733
|
+
j > i, within(n, s2), kind(n, "call").
|
|
734
|
+
// al_np_ext(d, t): the call of document d whose term is t (al_term) runs only on another database, by a test function
|
|
735
|
+
// of another module (al_fn_non_pg).
|
|
736
|
+
.decl al_np_dep_at(d: number, t: symbol, q: symbol, w: symbol)
|
|
737
|
+
al_np_dep_at(d, t, q, w) :- al_np_dep(n, q, w), al_term(n, d, t).
|
|
738
|
+
.decl al_np_ext(d: number, t: symbol)
|
|
739
|
+
al_np_ext(d, t) :- al_np_dep_at(d, t, q, w), al_fn_non_pg(q, w).
|
|
740
|
+
|
|
741
|
+
// ---- columns -----------------------------------------------------------------------------------------------------
|
|
742
|
+
.decl al_column_q(q: symbol)
|
|
743
|
+
al_column_q("sqlalchemy.Column"). al_column_q("sqlalchemy.schema.Column"). al_column_q("sqlalchemy.sql.schema.Column").
|
|
744
|
+
al_column_q("sqlmodel.Column").
|
|
745
|
+
.decl al_column_call(c: node)
|
|
746
|
+
al_column_call(c) :- kind(c, "call"), field(c, "function", f), sym_of(f, q), al_column_q(q).
|
|
747
|
+
.decl al_column_name(c: node, x: symbol)
|
|
748
|
+
al_column_name(c, x) :- al_column_call(c), call_arg(c, 0, "", a), al_name_value(a, x).
|
|
749
|
+
// al_added_column(c, col): add_column c adds Column col: inline, or bound to a name in the same scope first
|
|
750
|
+
// (`col = sa.Column(...); op.add_column("users", col)`).
|
|
751
|
+
.decl al_added_column(c: node, col: node)
|
|
752
|
+
al_added_column(c, col) :- al_call(c, "add_column", _), al_role(c, "column", e), unparen(e, col), al_column_call(col).
|
|
753
|
+
al_added_column(c, col) :- al_call(c, "add_column", _), al_role(c, "column", e), unparen(e, u), kind(u, "identifier"),
|
|
754
|
+
text(u, x), nearest(u, s, scope), rasg(s, x, r, _), unparen(r, col), al_column_call(col).
|
|
755
|
+
|
|
756
|
+
// ---- models ------------------------------------------------------------------------------------------------------
|
|
757
|
+
// al_class_stmt(k, a): statement a of class k's body is `x = v`, `x: T = v` or a bare `x: T`.
|
|
758
|
+
.decl al_class_stmt(k: node, a: node)
|
|
759
|
+
al_class_stmt(k, a) :- kind(k, "class_definition"), field(k, "body", b), body_stmt(b, s, _), kind(s, "expression_statement"),
|
|
760
|
+
child(s, a, 0), kind(a, "assignment"), field(a, "left", l), kind(l, "identifier").
|
|
761
|
+
.decl al_tablename(k: node, t: symbol)
|
|
762
|
+
al_tablename(k, t) :- al_class_stmt(k, a), field(a, "left", l), text(l, "__tablename__"), field(a, "right", v),
|
|
763
|
+
al_name_value(v, t).
|
|
764
|
+
// al_sqlmodel_table(k): `class K(..., table=True)`: a sqlmodel table class.
|
|
765
|
+
.decl al_sqlmodel_table(k: node)
|
|
766
|
+
al_sqlmodel_table(k) :- kind(k, "class_definition"), field(k, "superclasses", al), child(al, kw, _),
|
|
767
|
+
kind(kw, "keyword_argument"), field(kw, "name", nm), text(nm, "table"), field(kw, "value", v), kind(v, "true").
|
|
768
|
+
.decl al_model(k: node, t: symbol)
|
|
769
|
+
al_model(k, t) :- al_tablename(k, t).
|
|
770
|
+
al_model(k, t) :- al_sqlmodel_table(k), !al_tablename(k, _), def_name(k, x), t = @lower(x).
|
|
771
|
+
// What a class's ancestors hold is read by symbol (decision 0677): a class's ancestry through python/names'
|
|
772
|
+
// class_anc (the phase py_names), and what each class of the run declares by its own file, under its qualified name
|
|
773
|
+
// (al_sqlmodel_field_at, a rule's own *_at relations). A rule joins them by symbol, never a class node of another file.
|
|
774
|
+
// al_model_q(q): class q of the run names its table (al_model). It spans files on purpose (cross_file): the run's
|
|
775
|
+
// mapped classes, by qualified name alone.
|
|
776
|
+
.decl al_model_q_at(d: number, q: symbol)
|
|
777
|
+
al_model_q_at(d, q) :- al_model(k, _), class_def(k, q), docof(k, d).
|
|
778
|
+
.decl al_model_q(q: symbol) cross_file
|
|
779
|
+
al_model_q(q) :- al_model_q_at(_, q).
|
|
780
|
+
.decl al_mapped_value(v: node)
|
|
781
|
+
al_mapped_value(v) :- unparen(v, c), al_column_call(c).
|
|
782
|
+
al_mapped_value(v) :- unparen(v, c), kind(c, "call"), field(c, "function", f), sym_of(f, "sqlalchemy.orm.mapped_column").
|
|
783
|
+
.decl al_explicit_name(v: node, x: symbol)
|
|
784
|
+
al_explicit_name(v, x) :- unparen(v, c), kind(c, "call"), al_mapped_value(c), call_arg(c, 0, "", a), unparen(a, s),
|
|
785
|
+
str(s, x).
|
|
786
|
+
// al_class_column(k, x, a): statement a of class k's own body maps column x.
|
|
787
|
+
.decl al_class_column(k: node, x: symbol, a: node)
|
|
788
|
+
// `a = Column("name", ...)`, `a = mapped_column(...)`, with or without an annotation.
|
|
789
|
+
al_class_column(k, x, a) :- al_class_stmt(k, a), field(a, "right", v), al_mapped_value(v), al_explicit_name(v, x).
|
|
790
|
+
al_class_column(k, x, a) :- al_class_stmt(k, a), field(a, "right", v), al_mapped_value(v), !al_explicit_name(v, _),
|
|
791
|
+
field(a, "left", l), text(l, x).
|
|
792
|
+
// A bare `a: Mapped[T]` (SQLAlchemy 2.0 declarative).
|
|
793
|
+
al_class_column(k, x, a) :- al_class_stmt(k, a), !field(a, "right", _), field(a, "type", t), text(t, ty),
|
|
794
|
+
prefix("Mapped[", ty), field(a, "left", l), text(l, x).
|
|
795
|
+
// sqlmodel: every annotated field of a table class that is no Relationship. A class among a table class's bases
|
|
796
|
+
// gives its fields by symbol: al_sqlmodel_field_at.
|
|
797
|
+
al_class_column(k, x, a) :- al_sqlmodel_table(k), al_sqlmodel_field(k, x, a).
|
|
798
|
+
// al_sqlmodel_field(k, x, a): statement a of class k's body is an annotated field x, as sqlmodel reads one: no
|
|
799
|
+
// ClassVar, no Relationship, no leading underscore.
|
|
800
|
+
.decl al_sqlmodel_field(k: node, x: symbol, a: node)
|
|
801
|
+
al_sqlmodel_field(k, x, a) :- al_class_stmt(k, a), field(a, "type", t), text(t, ty), !prefix("ClassVar", ty),
|
|
802
|
+
!al_sqlmodel_relationship(a), field(a, "left", l), text(l, x), !prefix("_", x).
|
|
803
|
+
.decl al_sqlmodel_relationship(a: node)
|
|
804
|
+
al_sqlmodel_relationship(a) :- field(a, "right", v), unparen(v, c), kind(c, "call"), field(c, "function", f),
|
|
805
|
+
sym_of(f, "sqlmodel.Relationship").
|
|
806
|
+
// al_sqlmodel_field_at(d, q, x): class q of document d has the sqlmodel field x, which a table class among q's
|
|
807
|
+
// subclasses maps: by qualified name, in the class's own file.
|
|
808
|
+
.decl al_sqlmodel_field_at(d: number, q: symbol, x: symbol)
|
|
809
|
+
al_sqlmodel_field_at(d, q, x) :- al_sqlmodel_field(k, x, _), class_def(k, q), docof(k, d).
|
|
810
|
+
|
|
811
|
+
// ---- env.py ------------------------------------------------------------------------------------------------------
|
|
812
|
+
.decl al_env_configure(c: node)
|
|
813
|
+
al_env_configure(c) :- kind(c, "call"), field(c, "function", f), sym_of(f, "alembic.context.configure"), docof(c, d),
|
|
814
|
+
al_env_doc(d).
|