Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: Update sqlc to v1.16.0 #5788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 23, 2023
Merged

chore: Update sqlc to v1.16.0 #5788

merged 11 commits into from
Jan 23, 2023

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Jan 19, 2023

I wanted to use enum validation in #5749 and saw sqlc had added it, so I updated.

On the road, I noticed we've had nullable values that we didn't really treat that way. So I did a migration to change them:

UPDATE parameter_schemas SET default_source_scheme = 'none' WHERE default_source_scheme IS NULL;
ALTER TABLE parameter_schemas ALTER COLUMN default_source_scheme SET NOT NULL;

UPDATE parameter_schemas SET default_destination_scheme = 'none' WHERE default_destination_scheme IS NULL;
ALTER TABLE parameter_schemas ALTER COLUMN default_destination_scheme SET NOT NULL;

I considered adding DEFAULT 'none', but I thought that would be more likely to hide issues. Does this seem like the right solution to that?

Also note the UserStatus change, we were not treating it as nullable before, although the database schema allowed it.

  • chore: Update sqlc to v1.16.0
  • chore: Make gen
  • chore: Fix null issues

@mafredri mafredri self-assigned this Jan 19, 2023
Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks ok. I think the userstatus being null is a mistake? But it is my first time looking at that audit log sql and don't have a db atm to try things against.

Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured out my comments 👍

@mafredri mafredri force-pushed the mafredri/chore-update-sqlc branch from ae9aa46 to cc002a1 Compare January 20, 2023 09:15
@mafredri
Copy link
Member Author

I noticed invalid inserts in telemetry test (i.e. things that would never work in real postgres), so I went ahead and implemented validation in databasefake.

See d8c1d68 (#5788):

We'll now have some form of validation on inserts in the fake as well. Unfortunately it requires some manual plumbing when creating fake methods:

    telemetry_test.go:72:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:72
        	Error:      	Received unexpected error:
        	            	invalid database type fields:
        	            	    	InsertProvisionerJobParams.Provisioner: invalid ProvisionerType: ""
        	            	    	InsertProvisionerJobParams.StorageMethod: invalid ProvisionerStorageMethod: ""
        	            	    	InsertProvisionerJobParams.Type: invalid ProvisionerJobType: "":
        	            	    github.com/coder/coder/coderd/database/databasefake.validateDatabaseType
        	            	        /home/coder/src/coder/coder/coderd/database/databasefake/databasefake.go:183
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:78:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:78
        	Error:      	Received unexpected error:
        	            	invalid database type fields:
        	            	    	InsertTemplateParams.Provisioner: invalid ProvisionerType: "":
        	            	    github.com/coder/coder/coderd/database/databasefake.validateDatabaseType
        	            	        /home/coder/src/coder/coder/coderd/database/databasefake/databasefake.go:183
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:89:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:89
        	Error:      	Received unexpected error:
        	            	invalid database type fields:
        	            	    	InsertUserParams.LoginType: invalid LoginType: "":
        	            	    github.com/coder/coder/coderd/database/databasefake.validateDatabaseType
        	            	        /home/coder/src/coder/coder/coderd/database/databasefake/databasefake.go:183
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:101:
[...]

This doesn't fix all issues, though, fixing the issues reported by 8d143c0 (#5788) still leaves the following issues when switching to real database:

=== CONT  TestTelemetry/Snapshot
    telemetry_test.go:58:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:58
        	Error:      	Received unexpected error:
        	            	pq: null value in column "ip_address" of relation "api_keys" violates not-null constraint
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:66:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:66
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "parameter_schemas" violates foreign key constraint "parameter_schemas_job_id_fkey"
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:74:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:74
        	Error:      	Received unexpected error:
        	            	pq: invalid input syntax for type json
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:80:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:80
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "templates" violates foreign key constraint "templates_organization_id_fkey"
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:85:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:85
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "template_versions" violates foreign key constraint "template_versions_organization_id_fkey"
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:91:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:91
        	Error:      	Received unexpected error:
        	            	pq: null value in column "rbac_roles" of relation "users" violates not-null constraint
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:96:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:96
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "workspaces" violates foreign key constraint "workspaces_owner_id_fkey"
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:103:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:103
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "workspace_apps" violates foreign key constraint "workspace_apps_agent_id_fkey"
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:108:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:108
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "workspace_agents" violates foreign key constraint "workspace_agents_resource_id_fkey"
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:115:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:115
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "workspace_builds" violates foreign key constraint "workspace_builds_workspace_id_fkey"
        	Test:       	TestTelemetry/Snapshot
    telemetry_test.go:121:
        	Error Trace:	/home/coder/src/coder/coder/coderd/telemetry/telemetry_test.go:121
        	Error:      	Received unexpected error:
        	            	pq: insert or update on table "workspace_resources" violates foreign key constraint "workspace_resources_job_id_fkey"
        	Test:       	TestTelemetry/Snapshot

Comment on lines +505 to +509
// Ignore log errors because we get:
//
// (*Server).FailJob audit log - get build {"error": "sql: no rows in result set"}
ignoreLogErrors := true
srv := setup(t, ignoreLogErrors)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I invite extra scrutiny to this test here! The audit log implementation may not have ideal behavior. Please ensure that we actually shouldn't audit log when a workspace build wasn't created.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines 163 to 171
// Note: database.Null* types don't have a Valid method, we skip them here
// because their embedded types may have a Valid method and we don't want
// to bother with checking both that tha Valid field is true and that the
// type it embeds validates to true. We would need to check:
//
// dbNullEnum.Valid && dbNullEnum.Enum.Valid()
if strings.HasPrefix(v.Type().Name(), "Null") {
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above comment on the function says we do not go into nested structs. Isn't the Nullable case a nested struct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is to do nothing in the following scenario:

validateDatabaseType(database.NullThing{}) // HasPrefix Null

If we have:

validateDatabaseType(database.MyType{Thing: database.NullThing{}})

We ultimately will not validate Thing since we only go one level deep and the validator function doesn't check structs.

This was just to keep the logic (simpler) and not having to look at both database.NullThing.Valid == true && database.NullThing.Thing.Valid().

@mafredri mafredri merged commit 8afdf24 into main Jan 23, 2023
@mafredri mafredri deleted the mafredri/chore-update-sqlc branch January 23, 2023 11:14
@github-actions github-actions bot locked and limited conversation to collaborators Jan 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants