mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
chore: Move httpapi, httpmw, & database into coderd
(#568)
* chore: Move httpmw to /coderd directory httpmw is specific to coderd and should be scoped under coderd * chore: Move httpapi to /coderd directory httpapi is specific to coderd and should be scoped under coderd * chore: Move database to /coderd directory database is specific to coderd and should be scoped under coderd * chore: Update codecov & gitattributes for generated files * chore: Update Makefile
This commit is contained in:
0
coderd/database/migrations/000001_base.down.sql
Normal file
0
coderd/database/migrations/000001_base.down.sql
Normal file
92
coderd/database/migrations/000001_base.up.sql
Normal file
92
coderd/database/migrations/000001_base.up.sql
Normal file
@ -0,0 +1,92 @@
|
||||
-- This migration creates tables and types for v1 if they do not exist.
|
||||
-- This allows v2 to operate independently of v1, but share data if it exists.
|
||||
--
|
||||
-- All tables and types are stolen from:
|
||||
-- https://github.com/coder/m/blob/47b6fc383347b9f9fab424d829c482defd3e1fe2/product/coder/pkg/database/dump.sql
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE login_type AS ENUM (
|
||||
'built-in',
|
||||
'saml',
|
||||
'oidc'
|
||||
);
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE userstatus AS ENUM (
|
||||
'active',
|
||||
'dormant',
|
||||
'decommissioned'
|
||||
);
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id text NOT NULL,
|
||||
email text NOT NULL,
|
||||
name text NOT NULL,
|
||||
revoked boolean NOT NULL,
|
||||
login_type login_type NOT NULL,
|
||||
hashed_password bytea NOT NULL,
|
||||
created_at timestamp with time zone NOT NULL,
|
||||
updated_at timestamp with time zone NOT NULL,
|
||||
temporary_password boolean DEFAULT false NOT NULL,
|
||||
avatar_hash text DEFAULT '' :: text NOT NULL,
|
||||
ssh_key_regenerated_at timestamp with time zone DEFAULT now() NOT NULL,
|
||||
username text DEFAULT '' :: text NOT NULL,
|
||||
dotfiles_git_uri text DEFAULT '' :: text NOT NULL,
|
||||
roles text [] DEFAULT '{site-member}' :: text [] NOT NULL,
|
||||
status userstatus DEFAULT 'active' :: public.userstatus NOT NULL,
|
||||
relatime timestamp with time zone DEFAULT now() NOT NULL,
|
||||
gpg_key_regenerated_at timestamp with time zone DEFAULT now() NOT NULL,
|
||||
_decomissioned boolean DEFAULT false NOT NULL,
|
||||
shell text DEFAULT '' :: text NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS organizations (
|
||||
id text NOT NULL,
|
||||
name text NOT NULL,
|
||||
description text NOT NULL,
|
||||
created_at timestamp with time zone NOT NULL,
|
||||
updated_at timestamp with time zone NOT NULL,
|
||||
"default" boolean DEFAULT false NOT NULL,
|
||||
auto_off_threshold bigint DEFAULT '28800000000000' :: bigint NOT NULL,
|
||||
cpu_provisioning_rate real DEFAULT 4.0 NOT NULL,
|
||||
memory_provisioning_rate real DEFAULT 1.0 NOT NULL,
|
||||
workspace_auto_off boolean DEFAULT false NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS organization_members (
|
||||
organization_id text NOT NULL,
|
||||
user_id text NOT NULL,
|
||||
created_at timestamp with time zone NOT NULL,
|
||||
updated_at timestamp with time zone NOT NULL,
|
||||
roles text [] DEFAULT '{organization-member}' :: text [] NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS api_keys (
|
||||
id text NOT NULL,
|
||||
hashed_secret bytea NOT NULL,
|
||||
user_id text NOT NULL,
|
||||
application boolean NOT NULL,
|
||||
name text NOT NULL,
|
||||
last_used timestamp with time zone NOT NULL,
|
||||
expires_at timestamp with time zone NOT NULL,
|
||||
created_at timestamp with time zone NOT NULL,
|
||||
updated_at timestamp with time zone NOT NULL,
|
||||
login_type login_type NOT NULL,
|
||||
oidc_access_token text DEFAULT ''::text NOT NULL,
|
||||
oidc_refresh_token text DEFAULT ''::text NOT NULL,
|
||||
oidc_id_token text DEFAULT ''::text NOT NULL,
|
||||
oidc_expiry timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL,
|
||||
devurl_token boolean DEFAULT false NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS licenses (
|
||||
id integer NOT NULL,
|
||||
license jsonb NOT NULL,
|
||||
created_at timestamp with time zone NOT NULL
|
||||
);
|
6
coderd/database/migrations/000002_projects.down.sql
Normal file
6
coderd/database/migrations/000002_projects.down.sql
Normal file
@ -0,0 +1,6 @@
|
||||
DROP TABLE project_versions;
|
||||
|
||||
DROP TABLE projects;
|
||||
DROP TYPE provisioner_type;
|
||||
|
||||
DROP TABLE files;
|
55
coderd/database/migrations/000002_projects.up.sql
Normal file
55
coderd/database/migrations/000002_projects.up.sql
Normal file
@ -0,0 +1,55 @@
|
||||
-- Store arbitrary data like project source code or avatars.
|
||||
CREATE TABLE files (
|
||||
hash varchar(64) NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
created_by text NOT NULL,
|
||||
mimetype varchar(64) NOT NULL,
|
||||
data bytea NOT NULL
|
||||
);
|
||||
|
||||
CREATE TYPE provisioner_type AS ENUM ('echo', 'terraform');
|
||||
|
||||
-- Project defines infrastructure that your software project
|
||||
-- requires for development.
|
||||
CREATE TABLE projects (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
-- Projects must be scoped to an organization.
|
||||
organization_id text NOT NULL,
|
||||
deleted boolean NOT NULL DEFAULT FALSE,
|
||||
name varchar(64) NOT NULL,
|
||||
provisioner provisioner_type NOT NULL,
|
||||
-- Target's a Project Version to use for Workspaces.
|
||||
-- If a Workspace doesn't match this version, it will be prompted to rebuild.
|
||||
active_version_id uuid NOT NULL,
|
||||
-- Disallow projects to have the same name under
|
||||
-- the same organization.
|
||||
UNIQUE(organization_id, name)
|
||||
);
|
||||
|
||||
-- Enforces no active projects have the same name.
|
||||
CREATE UNIQUE INDEX ON projects (organization_id, name) WHERE deleted = FALSE;
|
||||
|
||||
-- Project Versions store historical project data. When a Project Version is imported,
|
||||
-- an "import" job is queued to parse parameters. A Project Version
|
||||
-- can only be used if the import job succeeds.
|
||||
CREATE TABLE project_versions (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
-- This should be indexed.
|
||||
project_id uuid REFERENCES projects (id),
|
||||
organization_id text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
-- Name is generated for ease of differentiation.
|
||||
-- eg. TheCozyRabbit16
|
||||
name varchar(64) NOT NULL,
|
||||
-- Extracted from a README.md on import.
|
||||
-- Maximum of 1MB.
|
||||
description varchar(1048576) NOT NULL,
|
||||
-- The job ID for building the project version.
|
||||
job_id uuid NOT NULL,
|
||||
-- Disallow projects to have the same build name
|
||||
-- multiple times.
|
||||
UNIQUE(project_id, name)
|
||||
);
|
2
coderd/database/migrations/000003_workspaces.down.sql
Normal file
2
coderd/database/migrations/000003_workspaces.down.sql
Normal file
@ -0,0 +1,2 @@
|
||||
DROP TYPE workspace_transition;
|
||||
DROP TABLE workspaces
|
19
coderd/database/migrations/000003_workspaces.up.sql
Normal file
19
coderd/database/migrations/000003_workspaces.up.sql
Normal file
@ -0,0 +1,19 @@
|
||||
CREATE TABLE workspaces (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
owner_id text NOT NULL,
|
||||
project_id uuid NOT NULL REFERENCES projects (id),
|
||||
deleted boolean NOT NULL DEFAULT FALSE,
|
||||
name varchar(64) NOT NULL
|
||||
);
|
||||
|
||||
-- Enforces no active workspaces have the same name.
|
||||
CREATE UNIQUE INDEX ON workspaces (owner_id, name) WHERE deleted = FALSE;
|
||||
|
||||
CREATE TYPE workspace_transition AS ENUM (
|
||||
'start',
|
||||
'stop',
|
||||
'delete'
|
||||
);
|
||||
|
21
coderd/database/migrations/000004_jobs.down.sql
Normal file
21
coderd/database/migrations/000004_jobs.down.sql
Normal file
@ -0,0 +1,21 @@
|
||||
DROP TABLE workspace_builds;
|
||||
|
||||
DROP TABLE parameter_values;
|
||||
DROP TABLE parameter_schemas;
|
||||
DROP TYPE parameter_destination_scheme;
|
||||
DROP TYPE parameter_source_scheme;
|
||||
DROP TYPE parameter_type_system;
|
||||
DROP TYPE parameter_scope;
|
||||
|
||||
DROP TABLE workspace_agents;
|
||||
DROP TABLE workspace_resources;
|
||||
|
||||
DROP TABLE provisioner_job_logs;
|
||||
DROP TYPE log_source;
|
||||
DROP TYPE log_level;
|
||||
|
||||
DROP TABLE provisioner_jobs;
|
||||
DROP TYPE provisioner_storage_method;
|
||||
DROP TYPE provisioner_job_type;
|
||||
|
||||
DROP TABLE provisioner_daemons;
|
167
coderd/database/migrations/000004_jobs.up.sql
Normal file
167
coderd/database/migrations/000004_jobs.up.sql
Normal file
@ -0,0 +1,167 @@
|
||||
CREATE TABLE IF NOT EXISTS provisioner_daemons (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz,
|
||||
organization_id text,
|
||||
-- Name is generated for ease of differentiation.
|
||||
-- eg. WowBananas16
|
||||
name varchar(64) NOT NULL UNIQUE,
|
||||
provisioners provisioner_type [ ] NOT NULL
|
||||
);
|
||||
|
||||
CREATE TYPE provisioner_job_type AS ENUM (
|
||||
'project_version_import',
|
||||
'workspace_build'
|
||||
);
|
||||
|
||||
CREATE TYPE provisioner_storage_method AS ENUM ('file');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS provisioner_jobs (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
started_at timestamptz,
|
||||
canceled_at timestamptz,
|
||||
completed_at timestamptz,
|
||||
error text,
|
||||
organization_id text NOT NULL,
|
||||
initiator_id text NOT NULL,
|
||||
provisioner provisioner_type NOT NULL,
|
||||
storage_method provisioner_storage_method NOT NULL,
|
||||
storage_source text NOT NULL,
|
||||
type provisioner_job_type NOT NULL,
|
||||
input jsonb NOT NULL,
|
||||
worker_id uuid
|
||||
);
|
||||
|
||||
CREATE TYPE log_level AS ENUM (
|
||||
'trace',
|
||||
'debug',
|
||||
'info',
|
||||
'warn',
|
||||
'error'
|
||||
);
|
||||
|
||||
CREATE TYPE log_source AS ENUM (
|
||||
'provisioner_daemon',
|
||||
'provisioner'
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS provisioner_job_logs (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
job_id uuid NOT NULL REFERENCES provisioner_jobs (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL,
|
||||
source log_source NOT NULL,
|
||||
level log_level NOT NULL,
|
||||
output varchar(1024) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE workspace_resources (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
job_id uuid NOT NULL REFERENCES provisioner_jobs (id) ON DELETE CASCADE,
|
||||
transition workspace_transition NOT NULL,
|
||||
address varchar(256) NOT NULL,
|
||||
type varchar(192) NOT NULL,
|
||||
name varchar(64) NOT NULL,
|
||||
agent_id uuid
|
||||
);
|
||||
|
||||
CREATE TABLE workspace_agents (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
first_connected_at timestamptz,
|
||||
last_connected_at timestamptz,
|
||||
disconnected_at timestamptz,
|
||||
resource_id uuid NOT NULL REFERENCES workspace_resources (id) ON DELETE CASCADE,
|
||||
auth_token uuid NOT NULL UNIQUE,
|
||||
auth_instance_id varchar(64),
|
||||
environment_variables jsonb,
|
||||
startup_script varchar(65534),
|
||||
instance_metadata jsonb,
|
||||
resource_metadata jsonb
|
||||
);
|
||||
|
||||
CREATE TYPE parameter_scope AS ENUM (
|
||||
'organization',
|
||||
'project',
|
||||
'import_job',
|
||||
'user',
|
||||
'workspace'
|
||||
);
|
||||
|
||||
-- Types of parameters the automator supports.
|
||||
CREATE TYPE parameter_type_system AS ENUM ('none', 'hcl');
|
||||
|
||||
-- Supported schemes for a parameter source.
|
||||
CREATE TYPE parameter_source_scheme AS ENUM('none', 'data');
|
||||
|
||||
-- Supported schemes for a parameter destination.
|
||||
CREATE TYPE parameter_destination_scheme AS ENUM('none', 'environment_variable', 'provisioner_variable');
|
||||
|
||||
-- Stores project version parameters parsed on import.
|
||||
-- No secrets are stored here.
|
||||
--
|
||||
-- All parameter validation occurs server-side to process
|
||||
-- complex validations.
|
||||
--
|
||||
-- Parameter types, description, and validation will produce
|
||||
-- a UI for users to enter values.
|
||||
-- Needs to be made consistent with the examples below.
|
||||
CREATE TABLE parameter_schemas (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
job_id uuid NOT NULL REFERENCES provisioner_jobs (id) ON DELETE CASCADE,
|
||||
name varchar(64) NOT NULL,
|
||||
description varchar(8192) NOT NULL DEFAULT '',
|
||||
default_source_scheme parameter_source_scheme,
|
||||
default_source_value text NOT NULL,
|
||||
-- Allows the user to override the source.
|
||||
allow_override_source boolean NOT null,
|
||||
default_destination_scheme parameter_destination_scheme,
|
||||
-- Allows the user to override the destination.
|
||||
allow_override_destination boolean NOT null,
|
||||
default_refresh text NOT NULL,
|
||||
-- Whether the consumer can view the source and destinations.
|
||||
redisplay_value boolean NOT null,
|
||||
-- This error would appear in the UI if the condition is not met.
|
||||
validation_error varchar(256) NOT NULL,
|
||||
validation_condition varchar(512) NOT NULL,
|
||||
validation_type_system parameter_type_system NOT NULL,
|
||||
validation_value_type varchar(64) NOT NULL,
|
||||
UNIQUE(job_id, name)
|
||||
);
|
||||
|
||||
-- Parameters are provided to jobs for provisioning and to workspaces.
|
||||
CREATE TABLE parameter_values (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
scope parameter_scope NOT NULL,
|
||||
scope_id text NOT NULL,
|
||||
name varchar(64) NOT NULL,
|
||||
source_scheme parameter_source_scheme NOT NULL,
|
||||
source_value text NOT NULL,
|
||||
destination_scheme parameter_destination_scheme NOT NULL,
|
||||
-- Prevents duplicates for parameters in the same scope.
|
||||
UNIQUE(scope_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE workspace_builds (
|
||||
id uuid NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
workspace_id uuid NOT NULL REFERENCES workspaces (id) ON DELETE CASCADE,
|
||||
project_version_id uuid NOT NULL REFERENCES project_versions (id) ON DELETE CASCADE,
|
||||
name varchar(64) NOT NULL,
|
||||
before_id uuid,
|
||||
after_id uuid,
|
||||
transition workspace_transition NOT NULL,
|
||||
initiator varchar(255) NOT NULL,
|
||||
-- State stored by the provisioner
|
||||
provisioner_state bytea,
|
||||
-- Job ID of the action
|
||||
job_id uuid NOT NULL UNIQUE REFERENCES provisioner_jobs (id) ON DELETE CASCADE,
|
||||
UNIQUE(workspace_id, name)
|
||||
);
|
14
coderd/database/migrations/create_migration.sh
Executable file
14
coderd/database/migrations/create_migration.sh
Executable file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
echo "First argument is the migration name!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
migrate create -ext sql -dir . -seq "$1"
|
||||
|
||||
echo "Run \"make gen\" to generate models."
|
Reference in New Issue
Block a user