Skip to content

Commit

Permalink
QuickFix adding PK Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
nebojsajsimic committed Feb 12, 2025
1 parent e0705a4 commit 8b3f775
Showing 1 changed file with 43 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,99 +1,47 @@
module.exports = {
async up(knex) {
try {
const strapiInstance = global.strapi;
let result =[];
try {
result = await knex.raw('ALTER TABLE "public"."strapi_api_token_permissions" ADD PRIMARY KEY ("id")');
console.log("Primary Key strapi_api_token_permissions created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."strapi_api_tokens" ADD PRIMARY KEY ("id")');
console.log("Primary Key strapi_api_tokens created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."strapi_transfer_token_permissions" ADD PRIMARY KEY ("id")');
console.log("Primary Key strapi_transfer_token_permissions created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."strapi_transfer_tokens" ADD PRIMARY KEY ("id")');
console.log("Primary Key strapi_transfer_tokens created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."files_related_morphs" ADD PRIMARY KEY ("id")');
console.log("Primary Key files_related_morphs created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."files" ADD PRIMARY KEY ("id")');
console.log("Primary Key files created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."upload_folders" ADD PRIMARY KEY ("id")');
console.log("Primary Key upload_folders created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."strapi_release_actions" ADD PRIMARY KEY ("id")');
console.log("Primary Key strapi_release_actions created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."strapi_releases" ADD PRIMARY KEY ("id")');
console.log("Primary Key strapi_releases created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."up_permissions" ADD PRIMARY KEY ("id")');
console.log("Primary Key up_permissions created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."up_roles" ADD PRIMARY KEY ("id")');
console.log("Primary Key up_roles created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."up_users_role_links" ADD PRIMARY KEY ("id")');
console.log("Primary Key up_roles created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."up_users" ADD PRIMARY KEY ("id")');
console.log("Primary Key up_roles created!");
} catch (error) {
console.error('Error creating PK:', error);
}
try {
result = await knex.raw('ALTER TABLE "public"."route_permissions" ADD PRIMARY KEY ("id")');
console.log("Primary Key route_permissions created!");
} catch (error) {
console.error('Error creating PK:', error);
}
console.log('Migration 20250502_1 completed!');
} catch (error) {
console.error('Error running migration:', error);
}
}
}

async up(knex) {
try {
const strapiInstance = global.strapi;
const tables = [
'strapi_api_token_permissions',
'strapi_api_tokens',
'strapi_transfer_token_permissions',
'strapi_transfer_tokens',
'files_related_morphs',
'files',
'upload_folders',
'strapi_release_actions',
'strapi_releases',
'up_permissions',
'up_roles',
'up_users_role_links',
'up_users',
'route_permissions'
];

for (const table of tables) {
try {
const primaryKeyCheck = await knex.raw(`
SELECT constraint_name
FROM information_schema.table_constraints
WHERE table_name = '${table}' AND constraint_type = 'PRIMARY KEY';
`);

if (primaryKeyCheck.rows.length === 0) {
// No primary key exists, create one
await knex.raw(`ALTER TABLE "public"."${table}" ADD PRIMARY KEY ("id")`);
console.log(`Primary Key for ${table} created!`);
} else {
console.log(`Primary Key already exists for ${table}.`);
}
} catch (error) {
console.error(`Error checking/creating PK for ${table}:`, error);
}
}

console.log('Migration 20250502_1 completed!');
} catch (error) {
console.error('Error running migration:', error);
}
}
};

0 comments on commit 8b3f775

Please sign in to comment.