Files
coder/coderd/database/migrations/000001_base.up.sql
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
3.2 KiB
SQL
Raw Normal View History

2022-01-12 12:04:11 -06:00
-- 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
2022-04-01 14:42:36 -05:00
CREATE TYPE login_type AS ENUM (
2022-04-23 17:58:57 -05:00
'password',
'github'
2022-04-01 14:42:36 -05:00
);
2022-01-12 12:04:11 -06:00
CREATE TABLE IF NOT EXISTS users (
2022-04-01 14:42:36 -05:00
id uuid NOT NULL,
2022-01-12 12:04:11 -06:00
email text NOT NULL,
username text DEFAULT ''::text NOT NULL,
2022-01-12 12:04:11 -06:00
hashed_password bytea NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
2022-04-01 14:42:36 -05:00
PRIMARY KEY (id)
2022-01-12 12:04:11 -06:00
);
2022-04-01 14:42:36 -05:00
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email ON users USING btree (email);
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_username ON users USING btree (username);
CREATE UNIQUE INDEX IF NOT EXISTS users_username_lower_idx ON users USING btree (lower(username));
2022-01-12 12:04:11 -06:00
CREATE TABLE IF NOT EXISTS organizations (
2022-04-01 14:42:36 -05:00
id uuid NOT NULL,
2022-01-12 12:04:11 -06:00
name text NOT NULL,
description text NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
2022-04-01 14:42:36 -05:00
PRIMARY KEY (id)
2022-01-12 12:04:11 -06:00
);
2022-04-01 14:42:36 -05:00
CREATE UNIQUE INDEX IF NOT EXISTS idx_organization_name ON organizations USING btree (name);
CREATE UNIQUE INDEX IF NOT EXISTS idx_organization_name_lower ON organizations USING btree (lower(name));
2022-01-12 12:04:11 -06:00
CREATE TABLE IF NOT EXISTS organization_members (
2022-04-01 14:42:36 -05:00
user_id uuid NOT NULL,
organization_id uuid NOT NULL,
2022-01-12 12:04:11 -06:00
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
2022-04-01 14:42:36 -05:00
roles text [] DEFAULT '{organization-member}' :: text [] NOT NULL,
PRIMARY KEY (organization_id, user_id)
2022-01-12 12:04:11 -06:00
);
2022-04-01 14:42:36 -05:00
CREATE INDEX idx_organization_member_organization_id_uuid ON organization_members USING btree (organization_id);
CREATE INDEX idx_organization_member_user_id_uuid ON organization_members USING btree (user_id);
ALTER TABLE ONLY organization_members
ADD CONSTRAINT organization_members_organization_id_uuid_fkey FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE;
ALTER TABLE ONLY organization_members
ADD CONSTRAINT organization_members_user_id_uuid_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
2022-01-12 12:04:11 -06:00
CREATE TABLE IF NOT EXISTS api_keys (
id text NOT NULL,
hashed_secret bytea NOT NULL,
2022-04-01 14:42:36 -05:00
user_id uuid NOT NULL,
2022-01-12 12:04:11 -06:00
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,
2022-04-23 17:58:57 -05:00
oauth_access_token text DEFAULT ''::text NOT NULL,
oauth_refresh_token text DEFAULT ''::text NOT NULL,
oauth_id_token text DEFAULT ''::text NOT NULL,
oauth_expiry timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL,
2022-04-01 14:42:36 -05:00
PRIMARY KEY (id)
2022-01-12 12:04:11 -06:00
);
2022-04-01 14:42:36 -05:00
CREATE INDEX IF NOT EXISTS idx_api_keys_user ON api_keys USING btree (user_id);
ALTER TABLE ONLY api_keys
ADD CONSTRAINT api_keys_user_id_uuid_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
CREATE TABLE IF NOT EXISTS licenses (
2022-04-01 14:42:36 -05:00
id serial,
2022-01-12 12:04:11 -06:00
license jsonb NOT NULL,
2022-04-01 14:42:36 -05:00
created_at timestamptz NOT NULL,
PRIMARY KEY (id)
2022-01-12 12:04:11 -06:00
);