BEGIN;
CREATE TABLE IF NOT EXISTS lnwallets (
  id SERIAL PRIMARY KEY,
  owner_id INT NOT NULL,
  name TEXT UNIQUE NOT NULL,
  domain TEXT NOT NULL,
  wallet TEXT NOT NULL,
  create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
  update_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
  CONSTRAINT fk_owner FOREIGN KEY(owner_id) REFERENCES users(id),
  CONSTRAINT ck_name CHECK (name ~ '^[a-z0-9]*$')
);

CREATE  FUNCTION lnwallets_update_ts()
RETURNS TRIGGER AS $$
BEGIN
    NEW.update_ts = now();
    RETURN NEW;
END;
$$ language 'plpgsql';

CREATE TRIGGER update_lnwallets_update_ts
    BEFORE UPDATE
    ON
        lnwallets
    FOR EACH ROW
EXECUTE PROCEDURE lnwallets_update_ts();

COMMIT;
