Skip to content

Commit

Permalink
feat(db): migrate repositories from old tables to provided_repositori…
Browse files Browse the repository at this point in the history
…es (#2188)

* feat(db): migrate repositories from old tables to provided_repositories

* Write unit test for migration

* [autofix.ci] apply automated fixes

* Remove old tables

* [autofix.ci] apply automated fixes

* Fix tests

* Move test

* Change migration code

* Update ee/tabby-db/migrations/0029_merged-provider-tables.down.sql

Co-authored-by: Meng Zhang <[email protected]>

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Meng Zhang <[email protected]>
  • Loading branch information
3 people authored May 20, 2024
1 parent e2e2c44 commit 8e0d82f
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 544 deletions.
15 changes: 15 additions & 0 deletions ee/tabby-db/migrations/0029_merged-provider-tables.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@ INSERT INTO integrations(kind, display_name, access_token)

INSERT INTO integrations(kind, display_name, access_token)
SELECT 'gitlab', display_name, access_token FROM gitlab_repository_provider WHERE access_token IS NOT NULL;

INSERT INTO provided_repositories(integration_id, vendor_id, name, git_url, active)
SELECT integrations.id, vendor_id, github_provided_repositories.name, git_url, active FROM github_provided_repositories
JOIN github_repository_provider ON github_repository_provider_id = github_repository_provider.id
JOIN integrations ON kind = 'github' AND github_repository_provider.access_token = integrations.access_token;

INSERT INTO provided_repositories(integration_id, vendor_id, name, git_url, active)
SELECT integrations.id, vendor_id, gitlab_provided_repositories.name, git_url, active FROM gitlab_provided_repositories
JOIN gitlab_repository_provider ON gitlab_repository_provider_id = gitlab_repository_provider.id
JOIN integrations ON kind = 'gitlab' AND gitlab_repository_provider.access_token = integrations.access_token;

DROP TABLE github_provided_repositories;
DROP TABLE gitlab_provided_repositories;
DROP TABLE github_repository_provider;
DROP TABLE gitlab_repository_provider;
Binary file modified ee/tabby-db/schema.sqlite
Binary file not shown.
42 changes: 0 additions & 42 deletions ee/tabby-db/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,6 @@ CREATE INDEX idx_user_completion_user_id_created_at_language ON user_completions
created_at,
language
);
CREATE TABLE github_provided_repositories(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
github_repository_provider_id INTEGER NOT NULL,
-- vendor_id from https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-a-user
vendor_id TEXT NOT NULL,
name TEXT NOT NULL,
git_url TEXT NOT NULL,
active BOOLEAN NOT NULL DEFAULT FALSE,
updated_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
FOREIGN KEY(github_repository_provider_id) REFERENCES github_repository_provider(id) ON DELETE CASCADE,
CONSTRAINT `idx_vendor_id_provider_id` UNIQUE(vendor_id, github_repository_provider_id)
);
CREATE INDEX github_provided_repositories_updated_at ON github_provided_repositories(
updated_at
);
CREATE TABLE user_events(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
Expand All @@ -145,33 +130,6 @@ CREATE TABLE password_reset(
created_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
FOREIGN KEY(user_id) REFERENCES users(id)
);
CREATE TABLE github_repository_provider(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
display_name TEXT NOT NULL,
access_token TEXT,
synced_at TIMESTAMP
);
CREATE TABLE gitlab_repository_provider(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
display_name TEXT NOT NULL,
access_token TEXT,
synced_at TIMESTAMP
);
CREATE TABLE gitlab_provided_repositories(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
gitlab_repository_provider_id INTEGER NOT NULL,
-- vendor_id from https://docs.gitlab.com/ee/api/repositories.html
vendor_id TEXT NOT NULL,
name TEXT NOT NULL,
git_url TEXT NOT NULL,
active BOOLEAN NOT NULL DEFAULT FALSE,
updated_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
FOREIGN KEY(gitlab_repository_provider_id) REFERENCES gitlab_repository_provider(id) ON DELETE CASCADE,
CONSTRAINT `idx_vendor_id_provider_id` UNIQUE(vendor_id, gitlab_repository_provider_id)
);
CREATE INDEX gitlab_provided_repositories_updated_at ON gitlab_provided_repositories(
updated_at
);
CREATE TABLE integrations(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
kind TEXT NOT NULL,
Expand Down
Loading

0 comments on commit 8e0d82f

Please sign in to comment.