Skip to content

Commit 6750b6d

Browse files
committed
refactor(native): read warehouse and migration models through native views
Planner warehouse and migration code read a model's name, destination, key, schema entry, config, references and dependency keys, and iterate the project's models, seeds, functions and sources, through the native view helpers. Reads that serve every resource kind keep the shared attribute dispatch.
1 parent 11f7ff1 commit 6750b6d

18 files changed

Lines changed: 92 additions & 72 deletions

‎crates/sqlbuild-python/src/bindings/_helpers/compiled_views/model_reads.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,12 @@ pub(crate) fn dep_rows(model: &Bound<'_, PyAny>) -> PyResult<Vec<(String, String
305305
}
306306
native_field(model, "dep_rows")?.extract()
307307
}
308+
309+
/// The model's dependency `CompiledObjectKey` tuple.
310+
pub(crate) fn deps<'py>(model: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
311+
let py: Python<'py> = model.py();
312+
if let Some(view) = native_model(model)? {
313+
return Ok(view.deps(py)?.into_bound(py).into_any());
314+
}
315+
native_field(model, "deps")
316+
}

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/column_candidates.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(super) fn candidates<'py>(
4545
let warehouse = native_field(&snapshot, "existing_columns")?
4646
.call_method1("get", (&name, PyTuple::empty(py)))?;
4747
let declared = PyDict::new(py);
48-
let schema = native_field(&model, "schema_entry")?;
48+
let schema = model_reads::schema_entry(&model)?;
4949
if !schema.is_none() {
5050
for column in native_field(&schema, "columns")?.try_iter()? {
5151
let column = column?;

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/discovery.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Native migration discovery over existing inspection and fingerprint cache boundaries.
22
33
use crate::bindings::_helpers::boundary::python_calls::call_with;
4+
use crate::bindings::_helpers::compiled_views::model_reads;
45
use crate::bindings::_helpers::planner_migrations::matching::{
56
match_migration_fingerprints, migration_destination_fingerprints,
67
};
@@ -56,7 +57,7 @@ pub(super) fn discover_model_migrations<'py>(
5657
let declared = discovery_events::declared(&declarations, &state)?;
5758
let names = PyDict::new(py);
5859
for item in &models {
59-
names.set_item(native_field(item, "name")?, item)?;
60+
names.set_item(model_reads::name_object(item)?, item)?;
6061
}
6162
let event_renames = discovery_events::renames(&runtime, &names, &state)?;
6263
for (name, origin) in declared.iter() {
@@ -74,7 +75,7 @@ pub(super) fn discover_model_migrations<'py>(
7475
}
7576
let unbuilt = PyDict::new(py);
7677
for item in &models {
77-
let name = native_field(item, "name")?;
78+
let name = model_reads::name_object(item)?;
7879
if !declared_names.contains(&name)?
7980
&& call_with(
8081
&native_field(&state, "named_fingerprint")?,

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/discovery_events.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use sqlbuild_host::entries::models::ExecutorHost;
44

55
use crate::bindings::_helpers::boundary::python_calls::call_with;
6+
use crate::bindings::_helpers::compiled_views::model_reads;
67
use crate::bindings::_helpers::native_host::host;
78
use crate::bindings::_helpers::planner_migrations::column_objects::config;
89
use crate::bindings::_helpers::planner_migrations::models::ModelMigrationDeclaration;
@@ -110,11 +111,11 @@ pub(super) fn resumed<'py>(
110111
let target = native_field(&native_field(runtime, "project")?, "effective_target_name")?;
111112
let events = native_field(state, "events")?;
112113
for item in models {
113-
let name = native_field(item, "name")?;
114+
let name = model_reads::name_object(item)?;
114115
if !unbuilt.contains(&name)? || !(history(item)? || identity(item)?) {
115116
continue;
116117
}
117-
let destination = native_field(item, "destination")?;
118+
let destination = model_reads::destination(item)?;
118119
let event = newest(&events, &destination)?;
119120
if event.is_none() {
120121
continue;

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/discovery_models.rs‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use sqlbuild_host::entries::models::UserCodeHost;
44

5+
use crate::bindings::_helpers::compiled_views::model_reads;
6+
use crate::bindings::_helpers::compiled_views::project_reads;
57
use crate::bindings::_helpers::graph::node_set::PyNodeSet;
68
use crate::bindings::_helpers::native_host::host;
79
use crate::bindings::_helpers::planner_identity::metadata::build_function_local_hashes;
@@ -19,16 +21,13 @@ use std::collections::BTreeMap;
1921
pub(super) fn ordered<'py>(runtime: &Bound<'py, PyAny>) -> PyResult<Vec<Bound<'py, PyAny>>> {
2022
let mut models: BTreeMap<String, Bound<'py, PyAny>> = BTreeMap::new();
2123
let mut dependencies: BTreeMap<String, Vec<String>> = BTreeMap::new();
22-
for item in native_field(
23-
&runtime_field(runtime, |runtime| &runtime.project)?,
24-
"models",
25-
)?
26-
.try_iter()?
24+
for item in
25+
project_reads::models(&runtime_field(runtime, |runtime| &runtime.project)?)?.try_iter()?
2726
{
2827
let item = item?;
29-
let name: String = native_field(&item, "name")?.extract()?;
28+
let name: String = model_reads::name_object(&item)?.extract()?;
3029
let mut upstream: Vec<String> = Vec::new();
31-
for dependency in native_field(&item, "deps")?.try_iter()? {
30+
for dependency in model_reads::deps(&item)?.try_iter()? {
3231
let dependency = dependency?;
3332
if native_field(&dependency, "resource_type")?.eq("model")? {
3433
upstream.push(native_field(&dependency, "name")?.extract()?);
@@ -63,8 +62,8 @@ pub(super) fn triggered(
6362
let relevant = relevant.0;
6463
let fingerprints = native_field(&native_field(snapshot, "fingerprints")?, "models")?;
6564
for item in models {
66-
let name = native_field(item, "name")?;
67-
if relevant.contains(native_field(item, "key")?)?
65+
let name = model_reads::name_object(item)?;
66+
if relevant.contains(model_reads::model_key(item)?)?
6867
&& !fingerprints.contains(&name)?
6968
&& !declared.contains(&name)?
7069
{
@@ -83,7 +82,7 @@ pub(super) fn metadata<'py>(
8382
let functions = object(
8483
py,
8584
build_function_local_hashes(
86-
&native_field(&project, "functions")?,
85+
&project_reads::functions(&project)?,
8786
native_field(&project, "sql_analysis_dialect")?
8887
.extract::<Option<String>>()?
8988
.as_deref(),
@@ -102,7 +101,7 @@ pub(super) fn metadata<'py>(
102101
let result = PyDict::new(py);
103102
for item in models {
104103
result.set_item(
105-
native_field(item, "name")?,
104+
model_reads::name_object(item)?,
106105
object(
107106
py,
108107
build_model_version_identity_metadata_json(

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/discovery_requests.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Live candidate inspection and automatic request materialization.
22
33
use crate::bindings::_helpers::boundary::python_calls::call_with;
4+
use crate::bindings::_helpers::compiled_views::model_reads;
45
use crate::bindings::_helpers::planner_migrations::column_objects::config;
56
use crate::bindings::_helpers::planner_migrations::column_objects::native;
67
use crate::bindings::_helpers::planner_migrations::discovery_events::{
@@ -114,10 +115,10 @@ pub(super) fn automatic<'py>(
114115
let py = runtime.py();
115116
let mut requests: Vec<ModelMigrationRequest<'py>> = Vec::new();
116117
for item in models {
117-
if !native_field(scope, "selected_keys")?.contains(native_field(item, "key")?)? {
118+
if !native_field(scope, "selected_keys")?.contains(model_reads::model_key(item)?)? {
118119
continue;
119120
}
120-
let name = native_field(item, "name")?;
121+
let name = model_reads::name_object(item)?;
121122
let event = matches.1.get_item(&name)?;
122123
let candidate = mapping_get(matches.0, &name)?;
123124
let origin = if let Some(event) = event {
@@ -162,8 +163,8 @@ pub(super) fn warnings<'py>(
162163
let py = scope.py();
163164
let selected = PySet::empty(py)?;
164165
for item in models {
165-
if native_field(scope, "selected_keys")?.contains(native_field(item, "key")?)? {
166-
selected.add(native_field(item, "name")?)?;
166+
if native_field(scope, "selected_keys")?.contains(model_reads::model_key(item)?)? {
167+
selected.add(model_reads::name_object(item)?)?;
167168
}
168169
}
169170
let ambiguous: BTreeMap<String, String> = ambiguous.extract()?;

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/model_entries.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use sqlbuild_host::entries::models::ExecutorHost;
44

55
use crate::bindings::_helpers::boundary::python_calls::call_with;
6+
use crate::bindings::_helpers::compiled_views::model_reads;
67
use crate::bindings::_helpers::native_host::host;
78
use crate::bindings::_helpers::planner_migrations::column_objects::{lower, model, replace, text};
89
use crate::bindings::_helpers::planner_migrations::compatibility::check_migration_compatibility;
@@ -73,7 +74,7 @@ pub(super) fn decide<'py>(
7374
let py = runtime.py();
7475
let request = decision_request.request;
7576
let item = request.model.clone();
76-
let destination = native_field(&item, "destination")?;
77+
let destination = model_reads::destination(&item)?;
7778
let origin = decision_request.origin.clone();
7879
let snapshot = decision_request.snapshot.clone();
7980
let state = decision_request.state.clone();
@@ -99,7 +100,7 @@ pub(super) fn decide<'py>(
99100
py,
100101
RecordId::ModelMigrationPlanEntry,
101102
&[
102-
("model_name", native_field(&item, "name")?),
103+
("model_name", model_reads::name_object(&item)?),
103104
("discovery", request.discovery.clone()),
104105
(
105106
"decision",
@@ -160,7 +161,7 @@ pub(super) fn decide<'py>(
160161
}
161162
let inherited = if recorded && !applies(&item, &newest, &snapshot)? {
162163
native_field(&native_field(&snapshot, "fingerprints")?, "models")?
163-
.call_method1("get", (native_field(&item, "name")?,))?
164+
.call_method1("get", (model_reads::name_object(&item)?,))?
164165
} else {
165166
empty_version(&item, &fingerprint)?
166167
};

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/model_planning.rs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
use crate::bindings::_helpers::boundary::python_calls::call_with;
44
use crate::bindings::_helpers::boundary::python_calls::keywords;
5+
use crate::bindings::_helpers::compiled_views::model_reads;
6+
use crate::bindings::_helpers::compiled_views::project_reads;
57
use crate::bindings::_helpers::model_config::typed_config::{
68
config_node_of, config_value, model_values,
79
};
@@ -97,11 +99,8 @@ fn discover<'py>(
9799
let scope = phase.scope.clone();
98100
let snapshot = phase.snapshot.clone();
99101
let mut declared_requests: Vec<ModelMigrationRequest<'py>> = Vec::new();
100-
for item in native_field(
101-
&runtime_field(&runtime, |runtime| &runtime.project)?,
102-
"models",
103-
)?
104-
.try_iter()?
102+
for item in
103+
project_reads::models(&runtime_field(&runtime, |runtime| &runtime.project)?)?.try_iter()?
105104
{
106105
if let Some(request) = declared(&item?)? {
107106
declared_requests.push(request);
@@ -117,7 +116,7 @@ fn discover<'py>(
117116
for request in &declared_requests {
118117
let (origin_location, origin_model) = resolve_origin(request, &runtime, state)?;
119118
declarations.push(ModelMigrationDeclaration {
120-
model_name: native_field(&request.model, "name")?,
119+
model_name: model_reads::name_object(&request.model)?,
121120
origin_location,
122121
origin_model,
123122
});
@@ -183,7 +182,7 @@ fn finish<'py>(
183182
let policy = super::planning_boundary::missing_origin_policy(&runtime)?;
184183
for (request, origin, origin_model) in resolved {
185184
let item = request.model.clone();
186-
let name = native_field(&item, "name")?;
185+
let name = model_reads::name_object(&item)?;
187186
let decided = super::model_entries::decide(&MigrationDecisionRequest {
188187
runtime: runtime.clone(),
189188
request,

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/old_name_entries.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::bindings::_helpers::boundary::python_calls::keywords;
44
use crate::bindings::_helpers::boundary::python_calls::{call_with, imported};
5+
use crate::bindings::_helpers::compiled_views::model_reads;
56
use crate::bindings::_helpers::planner_migrations::column_objects::{model, text};
67
use crate::bindings::_helpers::planner_migrations::constants::{PENDING_ARCHIVE, PENDING_VIEW};
78
use crate::bindings::_helpers::planner_migrations::models::OldNamePlanner;
@@ -113,8 +114,7 @@ impl<'py> OldNamePlanner<'py> {
113114
&self,
114115
item: &Bound<'py, PyAny>,
115116
) -> PyResult<Option<Bound<'py, PyAny>>> {
116-
let Some(history) = self.newest(&relation(&native_field(item, "destination")?)?, None)?
117-
else {
117+
let Some(history) = self.newest(&relation(&model_reads::destination(item)?)?, None)? else {
118118
return Ok(None);
119119
};
120120
if !native_field(&native_field(&history, "move")?, "destination_model")?
@@ -324,7 +324,7 @@ impl<'py> OldNamePlanner<'py> {
324324
py,
325325
RecordId::OldNameViewPlanEntry,
326326
&[
327-
("model_name", native_field(item, "name")?),
327+
("model_name", model_reads::name_object(item)?),
328328
(
329329
"origin",
330330
location(&self.runtime, &native_field(history, "old")?)?,

‎crates/sqlbuild-python/src/bindings/_helpers/planner_migrations/old_name_objects.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use sqlbuild_host::entries::models::ExecutorHost;
44

55
use crate::bindings::_helpers::boundary::python_calls::call_with;
6+
use crate::bindings::_helpers::compiled_views::model_reads;
67
use crate::bindings::_helpers::native_host::host;
78
use crate::bindings::_helpers::planner_migrations::column_objects::{config, model};
89
use crate::bindings::_helpers::records::construct::{new_record_with, record_class};
@@ -42,7 +43,7 @@ pub(super) fn retention<'py>(
4243
project: &Bound<'py, PyAny>,
4344
) -> PyResult<Bound<'py, PyAny>> {
4445
old_name_retention(
45-
&native_field(&native_field(item, "config")?, "values")?,
46+
&native_field(&model_reads::config(item)?, "values")?,
4647
project,
4748
)
4849
}
@@ -70,7 +71,7 @@ pub(super) fn recorded_view<'py>(
7071
let created = native_field(history, "created")?;
7172
let mut reused = py.None().into_bound(py);
7273
for item in models {
73-
if relation(&native_field(item, "destination")?)?
74+
if relation(&model_reads::destination(item)?)?
7475
.call_method1("matches", (native_field(history, "old")?,))?
7576
.is_truthy()?
7677
{

0 commit comments

Comments
 (0)