Skip to content

Commit

Permalink
step 3: delete gp_adopted_probes table
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Feb 17, 2025
1 parent 44171f4 commit e231309
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 1,247 deletions.
1,316 changes: 86 additions & 1,230 deletions snapshots/collections-schema.yml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function up (knex) {
if (!tokensTableExists || !probesTableExists || !usersTableExists) {
console.log('Old collections do not exist, migration of data is not required.');
} else {
await trx.raw(`INSERT INTO gp_adopted_probes (
await trx.raw(`INSERT INTO gp_probes (
asn,
country,
city,
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function up (knex) {
});

await trx('directus_permissions').where('collection', 'adopted_probes').update({
collection: 'gp_adopted_probes',
collection: 'gp_probes',
});

await trx('directus_permissions').where('collection', 'credits').update({
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/migrations/20240202GP-add-indexes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export async function up (knex) {
await knex.raw(`ALTER TABLE gp_tokens ADD INDEX value_index (value);`);
await knex.raw(`ALTER TABLE gp_adopted_probes ADD INDEX ip_index (ip);`);
await knex.raw(`ALTER TABLE gp_probes ADD INDEX ip_index (ip);`);
await knex.raw(`ALTER TABLE gp_credits ADD INDEX user_id_index (user_id);`);
await knex.raw(`ALTER TABLE gp_credits_deductions ADD INDEX user_id_and_date_index (user_id, date);`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const DIRECTUS_URL = process.env.DIRECTUS_URL;
const ADMIN_ACCESS_TOKEN = process.env.ADMIN_ACCESS_TOKEN;
const USER_POLICY_NAME = 'User';

const COLLECTION_NAME = 'gp_adopted_probes';
const COLLECTION_NAME = 'gp_probes';
const FIELDS_TO_REMOVE = [];
const FIELDS_TO_ADD = [ 'hardwareDevice' ];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const DIRECTUS_URL = process.env.DIRECTUS_URL;
const ADMIN_ACCESS_TOKEN = process.env.ADMIN_ACCESS_TOKEN;
const USER_POLICY_NAME = 'User';

const COLLECTION_NAME = 'gp_adopted_probes';
const COLLECTION_NAME = 'gp_probes';
const FIELDS_TO_REMOVE = [];
const FIELDS_TO_ADD = [ 'nodeVersion' ];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUserPermissions, editPermissions } from '../migration-utils.js';

const COLLECTION_NAME = 'gp_adopted_probes';
const COLLECTION_NAME = 'gp_probes';
const FIELDS_TO_ADD = [ 'userId' ];
const FIELDS_TO_REMOVE = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUserPermissions, editPermissions } from '../migration-utils.js';

const COLLECTION_NAME = 'gp_adopted_probes';
const COLLECTION_NAME = 'gp_probes';
const FIELDS_TO_ADD = [ 'altIps' ];
const FIELDS_TO_REMOVE = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { randomUUID } from 'node:crypto';

export async function up (knex) {
const columnsInfo = await knex('gp_adopted_probes').columnInfo();
const columnsInfo = await knex('gp_probes').columnInfo();

if (columnsInfo?.id?.type === 'char') {
console.log('Probe ids are already UUIDs');
Expand All @@ -14,22 +14,22 @@ export async function up (knex) {
});

// Update primary col type
await knex.raw(`ALTER TABLE gp_adopted_probes MODIFY id char(36) not null;`);
await knex.raw(`ALTER TABLE gp_probes MODIFY id char(36) not null;`);

// Update reference col type
await knex.raw(`ALTER TABLE gp_credits_additions MODIFY adopted_probe char(36);`);

// Update columns to new values
await knex.transaction(async (trx) => {
const result = await trx('gp_adopted_probes').select('id');
const result = await trx('gp_probes').select('id');
const prevIdToNewId = {};

for (const { id: prevId } of result) {
prevIdToNewId[prevId] = randomUUID();
}

for (const [ prevId, newId ] of Object.entries(prevIdToNewId)) {
await trx('gp_adopted_probes').where({ id: prevId }).update({ id: newId });
await trx('gp_probes').where({ id: prevId }).update({ id: newId });
await trx('gp_credits_additions').where({ adopted_probe: prevId }).update({ adopted_probe: newId });
}
});
Expand All @@ -38,7 +38,7 @@ export async function up (knex) {
await knex.schema.alterTable('gp_credits_additions', (table) => {
table.foreign('adopted_probe', 'gp_credits_additions_adopted_probe_foreign')
.references('id')
.inTable('gp_adopted_probes')
.inTable('gp_probes')
.onDelete('SET NULL');
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUserPermissions, editPermissions } from '../migration-utils.js';

const COLLECTION_NAME = 'gp_adopted_probes';
const COLLECTION_NAME = 'gp_probes';
const FIELDS_TO_ADD = [ 'state', 'onlineTimesToday', 'isIPv4Supported', 'isIPv6Supported' ];
const FIELDS_TO_REMOVE = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUserPermissions, editPermissions } from '../migration-utils.js';

const COLLECTION_NAME = 'gp_adopted_probes';
const COLLECTION_NAME = 'gp_probes';
const FIELDS_TO_ADD = [ 'systemTags' ];
const FIELDS_TO_REMOVE = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getUserPermissions, editPermissions } from '../migration-utils.js';

const COLLECTION_NAME = 'gp_adopted_probes';
const COLLECTION_NAME = 'gp_probes';
const FIELDS_TO_ADD = [ 'hardwareDeviceFirmware' ];
const FIELDS_TO_REMOVE = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export async function up (knex) {
const probesWithTags = await knex('gp_adopted_probes').select().whereNot({ tags: '[]' });
const probesWithTags = await knex('gp_probes').select().whereNot({ tags: '[]' });

probesWithTags.forEach((probe) => {
probe.tags = JSON.stringify(JSON.parse(probe.tags).map(tag => ({ ...tag, format: 'v1' })));
});

for (const probe of probesWithTags) {
await knex('gp_adopted_probes').where({ id: probe.id }).update({ tags: probe.tags });
await knex('gp_probes').where({ id: probe.id }).update({ tags: probe.tags });
}

console.log('Marked user tags as old format');
Expand Down

0 comments on commit e231309

Please sign in to comment.