Skip to content

Commit

Permalink
Merge pull request #9 from ezequielramos/main
Browse files Browse the repository at this point in the history
fix bin
  • Loading branch information
ezequielramos authored May 6, 2024
2 parents c8a57ef + a4b79fc commit 079d9f0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
46 changes: 29 additions & 17 deletions bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,31 @@ export { initializeModels };

}

async function loadGinaLib(pathName) {
try {
const ginaLib = await import(pathName + '/node_modules/gina-sequelize/dist/gina.js');
return ginaLib.default.default;
} catch {
const ginaLib = await import(pathName + '/lib/gina.ts');
return ginaLib.default;
}
}

async function generateMigration(migrationName) {
try {
const pathName = process.cwd();
const initFile = await import(pathName + '/gina/initializeModels.ts');

console.log('loading models...');
const sequelize = await initFile.initializeModels();
let initializeModels = initFile.initializeModels;
if (!initializeModels) {
initializeModels = initFile.default.initializeModels;
}
const sequelize = await initializeModels();

console.log('loading gina lib...');
try {
const ginaLib = await import(pathName + '/node_modules/gina-sequelize/dist/gina.js');
await ginaLib.default.createMigration(sequelize, migrationName);
} catch (err) {
const ginaLib = await import(pathName + '/lib/gina.ts');
await ginaLib.default.createMigration(sequelize, migrationName);
}
const ginaLib = await loadGinaLib(pathName);
await ginaLib.createMigration(sequelize, migrationName);

process.exit();
} catch (err) {
Expand All @@ -144,7 +153,11 @@ async function upgrade() {
const initFile = await import(pathName + '/gina/initializeModels.ts');

console.log('loading models...');
const sequelize = await initFile.initializeModels();
let initializeModels = initFile.initializeModels;
if (!initializeModels) {
initializeModels = initFile.default.initializeModels;
}
const sequelize = await initializeModels();

console.log('loading migrations...');
const files = (await readdir(pathName + '/gina/migrations')).filter(file => !file.includes('.d.ts') && !file.includes('.js.map'));
Expand All @@ -154,17 +167,16 @@ async function upgrade() {

for (const file of files) {
const filePath = await import(pathName + '/gina/migrations/' + file);
migrations[file.replace('.ts', '')] = filePath;
if (filePath.default) {
migrations[file.replace('.ts', '')] = filePath.default;
} else {
migrations[file.replace('.ts', '')] = filePath;
}
}

console.log('loading gina lib...');
try {
const ginaLib = await import(pathName + '/node_modules/gina-sequelize/dist/gina.js');
await ginaLib.default.runMigrations(sequelize, migrations);
} catch (err) {
const ginaLib = await import(pathName + '/lib/gina.ts');
await ginaLib.default.runMigrations(sequelize, migrations);
}
const ginaLib = await loadGinaLib(pathName);
await ginaLib.runMigrations(sequelize, migrations);

process.exit();
} catch (err) {
Expand Down
46 changes: 37 additions & 9 deletions lib/gina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Migration {
if (modelAttrs[modelAttrsKey].references) {
const references = modelAttrs[modelAttrsKey].references;
if (typeof references !== 'string') {
this.upForeignKey = `
this.upForeignKey += `
await queryInterface.addConstraint('${model.getTableName()}', {
type: 'foreign key',
fields: ['${modelAttrs[modelAttrsKey].field}'],
Expand All @@ -198,6 +198,21 @@ class Migration {

}

const modelIndexes = model.options.indexes;

if (modelIndexes) {
for (const modelIndex of modelIndexes) {
this.upIndexes += `
await queryInterface.addIndex('${model.getTableName()}', ['${modelIndex.fields?.join('\', \'')}'], {
name: '${modelIndex.name}',
unique: ${modelIndex.unique},
});
`;
this.downIndexes = `
await queryInterface.removeIndex('${model.getTableName()}', '${modelIndex.name}');
` + this.downIndexes;
}
}
this.upTables += tabsToSpace(`\t\t});`);

this.downTables = tabsToSpace(`\n\t\tawait queryInterface.dropTable('${model.getTableName()}');\n`) + this.downTables;
Expand Down Expand Up @@ -297,16 +312,24 @@ class Migration {

const modelFields: string[] = [];

// const modelRefs: string[] = [];

// for (const modelAttr of Object.keys(modelAttrs)) {
// if (modelAttrs[modelAttr].references) {
// modelRefs.push(modelAttrs[modelAttr].field || '');
// }
// }

// check each field
for (const modelAttr of Object.keys(modelAttrs)) {
modelFields.push(modelAttrs[modelAttr].field || modelAttr);
if (!Object.keys(attrs).includes(modelAttrs[modelAttr].field || modelAttr) && !(modelAttrs[modelAttr].type instanceof DataTypes.VIRTUAL)) {
this.newColumn(modelTable, modelAttrs[modelAttr]);

if (modelAttrs[modelAttr].references) {
const references = modelAttrs[modelAttr].references;
if (typeof references !== 'string') {
this.upForeignKey = `

this.upForeignKey += `
await queryInterface.addConstraint('${modelTable}', {
type: 'foreign key',
fields: ['${modelAttrs[modelAttr].field}'],
Expand All @@ -324,6 +347,7 @@ class Migration {
}
}

// drop column
for (const attr of Object.keys(attrs)) {
if (!modelFields.includes(attr)) {
this.dropColumn(modelTable, attr, attrs[attr]);
Expand All @@ -337,15 +361,15 @@ class Migration {
if (modelIndexes) {
for (const modelIndex of modelIndexes) {
if (!indexes.find((index) => index.name == modelIndex.name)) {
this.upIndexes = `
this.upIndexes += `
await queryInterface.addIndex('${modelTable}', ['${modelIndex.fields?.join('\', \'')}'], {
name: '${modelIndex.name}',
unique: ${modelIndex.unique},
});
`;
this.downFields = `
this.downIndexes = `
await queryInterface.removeIndex('${modelTable}', '${modelIndex.name}');
`;
` + this.downIndexes;
}
}
}
Expand All @@ -362,18 +386,22 @@ class Migration {
continue;
}

// if (modelRefs.includes(index.name)) {
// continue;
// }

if (modelIndexes === undefined || !modelIndexes.find((modelIndex) => index.name == modelIndex.name)) {
this.upIndexes = `
this.upIndexes += `
await queryInterface.removeIndex('${modelTable}', '${index.name}');
`;

this.downFields = `
this.downIndexes = `
await queryInterface.addIndex('${modelTable}', ['${index.fields?.map(field => field.attribute).join('\', \'')}'], {
name: '${index.name}',
unique: ${index.unique},
});
`;
` + this.downIndexes;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"gina-cli": "./bin/index.ts"
},
"types": "./dist/gina.d.ts",
"version": "0.0.12",
"version": "0.0.13",
"description": "Gina is a tool to auto-generate and run migrations based on Sequelize ORM.",
"scripts": {
"eslint": "eslint .",
"eslint_fix": "eslint . --fix",
"build": "tsc"
"build": "tsc",
"gina-cli": "ts-node --esm ./bin/index.ts"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 079d9f0

Please sign in to comment.