BEGIN TRANSACTION;

ALTER TABLE payment RENAME TO tmp_payment;

CREATE TABLE IF NOT EXISTS "payment"(
  "id" INTEGER PRIMARY KEY,
  "user" INTEGER NOT NULL REFERENCES "user",
  "name" VARCHAR NOT NULL,
  "cost" INTEGER NOT NULL,
  "date" DATE NOT NULL,
  "frequency" VARCHAR NOT NULL,
  "created_at" TIMESTAMP NOT NULL,
  "edited_at" TIMESTAMP NULL,
  "deleted_at" TIMESTAMP NULL
);

INSERT INTO payment(id, user, name, cost, date, frequency, created_at, edited_at, deleted_at)
SELECT id, user_id, name, cost, date, frequency, created_at, edited_at, deleted_at
FROM tmp_payment;

DROP TABLE tmp_payment;

COMMIT;
