Skip to content

Commit

Permalink
introspection --force: reuse same file if there's a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed May 30, 2024
1 parent c835843 commit a59a498
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions schema-engine/cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ fn execute_postgres(api: TestApi) {
}

#[test_connector(tags(Postgres), exclude(CockroachDb), preview_features("views"))]
fn introspect_postgres(api: TestApi) {
fn introspect_single_postgres_force(api: TestApi) {
/* Drop and create database via `drop-database` and `create-database` */

let connection_string = api.connection_string();
Expand Down Expand Up @@ -696,7 +696,7 @@ fn introspect_postgres(api: TestApi) {
stdout.read_line(&mut response).unwrap();

let expected = expect![[r#"
{"jsonrpc":"2.0","result":{"schema":{"files":[{"content":"generator js {\n provider = \"prisma-client-js\"\n previewFeatures = [\"views\"]\n}\n\ndatasource db {\n provider = \"postgres\"\n url = env(\"TEST_DATABASE_URL\")\n}\n\nmodel A {\n id Int @id @default(autoincrement())\n data String?\n}\n\n/// The underlying view does not contain a valid unique identifier and can therefore currently not be handled by Prisma Client.\nview B {\n col Int?\n\n @@ignore\n}\n","path":"./base_directory_path/introspected.prisma"}]},"views":[{"definition":"SELECT\n 1 AS col;","name":"B","schema":"public"}],"warnings":"*** WARNING ***\n\nThe following views were ignored as they do not have a valid unique identifier or id. This is currently not supported by Prisma Client. Please refer to the documentation on defining unique identifiers in views: https://pris.ly/d/view-identifiers\n - \"B\"\n"},"id":1}
{"jsonrpc":"2.0","result":{"schema":{"files":[{"content":"generator js {\n provider = \"prisma-client-js\"\n previewFeatures = [\"views\"]\n}\n\ndatasource db {\n provider = \"postgres\"\n url = env(\"TEST_DATABASE_URL\")\n}\n\nmodel A {\n id Int @id @default(autoincrement())\n data String?\n}\n\n/// The underlying view does not contain a valid unique identifier and can therefore currently not be handled by Prisma Client.\nview B {\n col Int?\n\n @@ignore\n}\n","path":"/var/folders/wt/rlkwn4mn2872qwqjyykz191m0000gn/T/.tmp2bon3K/schema.prisma"}]},"views":[{"definition":"SELECT\n 1 AS col;","name":"B","schema":"public"}],"warnings":"*** WARNING ***\n\nThe following views were ignored as they do not have a valid unique identifier or id. This is currently not supported by Prisma Client. Please refer to the documentation on defining unique identifiers in views: https://pris.ly/d/view-identifiers\n - \"B\"\n"},"id":1}
"#]];

expected.assert_eq(&response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct IntrospectionContext {
pub composite_type_depth: CompositeTypeDepth,
previous_schema: psl::ValidatedSchema,
namespaces: Option<Vec<String>>,
base_path_directory: PathBuf,
base_directory_path: PathBuf,
}

impl IntrospectionContext {
Expand All @@ -25,14 +25,14 @@ impl IntrospectionContext {
previous_schema: psl::ValidatedSchema,
composite_type_depth: CompositeTypeDepth,
namespaces: Option<Vec<String>>,
base_path_directory: PathBuf,
base_directory_path: PathBuf,
) -> Self {
IntrospectionContext {
previous_schema,
composite_type_depth,
render_config: true,
namespaces,
base_path_directory,
base_directory_path,
}
}

Expand All @@ -58,10 +58,7 @@ impl IntrospectionContext {
}

let previous_schema_config_only = psl::parse_schema_multi(&[(
base_directory_path
.join(INTROSPECTION_FILE_NAME)
.to_string_lossy()
.to_string(),
Self::introspection_file_path_impl(&previous_schema, &base_directory_path).to_string(),
config_blocks.into(),
)])
.unwrap();
Expand Down Expand Up @@ -123,12 +120,19 @@ impl IntrospectionContext {

/// Returns the file name into which new introspection data should be written.
pub fn introspection_file_path(&self) -> std::borrow::Cow<'_, str> {
if self.previous_schema.db.files_count() == 1 {
let file_id = self.previous_schema.db.iter_file_ids().next().unwrap();
Self::introspection_file_path_impl(&self.previous_schema, &self.base_directory_path)
}

self.previous_schema.db.file_name(file_id).into()
fn introspection_file_path_impl<'a>(
previous_schema: &'a psl::ValidatedSchema,
base_directory_path: &PathBuf,

Check failure on line 128 in schema-engine/connectors/schema-connector/src/introspection_context.rs

View workflow job for this annotation

GitHub Actions / clippy linting

writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
) -> std::borrow::Cow<'a, str> {
if previous_schema.db.files_count() == 1 {
let file_id = previous_schema.db.iter_file_ids().next().unwrap();

previous_schema.db.file_name(file_id).into()
} else {
self.base_path_directory
base_directory_path
.join(INTROSPECTION_FILE_NAME)
.to_string_lossy()
.to_string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async fn reintrospect_force_single_file(api: &mut TestApi) -> TestResult {
let input_dms = [("foo.prisma", with_config(foo_dm, api.pure_config()))];

let expected = expect![[r#"
// file: introspected.prisma
// file: foo.prisma
model Post {
id Int @id @default(autoincrement())
}
Expand Down

0 comments on commit a59a498

Please sign in to comment.