| 1 | //! TODO: automatic test fixture capture |
| 2 | |
| 3 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::databaserust-analyzer cargo sqlx-core 0.8.3 database/::Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#; |
| 4 | |
| 5 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::query_builderrust-analyzer cargo sqlx-core 0.8.3 query_builder/::QueryBuilderrust-analyzer cargo sqlx-core 0.8.3 query_builder/QueryBuilder#; |
| 6 | |
| 7 | use indexmaprust-analyzer cargo indexmap 2.14.0 crate/::setrust-analyzer cargo indexmap 2.14.0 set/::IndexSetrust-analyzer cargo indexmap 2.14.0 set/IndexSet#; |
| 8 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/; |
| 9 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::{BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#, HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#}; |
| 10 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#; |
| 11 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#; |
| 12 | |
| 13 | pub type Resultrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Result#<T, E = FixtureErrorrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureError#> = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<T, E>; |
| 14 | |
| 15 | /// A snapshot of the current state of the database. |
| 16 | /// |
| 17 | /// Can be used to generate an `INSERT` fixture for populating an empty database, |
| 18 | /// or in the future it may be possible to generate a fixture from the difference between |
| 19 | /// two snapshots. |
| 20 | pub struct FixtureSnapshotrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#<DB> { |
| 21 | tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables.: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#, Tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#>, |
| 22 | dbrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#db.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<DB>, |
| 23 | } |
| 24 | |
| 25 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, thiserrorrust-analyzer cargo thiserror 2.0.20 crate/::Errorrust-analyzer cargo thiserror-impl 2.0.20 Error:)] |
| 26 | #[error("could not create fixture: {0}")] |
| 27 | pub struct FixtureErrorrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureError#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#); |
| 28 | |
| 29 | pub struct Fixturerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#<DB> { |
| 30 | opsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#ops.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<FixtureOprust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#>, |
| 31 | dbrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#db.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<DB>, |
| 32 | } |
| 33 | |
| 34 | enum FixtureOprust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp# { |
| 35 | Insertrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert# { |
| 36 | tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#table.: TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#, |
| 37 | columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#columns.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ColumnNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/ColumnName#>, |
| 38 | rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#rows.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Valuerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Value#>>, |
| 39 | }, |
| 40 | // TODO: handle updates and deletes by diffing two snapshots |
| 41 | } |
| 42 | |
| 43 | type TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName# = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<str>; |
| 44 | type ColumnNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/ColumnName# = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<str>; |
| 45 | type Valuerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Value# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 46 | |
| 47 | struct Tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table# { |
| 48 | namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name.: TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#, |
| 49 | columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#columns.: IndexSetrust-analyzer cargo indexmap 2.14.0 set/IndexSet#<ColumnNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/ColumnName#>, |
| 50 | rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#rows.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Valuerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Value#>>, |
| 51 | foreign_keysrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#foreign_keys.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<ColumnNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/ColumnName#, (TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#, ColumnNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/ColumnName#)>, |
| 52 | } |
| 53 | |
| 54 | macro_rules! fixture_assertrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/fixture_assert! ( |
| 55 | ($cond:expr, $msg:literal $($arg:tt)*) => { |
| 56 | if !($cond) { |
| 57 | return Err(FixtureError(format!($msg $($arg)*))) |
| 58 | } |
| 59 | } |
| 60 | ); |
| 61 | |
| 62 | impl<DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#> FixtureSnapshotrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#<DB> { |
| 63 | /// Generate a fixture to reproduce this snapshot from an empty database using `INSERT`s. |
| 64 | /// |
| 65 | /// Note that this doesn't take into account any triggers that might modify the data before |
| 66 | /// it's stored. |
| 67 | /// |
| 68 | /// The `INSERT` statements are ordered on a best-effort basis to satisfy any foreign key |
| 69 | /// constraints (data from tables with no foreign keys are inserted first, then the tables |
| 70 | /// that reference those tables, and so on). |
| 71 | /// |
| 72 | /// If a cycle in foreign-key constraints is detected, this returns with an error. |
| 73 | pub fn additive_fixturerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/impl#[`FixtureSnapshot<DB>`]additive_fixture().(&self) -> Resultrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Result#<Fixturerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#<DB>> { |
| 74 | let visit_order = self.calculate_visit_orderrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/impl#[`FixtureSnapshot<DB>`]calculate_visit_order().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 75 | |
| 76 | let mut ops = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 77 | |
| 78 | for table_name in visit_order { |
| 79 | let table = self.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables..getrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]get().(&table_name).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 80 | |
| 81 | ops.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(FixtureOprust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#::Insertrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert# { |
| 82 | tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#table.: table_name, |
| 83 | columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#columns.: table.columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#columns..iterrust-analyzer cargo indexmap 2.14.0 set/impl#[`IndexSet<T, S>`]iter().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 84 | rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#rows.: table.rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#rows..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Fixturerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture# { opsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#ops., dbrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#db.: self.dbrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#db. }) |
| 89 | } |
| 90 | |
| 91 | /// Determine an order for outputting `INSERTS` for each table by calculating the max |
| 92 | /// length of all its foreign key chains. |
| 93 | /// |
| 94 | /// This should hopefully ensure that there are no foreign-key errors. |
| 95 | fn calculate_visit_orderrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/impl#[`FixtureSnapshot<DB>`]calculate_visit_order().(&self) -> Resultrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#>> { |
| 96 | let mut table_depths = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::with_capacityrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]with_capacity().(self.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]len().()); |
| 97 | let mut visited_set = IndexSetrust-analyzer cargo indexmap 2.14.0 set/IndexSet#::with_capacityrust-analyzer cargo indexmap 2.14.0 set/impl#[`IndexSet<T>`]with_capacity().(self.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]len().()); |
| 98 | |
| 99 | for table in self.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables..valuesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]values().() { |
| 100 | foreign_key_depthrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/foreign_key_depth().(&self.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables., table, &mut table_depths, &mut visited_set)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 101 | visited_set.clearrust-analyzer cargo indexmap 2.14.0 set/impl#[`IndexSet<T, S>`]clear().(); |
| 102 | } |
| 103 | |
| 104 | let mut table_names: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#> = table_depths.keysrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]keys().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 105 | table_names.sort_by_keyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by_key().(|name| table_depths.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(name).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 106 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(table_names) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// Implements `ToString` but not `Display` because it uses [`QueryBuilder`] internally, |
| 111 | /// which appends to an internal string. |
| 112 | #[allow(clippy::to_string_trait_impl)] |
| 113 | impl<DB: Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#> ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString# for Fixturerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#<DB> |
| 114 | where |
| 115 | for<'a> <DB as Databaserust-analyzer cargo sqlx-core 0.8.3 database/Database#>::Argumentsrust-analyzer cargo sqlx-core 0.8.3 database/Database#Arguments#<'a>: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#, |
| 116 | { |
| 117 | fn to_stringrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/impl#[`Fixture<DB>`][ToString]to_string().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 118 | let mut query = QueryBuilderrust-analyzer cargo sqlx-core 0.8.3 query_builder/QueryBuilder#::<DB>::newrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]new().(""); |
| 119 | |
| 120 | for op in &self.opsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Fixture#ops. { |
| 121 | match op { |
| 122 | FixtureOprust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#::Insertrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert# { |
| 123 | tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#table., |
| 124 | columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#columns., |
| 125 | rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureOp#Insert#rows., |
| 126 | } => { |
| 127 | // Sanity check, empty tables shouldn't appear in snapshots anyway. |
| 128 | if columns.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() || rows.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | query.pushrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]push().(format_argsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/format_args!!("INSERT INTO {table} (")); |
| 133 | |
| 134 | let mut separated = query.separatedrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]separated().(", "); |
| 135 | |
| 136 | for column in columns { |
| 137 | separated.pushrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`Separated<'qb, 'args, DB, Sep>`]push().(column); |
| 138 | } |
| 139 | |
| 140 | query.pushrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]push().(")\n"); |
| 141 | |
| 142 | query.push_valuesrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]push_values().(rows, |mut separated, row| { |
| 143 | for value in row { |
| 144 | separated.pushrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`Separated<'qb, 'args, DB, Sep>`]push().(value); |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | query.pushrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]push().(";\n"); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | query.into_sqlrust-analyzer cargo sqlx-core 0.8.3 query_builder/impl#[`QueryBuilder<'args, DB>`]into_sql().() |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | fn foreign_key_depthrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/foreign_key_depth().( |
| 158 | tables: &BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#, Tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#>, |
| 159 | table: &Tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#, |
| 160 | depths: &mut HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#, usize>, |
| 161 | visited_set: &mut IndexSetrust-analyzer cargo indexmap 2.14.0 set/IndexSet#<TableNamerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/TableName#>, |
| 162 | ) -> Resultrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Result#<usize> { |
| 163 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&depth) = depths.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&table.namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name.) { |
| 164 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(depth); |
| 165 | } |
| 166 | |
| 167 | // This keeps us from looping forever. |
| 168 | fixture_assertrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/fixture_assert!!( |
| 169 | visited_set.insertrust-analyzer cargo indexmap 2.14.0 set/impl#[`IndexSet<T, S>`]insert().(table.namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().()), |
| 170 | "foreign key cycle detected: {:?} -> {:?}", |
| 171 | visited_set, |
| 172 | table.namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name. |
| 173 | ); |
| 174 | |
| 175 | let mut refdepth = 0; |
| 176 | |
| 177 | for (colname, (refname, refcol)) in &table.foreign_keysrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#foreign_keys. { |
| 178 | let referenced = tables.getrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]get().(refname).ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| { |
| 179 | FixtureErrorrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureError#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 180 | "table {:?} in foreign key `{}.{} references {}.{}` does not exist", |
| 181 | refname, table.namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name., colname, refname, refcol |
| 182 | )) |
| 183 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 184 | |
| 185 | refdepth = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().( |
| 186 | refdepth, |
| 187 | foreign_key_depthrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/foreign_key_depth().(tables, referenced, depths, visited_set)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | let depth = refdepth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 192 | |
| 193 | depths.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(table.namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().(), depth); |
| 194 | |
| 195 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(depth) |
| 196 | } |
| 197 | |
| 198 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 199 | #[cfg(feature = "any")] |
| 200 | fn test_additive_fixturerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/test_additive_fixture().() -> Resultrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Result#<()> { |
| 201 | // Just need something that implements `Database` |
| 202 | use craterust-analyzer cargo sqlx-core 0.8.3 crate/::anyrust-analyzer cargo sqlx-core 0.8.3 any/::Anyrust-analyzer cargo sqlx-core 0.8.3 any/database/Any#; |
| 203 | |
| 204 | let mut snapshot = FixtureSnapshotrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot# { |
| 205 | tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables.: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V>`]new().(), |
| 206 | dbrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#db.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#::<Anyrust-analyzer cargo sqlx-core 0.8.3 any/database/Any#>, |
| 207 | }; |
| 208 | |
| 209 | snapshot.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables..insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().( |
| 210 | "foo".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 211 | Tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table# { |
| 212 | namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name.: "foo".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 213 | columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#columns.: ["foo_id", "foo_a", "foo_b"] |
| 214 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 215 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::<str>::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<str>`][`From<&str>`]from().) |
| 216 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 217 | rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#rows.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["1".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "'asdf'".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "true".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()]], |
| 218 | foreign_keysrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#foreign_keys.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().(), |
| 219 | }, |
| 220 | ); |
| 221 | |
| 222 | // foreign-keyed to `foo` |
| 223 | // since `tables` is a `BTreeMap` we would expect a naive algorithm to visit this first. |
| 224 | snapshot.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables..insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().( |
| 225 | "bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 226 | Tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table# { |
| 227 | namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name.: "bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 228 | columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#columns.: ["bar_id", "foo_id", "bar_a", "bar_b"] |
| 229 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 230 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::<str>::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<str>`][`From<&str>`]from().) |
| 231 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 232 | rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#rows.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 233 | "1234".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 234 | "1".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 235 | "'2022-07-22 23:27:48.775113301+00:00'".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 236 | "3.14".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 237 | ]], |
| 238 | foreign_keysrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#foreign_keys.: [("foo_id".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), ("foo".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "foo_id".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))] |
| 239 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 240 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 241 | }, |
| 242 | ); |
| 243 | |
| 244 | // foreign-keyed to both `foo` and `bar` |
| 245 | snapshot.tablesrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/FixtureSnapshot#tables..insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().( |
| 246 | "baz".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 247 | Tablerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table# { |
| 248 | namerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#name.: "baz".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 249 | columnsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#columns.: ["baz_id", "bar_id", "foo_id", "baz_a", "baz_b"] |
| 250 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 251 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::<str>::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<str>`][`From<&str>`]from().) |
| 252 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 253 | rowsrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#rows.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 254 | "5678".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 255 | "1234".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 256 | "1".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 257 | "'2022-07-22 23:27:48.775113301+00:00'".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 258 | "3.14".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 259 | ]], |
| 260 | foreign_keysrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/Table#foreign_keys.: [ |
| 261 | ("foo_id".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), ("foo".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "foo_id".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())), |
| 262 | ("bar_id".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), ("bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), "bar_id".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())), |
| 263 | ] |
| 264 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 265 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 266 | }, |
| 267 | ); |
| 268 | |
| 269 | let fixture = snapshot.additive_fixturerust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/impl#[`FixtureSnapshot<DB>`]additive_fixture().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 270 | |
| 271 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 272 | fixture.to_stringrust-analyzer cargo sqlx-core 0.8.3 testing/fixtures/impl#[`Fixture<DB>`][ToString]to_string().(), |
| 273 | "INSERT INTO foo (foo_id, foo_a, foo_b)\n\ |
| 274 | VALUES (1, 'asdf', true);\n\ |
| 275 | INSERT INTO bar (bar_id, foo_id, bar_a, bar_b)\n\ |
| 276 | VALUES (1234, 1, '2022-07-22 23:27:48.775113301+00:00', 3.14);\n\ |
| 277 | INSERT INTO baz (baz_id, bar_id, foo_id, baz_a, baz_b)\n\ |
| 278 | VALUES (5678, 1234, 1, '2022-07-22 23:27:48.775113301+00:00', 3.14);\n" |
| 279 | ); |
| 280 | |
| 281 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 282 | } |