-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
c0d411a
commit 79d39f9
Showing
4 changed files
with
143 additions
and
1 deletion.
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
139 changes: 139 additions & 0 deletions
139
packages/core/database/src/migrations/internal-migrations/document-id.ts
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,139 @@ | ||
import { createId } from '@paralleldrive/cuid2'; | ||
import type { Knex } from 'knex'; | ||
|
||
import { Relation } from '../../types'; | ||
import type { Migration } from '../common'; | ||
import type { Database } from '../..'; | ||
import type { Meta } from '../../metadata'; | ||
|
||
const QUERIES = { | ||
postgres: ` | ||
SELECT :tableName:.id as id, string_agg(DISTINCT :inverseJoinColumn:, ',') as otherIds | ||
FROM :tableName: | ||
LEFT JOIN :joinTableName: ON :tableName:.id = :joinTableName:.:joinColumn: | ||
WHERE document_id IS NULL | ||
GROUP BY :joinColumn: | ||
LIMIT 1; | ||
`, | ||
mysql: ` | ||
SELECT :tableName:.id as id, group_concat(DISTINCT :inverseJoinColumn:) as otherIds | ||
FROM :tableName: | ||
LEFT JOIN :joinTableName: ON :tableName:.id = :joinTableName:.:joinColumn: | ||
WHERE document_id IS NULL | ||
GROUP BY :joinColumn: | ||
LIMIT 1; | ||
`, | ||
sqlite: ` | ||
SELECT :tableName:.id as id, group_concat(DISTINCT :inverseJoinColumn:) as otherIds | ||
FROM :tableName: | ||
LEFT JOIN :joinTableName: ON :tableName:.id = :joinTableName:.:joinColumn: | ||
WHERE document_id IS NULL | ||
GROUP BY :joinColumn: | ||
LIMIT 1; | ||
`, | ||
}; | ||
|
||
const getNextIdsToCreateDocumentId = async ( | ||
db: Database, | ||
knex: Knex, | ||
{ | ||
joinColumn, | ||
inverseJoinColumn, | ||
tableName, | ||
joinTableName, | ||
}: { | ||
joinColumn: string; | ||
inverseJoinColumn: string; | ||
tableName: string; | ||
joinTableName: string; | ||
} | ||
): Promise<number[]> => { | ||
const res = await knex.raw(QUERIES[db.dialect.client as keyof typeof QUERIES], { | ||
joinColumn, | ||
inverseJoinColumn, | ||
tableName, | ||
joinTableName, | ||
}); | ||
|
||
if (res.length > 0) { | ||
const row = res[0]; | ||
const otherIds = row.otherIds | ||
? row.otherIds.split(',').map((v: string) => parseInt(v, 10)) | ||
: []; | ||
|
||
return [row.id, ...otherIds]; | ||
} | ||
|
||
return []; | ||
}; | ||
|
||
// Migrate document ids for tables that have localizations | ||
const migrateDocumentIdsWithLocalizations = async (db: Database, knex: Knex, meta: Meta) => { | ||
const joinColumn = `${meta.singularName.toLowerCase()}_id`; | ||
const inverseJoinColumn = `inv_${meta.singularName.toLowerCase()}_id`; | ||
|
||
let ids: number[]; | ||
|
||
do { | ||
ids = await getNextIdsToCreateDocumentId(db, knex, { | ||
joinColumn, | ||
inverseJoinColumn, | ||
tableName: meta.tableName, | ||
joinTableName: `${meta.tableName}_localizations_links`, | ||
}); | ||
|
||
if (ids.length > 0) { | ||
await knex(meta.tableName).update({ document_id: createId() }).whereIn('id', ids); | ||
} | ||
} while (ids.length > 0); | ||
}; | ||
|
||
// Migrate document ids for tables that don't have localizations | ||
const migrationDocumentIds = async (db: Database, knex: Knex, meta: Meta) => { | ||
let res; | ||
do { | ||
res = await knex.select('id').from(meta.tableName).whereNull('document_id').limit(1).first(); | ||
|
||
if (res) { | ||
await knex(meta.tableName).update({ document_id: createId() }).whereIn('id', res.id); | ||
} | ||
} while (res !== null); | ||
}; | ||
|
||
const createDocumentIdColumn = async (knex: Knex, tableName: string) => { | ||
await knex.schema.alterTable(tableName, (table) => { | ||
table.string('document_id'); | ||
}); | ||
}; | ||
|
||
const hasLocalizationsJoinTable = async (knex: Knex, tableName: string) => { | ||
return knex.schema.hasTable(`${tableName}_localizations_links`); | ||
}; | ||
|
||
export const createdDocumentId: Migration = { | ||
name: 'created-document-id', | ||
async up(knex, db) { | ||
// do sth | ||
for (const meta of db.metadata.values()) { | ||
if ('documentId' in meta.attributes) { | ||
// add column if doesn't exist | ||
const hasDocumentIdColumn = await knex.schema.hasColumn(meta.tableName, 'document_id'); | ||
|
||
if (hasDocumentIdColumn) { | ||
continue; | ||
} | ||
|
||
await createDocumentIdColumn(knex, meta.tableName); | ||
|
||
if (await hasLocalizationsJoinTable(knex, meta.tableName)) { | ||
await migrateDocumentIdsWithLocalizations(db, knex, meta); | ||
} else { | ||
await migrationDocumentIds(db, knex, meta); | ||
} | ||
} | ||
} | ||
}, | ||
async down() { | ||
throw new Error('not implemented'); | ||
}, | ||
}; |
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