-- +goose Up
-- +goose StatementBegin
CREATE TABLE
    users (
        id SERIAL8 PRIMARY KEY,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now(),
        deleted_at timestamp,
        status INT2 NOT NULL default 0,
        email VARCHAR(128) NOT NULL UNIQUE,
        phone VARCHAR(32) NOT NULL UNIQUE,
        username VARCHAR(128) NOT NULL UNIQUE,
        nickname VARCHAR(128) ,
        password VARCHAR(128) NOT NULL,
        age INT2 NOT NULL default 0,
        sex INT2 NOT NULL default 0,
        avatar VARCHAR(128)
    );

-- index on email phone username
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_phone ON users(phone);
CREATE INDEX idx_users_username ON users(username);


CREATE TABLE
    user_oauths (
        id SERIAL8 PRIMARY KEY,
        created_at timestamp NOT NULL default now(),
        updated_at timestamp NOT NULL default now(),
        deleted_at timestamp,
        channel INT2 NOT NULL,
        user_id INT8 NOT NULL,
        union_id VARCHAR(128),
        open_id VARCHAR(128) NOT NULL UNIQUE,
        access_key VARCHAR(256) NOT NULL default '',
        access_token VARCHAR(256) NOT NULL default '',
        refresh_token VARCHAR(256) NOT NULL default '',
        expire_at timestamp NOT NULL,
        meta jsonb default '{}'::jsonb
    );
-- index on channel user_id open_id
CREATE INDEX idx_user_oauths_channel ON user_oauths(channel);
CREATE INDEX idx_user_oauths_user_id ON user_oauths(user_id);
CREATE INDEX idx_user_oauths_open_id ON user_oauths(open_id);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE users;
DROP TABLE user_oauths;
-- +goose StatementEnd
