-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0705a4
commit 8b3f775
Showing
1 changed file
with
43 additions
and
95 deletions.
There are no files selected for viewing
138 changes: 43 additions & 95 deletions
138
backend/database/migrations/20250502_1_add_strapi_api_token_permissions_pk copy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}; |