Skip to content

Commit

Permalink
SQL Server: Reset should clear UDTs (#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Aug 2, 2021
1 parent 10fbb58 commit aca64ce
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
Expand Up @@ -37,6 +37,7 @@ impl TestApi {
let me = SqlMigrationConnector::new(connection_string, preview_features, None)
.await
.unwrap();

me.reset().await.unwrap();

(
Expand Down
Expand Up @@ -296,11 +296,30 @@ impl SqlFlavour for MssqlFlavour {
schema_name
);

let drop_types = format!(
r#"
DECLARE @stmt NVARCHAR(max)
DECLARE @n CHAR(1)
SET @n = CHAR(10)
SELECT @stmt = ISNULL(@stmt + @n, '') +
'DROP TYPE [' + SCHEMA_NAME(schema_id) + '].[' + name + ']'
FROM sys.types
WHERE SCHEMA_NAME(schema_id) = '{0}'
AND is_user_defined = 1
EXEC SP_EXECUTESQL @stmt
"#,
schema_name
);

connection.raw_cmd(&drop_procedures).await?;
connection.raw_cmd(&drop_views).await?;
connection.raw_cmd(&drop_shared_defaults).await?;
connection.raw_cmd(&drop_fks).await?;
connection.raw_cmd(&drop_tables).await?;
connection.raw_cmd(&drop_types).await?;

Ok(())
}
Expand Down
Expand Up @@ -393,7 +393,7 @@ impl SqlRenderer for MssqlFlavour {
}

fn render_drop_user_defined_type(&self, udt: &UserDefinedTypeWalker<'_>) -> String {
todo!("DROP TYPE {}", self.quote_with_schema(udt.name()))
format!("DROP TYPE {}", self.quote_with_schema(udt.name()))
}

fn render_begin_transaction(&self) -> Option<&'static str> {
Expand Down
13 changes: 12 additions & 1 deletion migration-engine/migration-engine-tests/src/sync_test_api.rs
Expand Up @@ -5,7 +5,10 @@ pub use test_setup::{BitFlags, Capabilities, Tags};

use crate::{commands::*, multi_engine_test_api::TestApi as RootTestApi};
use migration_connector::MigrationPersistence;
use quaint::prelude::{ConnectionInfo, ResultSet};
use quaint::{
prelude::{ConnectionInfo, ResultSet},
Value,
};
use sql_migration_connector::SqlMigrationConnector;
use std::{borrow::Cow, future::Future};
use tempfile::TempDir;
Expand Down Expand Up @@ -196,6 +199,14 @@ impl TestApi {
self.root.block_on(self.connector.queryable().query(q)).unwrap()
}

/// Like quaint::Queryable::query_raw()
#[track_caller]
pub fn query_raw(&self, q: &str, params: &[Value<'static>]) -> ResultSet {
self.root
.block_on(self.connector.queryable().query_raw(q, params))
.unwrap()
}

/// Send a SQL command to the database, and expect it to succeed.
#[track_caller]
pub fn raw_cmd(&self, sql: &str) {
Expand Down
27 changes: 27 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/mssql.rs
@@ -1,5 +1,32 @@
use migration_engine_tests::sync_test_api::*;

#[test_connector(tags(Mssql))]
fn reset_clears_udts(api: TestApi) {
let schema = api.schema_name();

api.raw_cmd(&format!("CREATE TYPE {}.[testType] AS TABLE (FooBar INT)", schema));

let schemas = api.query_raw(
&format!(
"SELECT * FROM sys.types WHERE SCHEMA_NAME(schema_id) = '{}' and NAME = 'testType'",
schema
),
&[],
);
assert_eq!(1, schemas.len());

api.reset().send_sync();

let schemas = api.query_raw(
&format!(
"SELECT * FROM sys.types WHERE SCHEMA_NAME(schema_id) = '{}' and NAME = 'testType'",
schema
),
&[],
);
assert_eq!(0, schemas.len());
}

#[test_connector(tags(Mssql))]
fn shared_default_constraints_are_ignored_issue_5423(api: TestApi) {
let schema = api.schema_name();
Expand Down

0 comments on commit aca64ce

Please sign in to comment.