Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions internal/database/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var roleColumns = []*sqlf.Query{
sqlf.Sprintf("roles.name"),
sqlf.Sprintf("roles.system"),
sqlf.Sprintf("roles.created_at"),
sqlf.Sprintf("roles.deleted_at"),
}

var roleInsertColumns = []*sqlf.Query{
Expand Down Expand Up @@ -120,7 +119,9 @@ func (r *roleStore) Get(ctx context.Context, opts GetRoleOpts) (*types.Role, err
conds = append(conds, sqlf.Sprintf("name = %s", opts.Name))
}

conds = append(conds, sqlf.Sprintf("deleted_at IS NULL"))
if len(conds) == 0 {
conds = append(conds, sqlf.Sprintf("TRUE"))
}

q := sqlf.Sprintf(
getRoleFmtStr,
Expand All @@ -146,7 +147,6 @@ func scanRole(sc dbutil.Scanner) (*types.Role, error) {
&role.Name,
&role.System,
&role.CreatedAt,
&dbutil.NullTime{Time: &role.DeletedAt},
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (r *roleStore) list(ctx context.Context, opts RolesListOptions, selects *sq
}

func (r *roleStore) computeConditionsAndJoins(opts RolesListOptions) ([]*sqlf.Query, *sqlf.Query) {
var conds = []*sqlf.Query{sqlf.Sprintf("deleted_at IS NULL")}
var conds []*sqlf.Query
var joins = sqlf.Sprintf("")

if opts.System {
Expand All @@ -221,6 +221,10 @@ func (r *roleStore) computeConditionsAndJoins(opts RolesListOptions) ([]*sqlf.Qu
joins = sqlf.Sprintf("INNER JOIN user_roles ON user_roles.role_id = roles.id")
}

if len(conds) == 0 {
conds = append(conds, sqlf.Sprintf("TRUE"))
}

return conds, joins
}

Expand Down Expand Up @@ -280,7 +284,7 @@ UPDATE roles
SET
name = %s
WHERE
id = %s
id = %s AND NOT system
RETURNING
%s
`
Expand All @@ -299,10 +303,8 @@ func (r *roleStore) Update(ctx context.Context, role *types.Role) (*types.Role,
}

const roleDeleteQueryFmtStr = `
UPDATE roles
SET
deleted_at = NOW()
WHERE id = %s AND NOT system AND deleted_at IS NULL
DELETE FROM roles
WHERE id = %s AND NOT system
`

func (r *roleStore) Delete(ctx context.Context, opts DeleteRoleOpts) error {
Expand Down
13 changes: 0 additions & 13 deletions internal/database/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -19461,19 +19461,6 @@
"GenerationExpression": "",
"Comment": ""
},
{
"Name": "deleted_at",
"Index": 4,
"TypeName": "timestamp with time zone",
"IsNullable": true,
"Default": "",
"CharacterMaximumLength": 0,
"IsIdentity": false,
"IdentityGeneration": "",
"IsGenerated": "NEVER",
"GenerationExpression": "",
"Comment": ""
},
{
"Name": "id",
"Index": 1,
Expand Down
1 change: 0 additions & 1 deletion internal/database/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -3023,7 +3023,6 @@ Foreign-key constraints:
id | integer | | not null | nextval('roles_id_seq'::regclass)
name | text | | not null |
created_at | timestamp with time zone | | not null | now()
deleted_at | timestamp with time zone | | |
system | boolean | | not null | false
Indexes:
"roles_pkey" PRIMARY KEY, btree (id)
Expand Down
1 change: 0 additions & 1 deletion internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,6 @@ type Role struct {
Name string
System bool
CreatedAt time.Time
DeletedAt time.Time
}

type Permission struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE roles
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMP WITH TIME ZONE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: remove_roles_deleted_at
parents: [1674669326, 1674814035, 1675257827, 1675367314]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE roles DROP COLUMN IF EXISTS deleted_at;
5 changes: 2 additions & 3 deletions migrations/frontend/squashed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3614,7 +3614,6 @@ CREATE TABLE roles (
id integer NOT NULL,
name text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
deleted_at timestamp with time zone,
system boolean DEFAULT false NOT NULL,
CONSTRAINT name_not_blank CHECK ((name <> ''::text))
);
Expand Down Expand Up @@ -5626,7 +5625,7 @@ INSERT INTO lsif_configuration_policies VALUES (3, NULL, 'Default commit retenti

SELECT pg_catalog.setval('lsif_configuration_policies_id_seq', 3, true);

INSERT INTO roles VALUES (1, 'USER', '2023-01-04 16:29:41.195966+00', NULL, true);
INSERT INTO roles VALUES (2, 'SITE_ADMINISTRATOR', '2023-01-04 16:29:41.195966+00', NULL, true);
INSERT INTO roles VALUES (1, 'USER', '2023-01-04 16:29:41.195966+00', true);
INSERT INTO roles VALUES (2, 'SITE_ADMINISTRATOR', '2023-01-04 16:29:41.195966+00', true);

SELECT pg_catalog.setval('roles_id_seq', 3, true);