Skip to content

Commit

Permalink
Merge branch 'alpha' into use-rewritten-introspection
Browse files Browse the repository at this point in the history
# Conflicts:
#	server/.buildkite/build-cli
#	server/prisma-rs/libs/database-introspection/src/lib.rs
  • Loading branch information
mavilein committed Aug 23, 2019
2 parents 0f9ca7a + bb28e2b commit 64b9504
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 40 deletions.
2 changes: 1 addition & 1 deletion server/.buildkite/build-cli
16 changes: 14 additions & 2 deletions server/prisma-rs/libs/database-introspection/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(missing_docs)]
//! Database introspection.
use failure::Fail;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
Expand All @@ -19,6 +21,7 @@ pub type IntrospectionResult<T> = core::result::Result<T, IntrospectionError>;

/// Connection abstraction for the introspection connectors.
pub trait IntrospectionConnection: Send + Sync + 'static {
/// Make raw SQL query.
fn query_raw(&self, sql: &str, schema: &str) -> prisma_query::Result<prisma_query::connector::ResultSet>;
}

Expand Down Expand Up @@ -68,6 +71,7 @@ impl DatabaseSchema {
self.table(&name).unwrap()
}

/// Get a sequence.
pub fn get_sequence(&self, name: &str) -> Option<&Sequence> {
self.sequences.iter().find(|x| x.name == name)
}
Expand Down Expand Up @@ -135,6 +139,13 @@ impl Table {
}
}
}
/// The type of an index.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum IndexType {
Unique,
Normal,
}

/// An index of a table.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
Expand All @@ -144,8 +155,8 @@ pub struct Index {
pub name: String,
/// Index columns.
pub columns: Vec<String>,
/// Is index unique?
pub unique: bool,
/// Type of index.
pub tpe: IndexType,
}

/// The primary key of a table.
Expand Down Expand Up @@ -291,6 +302,7 @@ pub struct ForeignKey {
pub referenced_table: String,
/// Referenced columns.
pub referenced_columns: Vec<String>,
/// Action on deletion.
pub on_delete_action: ForeignKeyAction,
}

Expand Down
8 changes: 7 additions & 1 deletion server/prisma-rs/libs/database-introspection/src/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! MySQL introspection.
use super::*;
use log::debug;
use std::collections::HashMap;
use std::sync::Arc;

/// IntrospectionConnector implementation.
pub struct IntrospectionConnector {
conn: Arc<dyn IntrospectionConnection>,
}
Expand All @@ -28,6 +30,7 @@ impl super::IntrospectionConnector for IntrospectionConnector {
}

impl IntrospectionConnector {
/// Constructor.
pub fn new(conn: Arc<dyn IntrospectionConnection>) -> IntrospectionConnector {
IntrospectionConnector { conn }
}
Expand Down Expand Up @@ -273,7 +276,10 @@ impl IntrospectionConnector {
Some(Index {
name: index_name,
columns: vec![column_name],
unique: is_unique,
tpe: match is_unique {
true => IndexType::Unique,
false => IndexType::Normal,
},
})
}
})
Expand Down
9 changes: 8 additions & 1 deletion server/prisma-rs/libs/database-introspection/src/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Postgres introspection.
use super::*;
use log::debug;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// IntrospectionConnector implementation.
pub struct IntrospectionConnector {
conn: Arc<dyn IntrospectionConnection>,
}
Expand Down Expand Up @@ -30,6 +32,7 @@ impl super::IntrospectionConnector for IntrospectionConnector {
}

impl IntrospectionConnector {
/// Constructor.
pub fn new(conn: Arc<dyn IntrospectionConnection>) -> IntrospectionConnector {
IntrospectionConnector { conn }
}
Expand Down Expand Up @@ -304,10 +307,14 @@ impl IntrospectionConnector {
pk = Some(PrimaryKey { columns });
None
} else {
let is_unique = index.get("is_unique").and_then(|x| x.as_bool()).expect("is_unique");
Some(Index {
name: index.get("name").and_then(|x| x.to_string()).expect("name"),
columns,
unique: index.get("is_unique").and_then(|x| x.as_bool()).expect("is_unique"),
tpe: match is_unique {
true => IndexType::Unique,
false => IndexType::Normal,
},
})
}
})
Expand Down
29 changes: 8 additions & 21 deletions server/prisma-rs/libs/database-introspection/src/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! SQLite introspection.
use super::*;
use crate::IntrospectionConnection;
use log::debug;
Expand All @@ -6,8 +7,9 @@ use regex::Regex;
use std::collections::HashMap;
use std::sync::Arc;

/// IntrospectionConnector implementation.
pub struct IntrospectionConnector {
pub conn: Arc<dyn IntrospectionConnection>,
conn: Arc<dyn IntrospectionConnection>,
}

impl super::IntrospectionConnector for IntrospectionConnector {
Expand All @@ -33,6 +35,7 @@ impl super::IntrospectionConnector for IntrospectionConnector {
}

impl IntrospectionConnector {
/// Constructor.
pub fn new(conn: Arc<dyn IntrospectionConnection>) -> IntrospectionConnector {
IntrospectionConnector { conn }
}
Expand Down Expand Up @@ -258,7 +261,10 @@ impl IntrospectionConnector {
.map(|index_repr| {
let mut index = Index {
name: index_repr.name.clone(),
unique: index_repr.is_unique,
tpe: match index_repr.is_unique {
true => IndexType::Unique,
false => IndexType::Normal,
},
columns: vec![],
};

Expand All @@ -281,25 +287,6 @@ impl IntrospectionConnector {
}
}

#[derive(Debug)]
pub struct IntrospectedForeignKey {
pub name: String,
pub table: String,
pub column: String,
pub referenced_table: String,
pub referenced_column: String,
}

#[derive(Debug, Clone)]
pub struct IntrospectedColumn {
pub name: String,
pub table: String,
pub tpe: String,
pub default: Option<String>,
pub is_required: bool,
pub pk: i64,
}

fn get_column_type(tpe: &str) -> ColumnType {
let tpe_lower = tpe.to_lowercase();
let family = match tpe_lower.as_ref() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1408,18 +1408,16 @@ fn names_with_hyphens_must_work() {
|db_type, inspector| {
let result = inspector.introspect(SCHEMA).expect("introspecting");
let user_table = result.get_table("User-table").expect("getting User table");
let expected_columns = vec![
Column {
name: "column-1".to_string(),
tpe: ColumnType {
raw: int_type(db_type),
family: ColumnTypeFamily::Int,
},
arity: ColumnArity::Required,
default: None,
auto_increment: false,
let expected_columns = vec![Column {
name: "column-1".to_string(),
tpe: ColumnType {
raw: int_type(db_type),
family: ColumnTypeFamily::Int,
},
];
arity: ColumnArity::Required,
default: None,
auto_increment: false,
}];
assert_eq!(user_table.columns, expected_columns);
},
);
Expand Down Expand Up @@ -1888,7 +1886,7 @@ fn indices_must_work() {
indices: vec![Index {
name: "count".to_string(),
columns: vec!["count".to_string()],
unique: false,
tpe: IndexType::Normal,
},],
primary_key: Some(PrimaryKey {
columns: vec!["id".to_string()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"columns": [
"column2"
],
"unique": false
"tpe": "normal"
}
],
"primaryKey": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn database_schema_is_serializable() {
indices: vec![Index {
name: "column2".to_string(),
columns: vec!["column2".to_string()],
unique: false,
tpe: IndexType::Normal,
}],
primary_key: Some(PrimaryKey {
columns: vec!["column1".to_string()],
Expand Down

0 comments on commit 64b9504

Please sign in to comment.