8000 feat(site): display user status history as an indication of license usage by SasSwart · Pull Request #16020 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(site): display user status history as an indication of license usage #16020

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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Split
Next Next commit
add user_status_changes table
  • Loading branch information
SasSwart committed Jan 3, 2025
commit 4cd66af5fbff7c29bdbefae36026305d785cbe8e
38 changes: 38 additions & 0 deletions coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/database/foreign_key_constraint.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions coderd/database/migrations/000279_user_status_changes.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Drop the trigger first
DROP TRIGGER IF EXISTS user_status_change_trigger ON users;

-- Drop the trigger function
DROP FUNCTION IF EXISTS record_user_status_change();

-- Drop the indexes
DROP INDEX IF EXISTS idx_user_status_changes_changed_at;
DROP INDEX IF EXISTS idx_user_status_changes_user_id;

-- Drop the table
DROP TABLE IF EXISTS user_status_changes;
43 changes: 43 additions & 0 deletions coderd/database/migrations/000279_user_status_changes.up.sql
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE user_status_changes (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES users(id),
new_status user_status NOT NULL,
changed_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE user_status_changes IS 'Tracks the history of user status changes';

CREATE INDEX idx_user_status_changes_user_id ON user_status_changes(user_id);
CREATE INDEX idx_user_status_changes_changed_at ON user_status_changes(changed_at);

INSERT INTO user_status_changes (
user_id,
new_status,
changed_at
)
SELECT
id,
status,
created_at
FROM users
WHERE NOT deleted;

CREATE FUNCTION record_user_status_change() RETURNS trigger AS $$
BEGIN
IF OLD.status IS DISTINCT FROM NEW.status THEN
INSERT INTO user_status_changes (
user_id,
new_status
) VALUES (
NEW.id,
NEW.status
);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER user_status_change_trigger
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION record_user_status_change();
8 changes: 8 additions & 0 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/database/unique_constraint.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0