-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: use string primary keys and normalise Entity/File schema #17
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
Open
johnf
wants to merge
2
commits into
feat/schema-migration-base
Choose a base branch
from
feat/schema-migration-string-pks
base: feat/schema-migration-base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
5 changes: 5 additions & 0 deletions
5
prisma/migrations/20260326000000_lowercase_table_names/migration.sql
This file contains hidden or 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,5 @@ | ||
| -- RenameTable | ||
| RENAME TABLE `Entity` TO `entity`; | ||
|
|
||
| -- RenameTable | ||
| RENAME TABLE `File` TO `file`; |
46 changes: 46 additions & 0 deletions
46
prisma/migrations/20260327000000_schema_improvements/migration.sql
This file contains hidden or 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,46 @@ | ||
| -- AlterTable: reduce column sizes on entity | ||
| ALTER TABLE `entity` MODIFY `rocrateId` VARCHAR(768) NOT NULL, | ||
| MODIFY `name` VARCHAR(512) NOT NULL, | ||
| MODIFY `entityType` VARCHAR(255) NOT NULL, | ||
| MODIFY `memberOf` VARCHAR(768) NULL, | ||
| MODIFY `rootCollection` VARCHAR(768) NULL, | ||
| MODIFY `metadataLicenseId` VARCHAR(255) NOT NULL, | ||
| MODIFY `contentLicenseId` VARCHAR(255) NOT NULL; | ||
|
|
||
| -- AlterTable: reduce column sizes on file, add entityId | ||
| ALTER TABLE `file` MODIFY `fileId` VARCHAR(768) NOT NULL; | ||
|
|
||
| -- Populate entityId from entity.fileId before making it required | ||
| ALTER TABLE `file` ADD COLUMN `entityId` VARCHAR(768) NULL; | ||
|
|
||
| UPDATE `file` | ||
| INNER JOIN `entity` ON `entity`.`fileId` = `file`.`fileId` | ||
| SET `file`.`entityId` = `entity`.`rocrateId`; | ||
|
|
||
| ALTER TABLE `file` MODIFY `entityId` VARCHAR(768) NOT NULL; | ||
|
|
||
| -- DropColumn: remove duplicated fields from file | ||
| ALTER TABLE `file` DROP COLUMN `contentLicenseId`, | ||
| DROP COLUMN `memberOf`, | ||
| DROP COLUMN `rootCollection`; | ||
|
|
||
| -- DropColumn: remove fileId from entity | ||
| ALTER TABLE `entity` DROP COLUMN `fileId`; | ||
|
|
||
| -- CreateIndex: unique constraint on entity.rocrateId | ||
| CREATE UNIQUE INDEX `entity_rocrateId_key` ON `entity`(`rocrateId`); | ||
|
|
||
| -- CreateIndex: indexes on entity | ||
| CREATE INDEX `entity_memberOf_idx` ON `entity`(`memberOf`); | ||
| CREATE INDEX `entity_rootCollection_idx` ON `entity`(`rootCollection`); | ||
| CREATE INDEX `entity_entityType_idx` ON `entity`(`entityType`); | ||
|
|
||
| -- DropIndex: old prefix-based unique index on file.fileId | ||
| DROP INDEX `File_fileId_key` ON `file`; | ||
|
|
||
| -- CreateIndex: unique constraints on file | ||
| CREATE UNIQUE INDEX `file_fileId_key` ON `file`(`fileId`); | ||
| CREATE UNIQUE INDEX `file_entityId_key` ON `file`(`entityId`); | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE `file` ADD CONSTRAINT `file_entityId_fkey` FOREIGN KEY (`entityId`) REFERENCES `entity`(`rocrateId`) ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains hidden or 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,21 @@ | ||
| -- Drop foreign key constraint | ||
| ALTER TABLE `file` DROP FOREIGN KEY `file_entityId_fkey`; | ||
|
|
||
| -- Drop unique constraints that reference old columns | ||
| DROP INDEX `entity_rocrateId_key` ON `entity`; | ||
| DROP INDEX `file_fileId_key` ON `file`; | ||
|
|
||
| -- Entity: drop auto-increment PK, rename rocrateId to id, make it the PK | ||
| ALTER TABLE `entity` DROP PRIMARY KEY, | ||
| DROP COLUMN `id`, | ||
| CHANGE COLUMN `rocrateId` `id` VARCHAR(768) NOT NULL, | ||
| ADD PRIMARY KEY (`id`); | ||
|
|
||
| -- File: drop auto-increment PK, rename fileId to id, make it the PK | ||
| ALTER TABLE `file` DROP PRIMARY KEY, | ||
| DROP COLUMN `id`, | ||
| CHANGE COLUMN `fileId` `id` VARCHAR(768) NOT NULL, | ||
| ADD PRIMARY KEY (`id`); | ||
|
|
||
| -- Re-add foreign key referencing the renamed Entity.id | ||
| ALTER TABLE `file` ADD CONSTRAINT `file_entityId_fkey` FOREIGN KEY (`entityId`) REFERENCES `entity`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains hidden or 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,20 +1,24 @@ | ||
| model Entity { | ||
| id Int @id @default(autoincrement()) | ||
| id String @id @db.VarChar(768) | ||
|
|
||
| rocrateId String @db.VarChar(2048) | ||
| name String @db.VarChar(512) | ||
| description String @db.Text | ||
|
|
||
| name String @db.VarChar(1024) | ||
| description String @db.Text | ||
| entityType String @db.VarChar(255) | ||
| memberOf String? @db.VarChar(768) | ||
| rootCollection String? @db.VarChar(768) | ||
| metadataLicenseId String @db.VarChar(255) | ||
| contentLicenseId String @db.VarChar(255) | ||
|
|
||
| entityType String @db.VarChar(1024) | ||
| memberOf String? @db.VarChar(2048) | ||
| rootCollection String? @db.VarChar(2048) | ||
| metadataLicenseId String @db.VarChar(2048) | ||
| contentLicenseId String @db.VarChar(2048) | ||
| fileId String? @db.VarChar(2048) | ||
| meta Json? | ||
|
|
||
| meta Json? | ||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
|
|
||
| createdAt DateTime @default(now()) | ||
| updatedAt DateTime @updatedAt | ||
| file File? | ||
|
|
||
| @@index([memberOf]) | ||
| @@index([rootCollection]) | ||
| @@index([entityType]) | ||
| @@map("entity") | ||
| } |
This file contains hidden or 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,20 +1,18 @@ | ||||||
| model File { | ||||||
| id Int @id @default(autoincrement()) | ||||||
| id String @id @db.VarChar(768) | ||||||
| entityId String @db.VarChar(768) | ||||||
|
|
||||||
| fileId String @db.VarChar(2048) | ||||||
| filename String @db.VarChar(255) | ||||||
| mediaType String @db.VarChar(127) | ||||||
| size BigInt | ||||||
|
|
||||||
| filename String @db.VarChar(255) | ||||||
| mediaType String @db.VarChar(127) | ||||||
| size BigInt | ||||||
| meta Json? | ||||||
|
|
||||||
| memberOf String @db.VarChar(2048) | ||||||
| rootCollection String @db.VarChar(2048) | ||||||
| contentLicenseId String @db.VarChar(2048) | ||||||
| createdAt DateTime @default(now()) | ||||||
| updatedAt DateTime @updatedAt | ||||||
|
|
||||||
| meta Json? | ||||||
| entity Entity @relation(fields: [entityId], references: [id]) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example if we get rid of the entityId |
||||||
|
|
||||||
| createdAt DateTime @default(now()) | ||||||
| updatedAt DateTime @updatedAt | ||||||
|
|
||||||
| @@unique([fileId]) | ||||||
| @@unique([entityId]) | ||||||
| @@map("file") | ||||||
| } | ||||||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also remove the entityId altogether because the id is already unique and corresponds one-to-one with the id of the Entity table.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alvinsw I'll see where this ends up after I merge everything.
It was supposed to go but may have been lost when I split all the commits at the end.