Commit df74ad38 authored by Chris Molozian's avatar Chris Molozian
Browse files

Allow user_id to be NULL in purchase but maintain referential integrity.

parent e03e0c9d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ type migrationService struct {

func StartupCheck(logger *zap.Logger, db *sql.DB) {
	migrate.SetTable(migrationTable)
	migrate.SetIgnoreUnknown(true)

	ms := &migrate.AssetMigrationSource{
		Asset: func(path string) ([]byte, error) {
+2 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ DROP TABLE IF EXISTS purchase_receipt;

CREATE TABLE IF NOT EXISTS purchase (
    PRIMARY KEY (transaction_id),
    FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE SET NULL,

    create_time    TIMESTAMPTZ  NOT NULL DEFAULT now(),
    environment    SMALLINT     NOT NULL DEFAULT 0, -- Unknown(0), Sandbox(1), Production(2)
@@ -28,7 +29,7 @@ CREATE TABLE IF NOT EXISTS purchase (
    store          SMALLINT     NOT NULL DEFAULT 0, -- AppleAppStore(0), GooglePlay(1), Huawei(2)
    transaction_id VARCHAR(512) NOT NULL CHECK (length(transaction_id) > 0),
    update_time    TIMESTAMPTZ  NOT NULL DEFAULT now(),
    user_id        UUID         NOT NULL
    user_id        UUID         DEFAULT NULL
);
CREATE INDEX IF NOT EXISTS purchase_user_id_purchase_time_transaction_id_idx
    ON purchase (user_id, purchase_time DESC, transaction_id);
@@ -38,7 +39,6 @@ DROP TABLE IF EXISTS purchase;

CREATE TABLE IF NOT EXISTS purchase_receipt (
    PRIMARY KEY (receipt),
    FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION,

    create_time    TIMESTAMPTZ  NOT NULL DEFAULT now(),
    product_id     VARCHAR(512) NOT NULL,