@@ -1976,42 +1976,51 @@ func TestOracleTypeCreateDrop(t *testing.T) {
19761976 t .Skip ("Skipping Oracle type test: not running on Oracle" )
19771977 }
19781978
1979- const typeName = "email_list"
1980- const tableName = "email_varray_tab"
1979+ const (
1980+ typeName = "email_list"
1981+ tableName = "email_varray_tab"
1982+
1983+ objectTypeName = "person_obj"
1984+ objectTableName = "person_obj_tab"
1985+
1986+ incompleteTypeName = "department_t"
1987+ unsupportedTypeName = "unsupported_type_t"
1988+ )
19811989
19821990 // Assert that DB.Migrator() is an oracle.Migrator (so we can use Oracle-specific methods)
19831991 m , ok := DB .Migrator ().(oracle.Migrator )
19841992 if ! ok {
19851993 t .Skip ("Skipping: current dialect migrator is not Oracle-specific" )
19861994 }
19871995
1988- // Drop type if it exists
1989- t .Run ("drop_existing_type_if_any" , func (t * testing.T ) {
1990- err := m .DropType (typeName )
1991- if err != nil && ! strings .Contains (err .Error (), "ORA-04043" ) {
1992- t .Fatalf ("Unexpected error dropping type: %v" , err )
1996+ // Drop types if they exist
1997+ t .Run ("drop_existing_types_if_any" , func (t * testing.T ) {
1998+ if err := m .DropType (typeName ); err != nil && ! strings .Contains (err .Error (), "ORA-04043" ) {
1999+ t .Fatalf ("Unexpected error dropping type %s: %v" , typeName , err )
2000+ }
2001+ if err := m .DropType (objectTypeName ); err != nil && ! strings .Contains (err .Error (), "ORA-04043" ) {
2002+ t .Fatalf ("Unexpected error dropping type %s: %v" , objectTypeName , err )
2003+ }
2004+ if err := m .DropType (incompleteTypeName ); err != nil && ! strings .Contains (err .Error (), "ORA-04043" ) {
2005+ t .Fatalf ("Unexpected error dropping type %s: %v" , incompleteTypeName , err )
19932006 }
19942007 })
19952008
19962009 // Create new VARRAY type
19972010 t .Run ("create_varray_type" , func (t * testing.T ) {
19982011 err := m .CreateType (typeName , "VARRAY(10)" , "VARCHAR2(60)" )
19992012 if err != nil {
2000- t .Fatalf ("Failed to create Oracle type: %v" , err )
2013+ t .Fatalf ("Failed to create Oracle VARRAY type: %v" , err )
20012014 }
20022015
2003- // Verify it exists
2004- var count int
2005- if err := DB .Raw (`SELECT COUNT(*) FROM USER_TYPES WHERE TYPE_NAME = LOWER(?)` , typeName ).Scan (& count ).Error ; err != nil {
2006- t .Fatalf ("Failed to verify created type: %v" , err )
2007- }
2008- if count == 0 {
2009- t .Fatalf ("Expected Oracle type %s to exist" , typeName )
2016+ // Verify it exists via HasType
2017+ if ! m .HasType (typeName ) {
2018+ t .Fatalf ("Expected Oracle VARRAY type %s to exist" , typeName )
20102019 }
20112020 })
20122021
2013- // Create table using the custom type
2014- t .Run ("create_table_using_custom_type " , func (t * testing.T ) {
2022+ // Create table using the VARRAY type
2023+ t .Run ("create_table_using_varray_type " , func (t * testing.T ) {
20152024 createTableSQL := fmt .Sprintf (`
20162025 CREATE TABLE "%s" (
20172026 "ID" NUMBER PRIMARY KEY,
@@ -2028,24 +2037,105 @@ func TestOracleTypeCreateDrop(t *testing.T) {
20282037 }
20292038 })
20302039
2031- // Drop table and type
2032- t .Run ("drop_table_and_type" , func (t * testing.T ) {
2040+ // Create ADT (OBJECT) type
2041+ t .Run ("create_object_type" , func (t * testing.T ) {
2042+ err := m .CreateType (objectTypeName , "OBJECT" , `
2043+ first_name VARCHAR2(50),
2044+ last_name VARCHAR2(50),
2045+ age NUMBER
2046+ ` )
2047+ if err != nil {
2048+ t .Fatalf ("Failed to create Oracle OBJECT type: %v" , err )
2049+ }
2050+
2051+ // Verify it exists via HasType
2052+ if ! m .HasType (objectTypeName ) {
2053+ t .Fatalf ("Expected Oracle OBJECT type %s to exist" , objectTypeName )
2054+ }
2055+ })
2056+
2057+ // Create table using the OBJECT type
2058+ t .Run ("create_table_using_object_type" , func (t * testing.T ) {
2059+ createTableSQL := fmt .Sprintf (`
2060+ CREATE TABLE "%s" (
2061+ "ID" NUMBER PRIMARY KEY,
2062+ "PERSON" "%s"
2063+ )` , objectTableName , objectTypeName )
2064+
2065+ if err := DB .Exec (createTableSQL ).Error ; err != nil {
2066+ t .Fatalf ("Failed to create table using object type %s: %v" , objectTypeName , err )
2067+ }
2068+
2069+ // Verify table exists
2070+ if ! m .HasTable (objectTableName ) {
2071+ t .Fatalf ("Expected table %s to exist" , objectTableName )
2072+ }
2073+ })
2074+
2075+ // Create incomplete type (forward declaration)
2076+ t .Run ("create_incomplete_type" , func (t * testing.T ) {
2077+ if err := m .CreateType (incompleteTypeName ); err != nil {
2078+ t .Fatalf ("Failed to create incomplete type %s: %v" , incompleteTypeName , err )
2079+ }
2080+ if ! m .HasType (incompleteTypeName ) {
2081+ t .Fatalf ("Expected incomplete type %s to exist" , incompleteTypeName )
2082+ }
2083+ if err := m .DropType (incompleteTypeName ); err != nil {
2084+ t .Fatalf ("Failed to drop incomplete type %s: %v" , incompleteTypeName , err )
2085+ }
2086+ if m .HasType (incompleteTypeName ) {
2087+ t .Fatalf ("Expected incomplete type %s to be dropped" , incompleteTypeName )
2088+ }
2089+ })
2090+
2091+ // Unsupported type kinds should return an error and not create anything
2092+ t .Run ("create_unsupported_type" , func (t * testing.T ) {
2093+ err := m .CreateType (unsupportedTypeName , "Unsupported" , "Unsupported" )
2094+ if err == nil {
2095+ t .Fatalf ("Expected error when creating unsupported type %s, got nil" , unsupportedTypeName )
2096+ }
2097+
2098+ // Ensure the type was NOT created
2099+ if m .HasType (unsupportedTypeName ) {
2100+ t .Fatalf ("Type %s should not exist after failed creation" , unsupportedTypeName )
2101+ }
2102+
2103+ // Also ensure DropType is safe to call (idempotent)
2104+ if err := m .DropType (unsupportedTypeName ); err != nil {
2105+ if ! strings .Contains (strings .ToLower (err .Error ()), "does not exist" ) {
2106+ t .Fatalf ("Unexpected error dropping type %s: %v" , unsupportedTypeName , err )
2107+ }
2108+ }
2109+
2110+ if m .HasType (unsupportedTypeName ) {
2111+ t .Fatalf ("Expected type %s to be absent after drop" , unsupportedTypeName )
2112+ }
2113+ })
2114+
2115+ // Drop tables and types
2116+ t .Run ("drop_tables_and_types" , func (t * testing.T ) {
2117+ if err := m .DropTable (objectTableName ); err != nil {
2118+ t .Fatalf ("Failed to drop table %s: %v" , objectTableName , err )
2119+ }
20332120 if err := m .DropTable (tableName ); err != nil {
20342121 t .Fatalf ("Failed to drop table %s: %v" , tableName , err )
20352122 }
20362123
2124+ // Drop types
2125+ if err := m .DropType (objectTypeName ); err != nil {
2126+ t .Fatalf ("Failed to drop type %s: %v" , objectTypeName , err )
2127+ }
20372128 if err := m .DropType (typeName ); err != nil {
20382129 t .Fatalf ("Failed to drop type %s: %v" , typeName , err )
20392130 }
20402131
2041- // Verify type is gone
2042- var count int
2043- if err := DB .Raw (`SELECT COUNT(*) FROM USER_TYPES WHERE TYPE_NAME = LOWER(?)` , typeName ).Scan (& count ).Error ; err != nil {
2044- t .Fatalf ("Failed to verify dropped type: %v" , err )
2045- }
2046- if count > 0 {
2132+ // Verify types are gone via HasType
2133+ if m .HasType (typeName ) {
20472134 t .Fatalf ("Expected Oracle type %s to be dropped" , typeName )
20482135 }
2136+ if m .HasType (objectTypeName ) {
2137+ t .Fatalf ("Expected Oracle type %s to be dropped" , objectTypeName )
2138+ }
20492139 })
20502140}
20512141
0 commit comments