-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1546 from Shelf-nu/fix-prisma-schema
fix: prisma schema mismatch with DB
- Loading branch information
Showing
7 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { protectIndexesInMigration } from "./protected-indexes"; | ||
|
||
// This can be run as part of your migration pipeline | ||
protectIndexesInMigration(); |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* eslint-disable no-console */ | ||
/** | ||
* Configuration and utilities for protecting specific PostgreSQL indexes from being dropped during Prisma migrations. | ||
* This is necessary because Prisma attempts to drop certain many-to-many relationship indexes that we want to maintain. | ||
*/ | ||
|
||
import fs from "fs"; | ||
import path from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
/** | ||
* Indexes that should be protected from being dropped during migrations. | ||
* These are crucial for performance in many-to-many relationship queries. | ||
* | ||
* _AssetToBooking_Asset_idx: Optimizes booking availability checks | ||
* _AssetToTag_asset_idx: Optimizes tag filtering operations | ||
*/ | ||
const PROTECTED_INDEXES = [ | ||
"_AssetToBooking_Asset_idx", // Critical for booking availability checks | ||
"_AssetToTag_asset_idx", // Critical for tag filtering performance | ||
] as const; | ||
|
||
/** | ||
* Processes a newly created migration file to remove any DROP INDEX statements | ||
* for protected indexes. This ensures our critical indexes remain in place. | ||
* | ||
* @throws {Error} If the migrations directory cannot be read or if migration files cannot be processed | ||
*/ | ||
export function protectIndexesInMigration(): void { | ||
try { | ||
// Get the directory path using ESM compatible approach | ||
const currentFilePath = fileURLToPath(import.meta.url); | ||
const currentDir = path.dirname(currentFilePath); | ||
const migrationsDir = path.join(currentDir, "migrations"); | ||
|
||
const migrations = fs | ||
.readdirSync(migrationsDir) | ||
.filter((file) => | ||
fs.statSync(path.join(migrationsDir, file)).isDirectory() | ||
) | ||
.sort((a, b) => b.localeCompare(a)); // Get latest migration first | ||
|
||
const latestMigration = migrations[0]; | ||
if (!latestMigration) { | ||
console.log("No migrations found to process"); | ||
return; | ||
} | ||
|
||
const migrationPath = path.join( | ||
migrationsDir, | ||
latestMigration, | ||
"migration.sql" | ||
); | ||
if (!fs.existsSync(migrationPath)) { | ||
console.log(`No migration file found in ${latestMigration}`); | ||
return; | ||
} | ||
|
||
let content = fs.readFileSync(migrationPath, "utf8"); | ||
let modified = false; | ||
|
||
PROTECTED_INDEXES.forEach((index) => { | ||
const dropIndexPattern = new RegExp( | ||
`-- DropIndex\\s*DROP INDEX (?:IF EXISTS )?["']?${index}["']?;\\s*`, | ||
"gi" | ||
); | ||
|
||
if (dropIndexPattern.test(content)) { | ||
content = content.replace(dropIndexPattern, ""); | ||
modified = true; | ||
console.log(`Protected index ${index} from being dropped`); | ||
} | ||
}); | ||
|
||
if (modified) { | ||
fs.writeFileSync(migrationPath, content); | ||
console.log(`Successfully processed migration ${latestMigration}`); | ||
} else { | ||
console.log("No protected indexes were found in drop statements"); | ||
} | ||
} catch (error) { | ||
console.error("Failed to process migration for protected indexes:", error); | ||
throw error; | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Protected Database Indexes | ||
|
||
## Overview | ||
|
||
This project maintains certain critical indexes that Prisma attempts to drop during migrations. We have implemented an automated protection system to prevent this from happening. | ||
|
||
## Protected Indexes | ||
|
||
- `_AssetToBooking_Asset_idx`: Optimizes queries for checking booking availability | ||
- `_AssetToTag_asset_idx`: Optimizes asset filtering by tags | ||
|
||
## How It Works | ||
|
||
1. When Prisma generates a new migration, our post-migration script automatically removes any DROP INDEX statements for these protected indexes | ||
2. This ensures the indexes remain in place while allowing Prisma migrations to proceed normally | ||
|
||
## Important Notes | ||
|
||
- Never manually drop these indexes | ||
- If you need to modify these indexes, update the PROTECTED_INDEXES array in `prisma/protected-indexes.ts` | ||
- The protection script runs automatically after `prisma migrate dev` and `prisma migrate deploy` | ||
|
||
To find more information about the solution we have implemented you can refer to the PR that made the change [#1546](https://github.com/Shelf-nu/shelf.nu/pull/1546) |
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