Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Settings on Account/DataType deletion #1748

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions migrations/00003_account-data-settings/index.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ CREATE TABLE account_data_settings (
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
PRIMARY KEY (account_id, account_data_type_id),
FOREIGN KEY (account_id) REFERENCES accounts(id),
FOREIGN KEY (account_data_type_id) REFERENCES account_data_types(id)
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE,
FOREIGN KEY (account_data_type_id) REFERENCES account_data_types(id) ON DELETE CASCADE
);

CREATE OR REPLACE TRIGGER update_account_data_settings_updated_at
Expand Down
60 changes: 60 additions & 0 deletions migrations/__tests__/00003_account-data-settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,64 @@ describe('Migration 00003_account-data-settings', () => {
expect(createdAtAfterUpdate).toStrictEqual(createdAt);
expect(updatedAtAfterUpdate.getTime()).toBeGreaterThan(createdAt.getTime());
});

it('should trigger a cascade delete when the referenced account is deleted', async () => {
const accountAddress = getAddress(faker.finance.ethereumAddress());
const name = faker.lorem.word();
let accountRows: AccountRow[] = [];
let accountDataTypeRows: AccountDataTypeRow[] = [];

const {
after: accountDataSettingRows,
}: { after: AccountDataSettingsRow[] } = await migrator.test({
migration: '00003_account-data-settings',
after: async (sql: postgres.Sql): Promise<AccountDataSettingsRow[]> => {
accountRows = await sql<
AccountRow[]
>`INSERT INTO accounts (address) VALUES (${accountAddress}) RETURNING *;`;
accountDataTypeRows = await sql<
AccountDataTypeRow[]
>`INSERT INTO account_data_types (name) VALUES (${name}) RETURNING *;`;
await sql<
AccountDataSettingsRow[]
>`INSERT INTO account_data_settings (account_id, account_data_type_id) VALUES (${accountRows[0].id}, ${accountDataTypeRows[0].id}) RETURNING *;`;
await sql`DELETE FROM accounts WHERE id = ${accountRows[0].id};`;
return sql<
AccountDataSettingsRow[]
>`SELECT * FROM account_data_settings WHERE account_id = ${accountRows[0].id}`;
},
});

expect(accountDataSettingRows).toHaveLength(0);
});

it('should trigger a cascade delete when the referenced data type is deleted', async () => {
const accountAddress = getAddress(faker.finance.ethereumAddress());
const name = faker.lorem.word();
let accountRows: AccountRow[] = [];
let accountDataTypeRows: AccountDataTypeRow[] = [];

const {
after: accountDataSettingRows,
}: { after: AccountDataSettingsRow[] } = await migrator.test({
migration: '00003_account-data-settings',
after: async (sql: postgres.Sql): Promise<AccountDataSettingsRow[]> => {
accountRows = await sql<
AccountRow[]
>`INSERT INTO accounts (address) VALUES (${accountAddress}) RETURNING *;`;
accountDataTypeRows = await sql<
AccountDataTypeRow[]
>`INSERT INTO account_data_types (name) VALUES (${name}) RETURNING *;`;
await sql<
AccountDataSettingsRow[]
>`INSERT INTO account_data_settings (account_id, account_data_type_id) VALUES (${accountRows[0].id}, ${accountDataTypeRows[0].id}) RETURNING *;`;
await sql`DELETE FROM account_data_types WHERE id = ${accountDataTypeRows[0].id};`;
return sql<
AccountDataSettingsRow[]
>`SELECT * FROM account_data_settings WHERE account_id = ${accountRows[0].id}`;
},
});

expect(accountDataSettingRows).toHaveLength(0);
});
});
Loading