mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 00:03:40 +00:00
Fix sqlite column update
This commit is contained in:
parent
5f48dbfbe2
commit
fb04c4dcbe
@ -471,7 +471,7 @@ module.exports = function(strapi) {
|
|||||||
const type = getType(attribute, attr);
|
const type = getType(attribute, attr);
|
||||||
|
|
||||||
if (type) {
|
if (type) {
|
||||||
acc.push(`${quote}${attr}${quote} ${type}`);
|
acc.push(`${quote}${attr}${quote} ${type} ${attribute.required ? 'NOT' : ''} NULL `);
|
||||||
}
|
}
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
@ -544,14 +544,12 @@ module.exports = function(strapi) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const createTable = async (table) => {
|
||||||
if (!tableExist) {
|
|
||||||
const defaultAttributeDifinitions = {
|
const defaultAttributeDifinitions = {
|
||||||
mysql: [`id INT AUTO_INCREMENT NOT NULL PRIMARY KEY`],
|
mysql: [`id INT AUTO_INCREMENT NOT NULL PRIMARY KEY`],
|
||||||
pg: [`id SERIAL NOT NULL PRIMARY KEY`],
|
pg: [`id SERIAL NOT NULL PRIMARY KEY`],
|
||||||
sqlite3: ['id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL']
|
sqlite3: ['id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL']
|
||||||
};
|
};
|
||||||
|
|
||||||
let idAttributeBuilder = defaultAttributeDifinitions[definition.client];
|
let idAttributeBuilder = defaultAttributeDifinitions[definition.client];
|
||||||
if (definition.primaryKeyType === 'uuid' && definition.client === 'pg') {
|
if (definition.primaryKeyType === 'uuid' && definition.client === 'pg') {
|
||||||
idAttributeBuilder = ['id uuid NOT NULL DEFAULT uuid_generate_v4() NOT NULL PRIMARY KEY'];
|
idAttributeBuilder = ['id uuid NOT NULL DEFAULT uuid_generate_v4() NOT NULL PRIMARY KEY'];
|
||||||
@ -566,13 +564,16 @@ module.exports = function(strapi) {
|
|||||||
${columns}
|
${columns}
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!tableExist) {
|
||||||
|
await createTable(table);
|
||||||
|
|
||||||
// Generate indexes.
|
// Generate indexes.
|
||||||
await generateIndexes(table, attributes);
|
await generateIndexes(table, attributes);
|
||||||
|
|
||||||
await storeTable(table, attributes);
|
await storeTable(table, attributes);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const columns = Object.keys(attributes);
|
const columns = Object.keys(attributes);
|
||||||
|
|
||||||
// Fetch existing column
|
// Fetch existing column
|
||||||
@ -594,6 +595,14 @@ module.exports = function(strapi) {
|
|||||||
// Generate indexes for new attributes.
|
// Generate indexes for new attributes.
|
||||||
await generateIndexes(table, columnsToAdd);
|
await generateIndexes(table, columnsToAdd);
|
||||||
|
|
||||||
|
let previousAttributes;
|
||||||
|
try {
|
||||||
|
previousAttributes = JSON.parse((await StrapiConfigs.forge({key: `db_model_${table}`}).fetch()).toJSON().value);
|
||||||
|
} catch (err) {
|
||||||
|
await storeTable(table, attributes);
|
||||||
|
previousAttributes = JSON.parse((await StrapiConfigs.forge({key: `db_model_${table}`}).fetch()).toJSON().value);
|
||||||
|
}
|
||||||
|
|
||||||
// Generate and execute query to add missing column
|
// Generate and execute query to add missing column
|
||||||
if (Object.keys(columnsToAdd).length > 0) {
|
if (Object.keys(columnsToAdd).length > 0) {
|
||||||
const columns = generateColumns(columnsToAdd, []);
|
const columns = generateColumns(columnsToAdd, []);
|
||||||
@ -605,31 +614,28 @@ module.exports = function(strapi) {
|
|||||||
await Promise.all(queries.map(query => ORM.knex.raw(query)));
|
await Promise.all(queries.map(query => ORM.knex.raw(query)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let previousAttributes;
|
let sqlite3Change = false;
|
||||||
try {
|
|
||||||
previousAttributes = JSON.parse((await StrapiConfigs.forge({key: `db_model_${table}`}).fetch()).toJSON().value);
|
|
||||||
} catch (err) {
|
|
||||||
await storeTable(table, attributes);
|
|
||||||
previousAttributes = JSON.parse((await StrapiConfigs.forge({key: `db_model_${table}`}).fetch()).toJSON().value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute query to update column type
|
// Execute query to update column type
|
||||||
await Promise.all(columns.map(attribute =>
|
await Promise.all(columns.map(attribute =>
|
||||||
new Promise(async (resolve) => {
|
new Promise(async (resolve) => {
|
||||||
if (JSON.stringify(previousAttributes[attribute]) === JSON.stringify(attributes[attribute])) {
|
if (JSON.stringify(previousAttributes[attribute]) === JSON.stringify(attributes[attribute])) {
|
||||||
return resolve();
|
return resolve();
|
||||||
|
} else {
|
||||||
|
sqlite3Change = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const type = getType(attributes[attribute], attribute);
|
const type = getType(attributes[attribute], attribute);
|
||||||
|
|
||||||
if (type) {
|
if (type && definition.client !== 'sqlite3') {
|
||||||
const changeType = definition.client === 'pg' || definition.client === 'sqlite3'
|
const changeType = definition.client === 'pg'
|
||||||
? `ALTER COLUMN ${quote}${attribute}${quote} TYPE ${type} USING ${quote}${attribute}${quote}::${type}`
|
? `ALTER COLUMN ${quote}${attribute}${quote} TYPE ${type} USING ${quote}${attribute}${quote}::${type}`
|
||||||
: `CHANGE ${quote}${attribute}${quote} ${quote}${attribute}${quote} ${type} `;
|
: `CHANGE ${quote}${attribute}${quote} ${quote}${attribute}${quote} ${type} `;
|
||||||
|
|
||||||
const changeRequired = definition.client === 'pg' || definition.client === 'sqlite3'
|
const changeRequired = definition.client === 'pg'
|
||||||
? `ALTER COLUMN ${quote}${attribute}${quote} ${attributes[attribute].required ? 'SET' : 'DROP'} NOT NULL`
|
? `ALTER COLUMN ${quote}${attribute}${quote} ${attributes[attribute].required ? 'SET' : 'DROP'} NOT NULL`
|
||||||
: `CHANGE ${quote}${attribute}${quote} ${quote}${attribute}${quote} ${type} ${attributes[attribute].required ? 'NOT' : ''} NULL`;
|
: `CHANGE ${quote}${attribute}${quote} ${quote}${attribute}${quote} ${type} ${attributes[attribute].required ? 'NOT' : ''} NULL`;
|
||||||
|
|
||||||
await ORM.knex.raw(`ALTER TABLE ${quote}${table}${quote} ${changeType}`);
|
await ORM.knex.raw(`ALTER TABLE ${quote}${table}${quote} ${changeType}`);
|
||||||
await ORM.knex.raw(`ALTER TABLE ${quote}${table}${quote} ${changeRequired}`);
|
await ORM.knex.raw(`ALTER TABLE ${quote}${table}${quote} ${changeRequired}`);
|
||||||
}
|
}
|
||||||
@ -638,6 +644,28 @@ module.exports = function(strapi) {
|
|||||||
})
|
})
|
||||||
));
|
));
|
||||||
|
|
||||||
|
if (sqlite3Change) {
|
||||||
|
await createTable(`tmp_${table}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await ORM.knex.raw(`INSERT INTO ${quote}tmp_${table}${quote}(${Object.keys(attributes).join(' ,')}) SELECT ${Object.keys(attributes).join(' ,')} FROM ${quote}${table}${quote}`);
|
||||||
|
} catch (err) {
|
||||||
|
console.log('Warning!');
|
||||||
|
console.log('We can\'t migrate your data caused by the following error');
|
||||||
|
console.log();
|
||||||
|
console.log(err);
|
||||||
|
console.log();
|
||||||
|
console.log(`We created a new table "tmp_${table}" with your last update.`);
|
||||||
|
console.log(`We suggest you manually migrate your data from "${table}" to "tmp_${table}" and then to DROP and RENAME tables.`);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
await ORM.knex.raw(`DROP TABLE ${quote}${table}${quote}`);
|
||||||
|
await ORM.knex.raw(`ALTER TABLE ${quote}tmp_${table}${quote} RENAME TO ${quote}${table}${quote}`);
|
||||||
|
|
||||||
|
await generateIndexes(table, attributes);
|
||||||
|
}
|
||||||
|
|
||||||
await storeTable(table, attributes);
|
await storeTable(table, attributes);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user