-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bufixes and improvements: - pgdialect.Inspector canonicalizes all default expressions (lowercase) to make sure they are always comparable with the model definition. - sqlschema.SchemaInspector canonicalizes all default expressions (lowercase) - pgdialect and sqlschema now support type-equivalence, which prevents unnecessary migrations like CHAR -> CHARACTER from being created. Changing PRIMARY KEY and UNIQUE-ness are outside of this commit's scope, because those constraints can span multiple columns.
- Loading branch information
Showing
11 changed files
with
747 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package pgdialect | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/uptrace/bun/dialect/sqltype" | ||
"github.com/uptrace/bun/migrate/sqlschema" | ||
) | ||
|
||
func TestInspectorDialect_EquivalentType(t *testing.T) { | ||
d := New() | ||
|
||
t.Run("common types", func(t *testing.T) { | ||
for _, tt := range []struct { | ||
typ1, typ2 string | ||
want bool | ||
}{ | ||
{"text", "text", true}, // identical types | ||
|
||
{sqltype.VarChar, pgTypeVarchar, true}, | ||
{sqltype.VarChar, pgTypeCharacterVarying, true}, | ||
{sqltype.VarChar, pgTypeChar, false}, | ||
{sqltype.VarChar, pgTypeCharacter, false}, | ||
{pgTypeCharacterVarying, pgTypeVarchar, true}, | ||
{pgTypeCharacter, pgTypeChar, true}, | ||
{sqltype.VarChar, pgTypeText, false}, | ||
{pgTypeChar, pgTypeText, false}, | ||
{pgTypeVarchar, pgTypeText, false}, | ||
|
||
// SQL standards require that TIMESTAMP be default alias for "TIMESTAMP WITH TIME ZONE" | ||
{sqltype.Timestamp, pgTypeTimestampTz, true}, | ||
{sqltype.Timestamp, pgTypeTimestampWithTz, true}, | ||
{sqltype.Timestamp, pgTypeTimestamp, true}, // Still, TIMESTAMP == TIMESTAMP | ||
{sqltype.Timestamp, pgTypeTimeTz, false}, | ||
{pgTypeTimestampTz, pgTypeTimestampWithTz, true}, | ||
} { | ||
eq := " ~ " | ||
if !tt.want { | ||
eq = " !~ " | ||
} | ||
t.Run(tt.typ1+eq+tt.typ2, func(t *testing.T) { | ||
got := d.EquivalentType( | ||
sqlschema.Column{SQLType: tt.typ1}, | ||
sqlschema.Column{SQLType: tt.typ2}, | ||
) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
|
||
}) | ||
|
||
t.Run("custom varchar length", func(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
col1, col2 sqlschema.Column | ||
want bool | ||
}{ | ||
{ | ||
name: "varchars of different length are not equivalent", | ||
col1: sqlschema.Column{SQLType: "varchar", VarcharLen: 10}, | ||
col2: sqlschema.Column{SQLType: "varchar"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "varchar with no explicit length is equivalent to varchar of default length", | ||
col1: sqlschema.Column{SQLType: "varchar", VarcharLen: d.DefaultVarcharLen()}, | ||
col2: sqlschema.Column{SQLType: "varchar"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "characters with equal custom length", | ||
col1: sqlschema.Column{SQLType: "character varying", VarcharLen: 200}, | ||
col2: sqlschema.Column{SQLType: "varchar", VarcharLen: 200}, | ||
want: true, | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := d.EquivalentType(tt.col1, tt.col2) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
}) | ||
} |
Oops, something went wrong.