Skip to content

Commit

Permalink
Update all usages of Column.Nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-farries committed Nov 27, 2024
1 parent 01b19e9 commit 5aa59b2
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 157 deletions.
2 changes: 1 addition & 1 deletion internal/benchmarks/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ var migCreateTable = migrations.Migration{
{
Name: "name",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
Unique: ptr(false),
},
},
Expand Down
5 changes: 1 addition & 4 deletions pkg/migrations/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ package migrations

// IsNullable returns true if the column is nullable
func (c *Column) IsNullable() bool {
if c.Nullable != nil {
return *c.Nullable
}
return false
return c.Nullable
}

// IsUnique returns true if the column values must be unique
Expand Down
2 changes: 1 addition & 1 deletion pkg/migrations/op_add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func addColumn(ctx context.Context, conn db.DB, o OpAddColumn, t *schema.Table,
// on migration completion
// This is to avoid unnecessary exclusive table locks.
if !o.Column.IsNullable() && o.Column.Default == nil {
o.Column.Nullable = ptr(true)
o.Column.Nullable = true
}

// Don't add a column with a CHECK constraint directly.
Expand Down
44 changes: 22 additions & 22 deletions pkg/migrations/op_add_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestAddColumn(t *testing.T) {
Column: migrations.Column{
Name: "age",
Type: "integer",
Nullable: ptr(false),
Nullable: false,
Default: ptr("0"),
Comment: ptr("the age of the user"),
},
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestAddColumn(t *testing.T) {
Column: migrations.Column{
Name: "description",
Type: "integer",
Nullable: ptr(false),
Nullable: false,
Unique: ptr(true),
},
Up: "'this is a description'",
Expand Down Expand Up @@ -182,22 +182,22 @@ func TestAddColumn(t *testing.T) {
Column: migrations.Column{
Name: "counter_smallserial",
Type: "smallserial",
Nullable: ptr(false),
Nullable: false,
},
},
&migrations.OpAddColumn{
Table: "users",
Column: migrations.Column{
Name: "counter_serial",
Type: "serial",
Nullable: ptr(false),
Nullable: false,
},
}, &migrations.OpAddColumn{
Table: "users",
Column: migrations.Column{
Name: "counter_bigserial",
Type: "bigserial",
Nullable: ptr(false),
Nullable: false,
},
},
},
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestAddForeignKeyColumn(t *testing.T) {
Table: "users",
Column: "id",
},
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestAddForeignKeyColumn(t *testing.T) {
Table: "users",
Column: "id",
},
Nullable: ptr(false),
Nullable: false,
},
Up: "1",
},
Expand Down Expand Up @@ -510,7 +510,7 @@ func TestAddForeignKeyColumn(t *testing.T) {
Column: migrations.Column{
Name: "user_id",
Type: "integer",
Nullable: ptr(true),
Nullable: true,
References: &migrations.ForeignKeyReference{
Name: "fk_users_id",
Table: "users",
Expand Down Expand Up @@ -621,7 +621,7 @@ func TestAddForeignKeyColumn(t *testing.T) {
Column: migrations.Column{
Name: "user_id",
Type: "integer",
Nullable: ptr(true),
Nullable: true,
References: &migrations.ForeignKeyReference{
Name: "fk_users_id",
Table: "users",
Expand Down Expand Up @@ -736,7 +736,7 @@ func TestAddColumnWithUpSql(t *testing.T) {
Column: migrations.Column{
Name: "description",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -818,7 +818,7 @@ func TestAddColumnWithUpSql(t *testing.T) {
Column: migrations.Column{
Name: "description",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -900,7 +900,7 @@ func TestAddColumnWithUpSql(t *testing.T) {
Column: migrations.Column{
Name: "description",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -982,7 +982,7 @@ func TestAddColumnWithUpSql(t *testing.T) {
Column: migrations.Column{
Name: "description",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -1051,7 +1051,7 @@ func TestAddColumnWithUpSql(t *testing.T) {
Name: "name",
Type: "varchar(255)",
Unique: ptr(true),
Nullable: ptr(false),
Nullable: false,
},
},
},
Expand All @@ -1071,7 +1071,7 @@ func TestAddColumnWithUpSql(t *testing.T) {
Column: migrations.Column{
Name: "description",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -1165,7 +1165,7 @@ func TestAddNotNullColumnWithNoDefault(t *testing.T) {
Column: migrations.Column{
Name: "description",
Type: "varchar(255)",
Nullable: ptr(false),
Nullable: false,
},
},
},
Expand Down Expand Up @@ -1238,7 +1238,7 @@ func TestAddColumnValidation(t *testing.T) {
Name: "name",
Type: "varchar(255)",
Unique: ptr(true),
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand All @@ -1259,7 +1259,7 @@ func TestAddColumnValidation(t *testing.T) {
Name: "name",
Type: "varchar(255)",
Unique: ptr(true),
Nullable: ptr(false),
Nullable: false,
},
},
},
Expand All @@ -1279,7 +1279,7 @@ func TestAddColumnValidation(t *testing.T) {
Column: migrations.Column{
Name: "age",
Type: "integer",
Nullable: ptr(false),
Nullable: false,
Default: ptr("0"),
},
},
Expand Down Expand Up @@ -1319,7 +1319,7 @@ func TestAddColumnValidation(t *testing.T) {
Column: migrations.Column{
Name: "age",
Type: "integer",
Nullable: ptr(false),
Nullable: false,
},
},
},
Expand Down Expand Up @@ -1536,7 +1536,7 @@ func TestAddColumnWithComment(t *testing.T) {
Column: migrations.Column{
Name: "age",
Type: "integer",
Nullable: ptr(false),
Nullable: false,
Default: ptr("0"),
Comment: ptr("the age of the user"),
},
Expand Down Expand Up @@ -1687,7 +1687,7 @@ func TestAddColumnToATableCreatedInTheSameMigration(t *testing.T) {
Column: migrations.Column{
Name: "age",
Type: "integer",
Nullable: ptr(false),
Nullable: false,
Default: ptr("18"),
Check: &migrations.CheckConstraint{
Name: "age_check",
Expand Down
12 changes: 6 additions & 6 deletions pkg/migrations/op_alter_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) {
{
Name: "name",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) {
{
Name: "name",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -308,7 +308,7 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) {
{
Name: "name",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -379,7 +379,7 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) {
{
Name: "name",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand All @@ -394,12 +394,12 @@ func TestAlterColumnMultipleSubOperations(t *testing.T) {
{
Name: "name",
Type: "varchar(255)",
Nullable: ptr(true),
Nullable: true,
},
{
Name: "manages",
Type: "integer",
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/migrations/op_change_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func TestChangeColumnType(t *testing.T) {
{
Name: "name",
Type: "text",
Nullable: ptr(false),
Nullable: false,
},
},
},
Expand All @@ -176,7 +176,7 @@ func TestChangeColumnType(t *testing.T) {
{
Name: "name",
Type: "text",
Nullable: ptr(false),
Nullable: false,
},
{
Name: "department_id",
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestChangeColumnType(t *testing.T) {
Name: "age",
Type: "text",
Default: ptr("'0'"),
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestChangeColumnType(t *testing.T) {
Name: "username",
Type: "text",
Default: ptr("'alice'"),
Nullable: ptr(true),
Nullable: true,
},
},
},
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestChangeColumnType(t *testing.T) {
{
Name: "age",
Type: "text",
Nullable: ptr(true),
Nullable: true,
Check: &migrations.CheckConstraint{
Name: "age_length",
Constraint: "length(age) < 3",
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestChangeColumnType(t *testing.T) {
{
Name: "username",
Type: "text",
Nullable: ptr(true),
Nullable: true,
Check: &migrations.CheckConstraint{
Name: "username_length",
Constraint: "length(username) > 3",
Expand Down Expand Up @@ -479,7 +479,7 @@ func TestChangeColumnType(t *testing.T) {
{
Name: "username",
Type: "text",
Nullable: ptr(false),
Nullable: false,
},
},
},
Expand Down
4 changes: 0 additions & 4 deletions pkg/migrations/op_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,3 @@ func OperationName(op Operation) OpName {

panic(fmt.Errorf("unknown operation for %T", op))
}

func ptr[T any](v T) *T {
return &v
}
Loading

0 comments on commit 5aa59b2

Please sign in to comment.