-
Notifications
You must be signed in to change notification settings - Fork 56
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
6550c90
commit 4bdf84c
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...e-postgresql-schema-for-case-insensitive-and-accent-insensitive-comparison-and-sorting.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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Make sure you properly test your migration, especially DDL (Data Definition Language) | ||
// ! If the target table is large, and the migration take more than 20 minutes, the deployment will fail ! | ||
|
||
// You can design and test your migration to avoid this by following this guide | ||
// https://1024pix.atlassian.net/wiki/spaces/EDTDT/pages/3849323922/Cr+er+une+migration | ||
|
||
// If your migrations target `answers` or `knowledge-elements` | ||
// contact @team-captains, because automatic migrations are not active on `pix-datawarehouse-production` | ||
// this may prevent data replication to succeed the day after your migration is deployed on `pix-api-production` | ||
const TABLE_NAME = 'users'; | ||
const COLUMN_EMAIL2_NAME = 'email2'; | ||
const COLUMN_FIRSTNAME2_NAME = 'firstName2'; | ||
const COLUMN_LASTNAME2_NAME = 'lastName2'; | ||
const COLUMN_EMAIL3_NAME = 'email3'; | ||
const COLUMN_FIRSTNAME3_NAME = 'firstName3'; | ||
const COLUMN_LASTNAME3_NAME = 'lastName3'; | ||
|
||
const up = async function (knex) { | ||
// CITEXT | ||
await knex.schema.raw('create extension if not exists citext'); | ||
|
||
await knex.schema.table(TABLE_NAME, function (table) { | ||
table.specificType(COLUMN_EMAIL2_NAME, 'citext').unique(); //.notNull(); | ||
table.specificType(COLUMN_FIRSTNAME2_NAME, 'citext'); //.notNull(); | ||
table.specificType(COLUMN_LASTNAME2_NAME, 'citext'); //.notNull(); | ||
}); | ||
|
||
// COLLATIONS | ||
await knex.schema.raw('drop collation if exists case_insensitive'); | ||
await knex.schema.raw('drop collation if exists ignore_accents'); | ||
|
||
await knex.schema.raw( | ||
"create collation case_insensitive (provider = icu, locale = 'und-u-ks-level2', deterministic = false)", | ||
); | ||
await knex.schema.raw( | ||
"create collation ignore_accents (provider = icu, locale = 'und-u-ks-level1-kc-true', deterministic = false)", | ||
); | ||
|
||
// await knex.schema.table(TABLE_NAME, function (table) { | ||
// For now only works with MySQL: contrib to Knex the PostgreSQL support! | ||
// table.string(COLUMN_EMAIL3_NAME).unique().collate('case_insensitive'); | ||
// table.string(COLUMN_FIRSTNAME3_NAME).collate('case_insensitive'); | ||
// table.string(COLUMN_LASTNAME3_NAME).collate('ignore_accents'); | ||
// }); | ||
|
||
// eslint-disable-next-line knex/avoid-injections | ||
await knex.schema.raw( | ||
`alter table "users" add column "${COLUMN_EMAIL3_NAME}" character varying(255) collate "case_insensitive"`, | ||
); | ||
// eslint-disable-next-line knex/avoid-injections | ||
await knex.schema.raw( | ||
`alter table "users" add column "${COLUMN_FIRSTNAME3_NAME}" character varying(255) collate "case_insensitive"`, | ||
); | ||
// eslint-disable-next-line knex/avoid-injections | ||
await knex.schema.raw( | ||
`alter table "users" add column "${COLUMN_LASTNAME3_NAME}" character varying(255) collate "case_insensitive"`, | ||
); | ||
}; | ||
|
||
const down = async function (knex) { | ||
await knex.schema.table(TABLE_NAME, function (table) { | ||
table.dropColumn(COLUMN_EMAIL2_NAME); | ||
table.dropColumn(COLUMN_FIRSTNAME2_NAME); | ||
table.dropColumn(COLUMN_LASTNAME2_NAME); | ||
}); | ||
|
||
await knex.schema.table(TABLE_NAME, function (table) { | ||
table.dropColumn(COLUMN_EMAIL3_NAME); | ||
table.dropColumn(COLUMN_FIRSTNAME3_NAME); | ||
table.dropColumn(COLUMN_LASTNAME3_NAME); | ||
}); | ||
}; | ||
|
||
export { down, up }; |