Skip to content

Commit

Permalink
Feature/fix lower case all column names option (#131)
Browse files Browse the repository at this point in the history
* fixed 'lowerCaseAllColumnNames' option

* updated nmig version

* updated dependencies

* updated configuration

* fixed comment
  • Loading branch information
AnatolyUss authored Aug 30, 2024
1 parent 3b31651 commit 06417d1
Show file tree
Hide file tree
Showing 8 changed files with 1,098 additions and 548 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Or, if you have moved <code>config</code> folder out from Nmig's directory:<br /
<br /><b>Note:</b> "logs_directory" will be created during script execution.</p>

<h3>VERSION</h3>
<p>Current version is 6.1.0</p>
<p>Current version is 6.1.1</p>

<h3>LICENSE</h3>
<p>NMIG is available under "GNU GENERAL PUBLIC LICENSE" (v. 3) <br />
Expand Down
4 changes: 2 additions & 2 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"source" : {
"host" : "127.0.0.1",
"port" : 3306,
"database" : "test_db",
"database" : "sample_stuff",
"charset" : "utf8mb4",
"supportBigNumbers": true,
"user" : "root",
Expand All @@ -31,7 +31,7 @@
"target" : {
"host" : "127.0.0.1",
"port" : 5432,
"database" : "test_db",
"database" : "sample_stuff",
"charset" : "UTF8",
"user" : "postgres",
"password" : "0123456789"
Expand Down
7 changes: 7 additions & 0 deletions config/extra_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
"If you don't need to rename neither tables nor columns, then remove the 'tables' section."
],

"README_lowerCaseAllColumnNames": [
"'lowerCaseAllColumnNames', once set true, instructs nmig to rename/lowercase all columns in all tables.",
"By default, 'lowerCaseAllColumnNames' is false.",
"Note, 're-namings' defined below under 'tables' -> 'columns' will take precedence over 'lowerCaseAllColumnNames: true'."
],
"lowerCaseAllColumnNames": false,

"tables" : [
{
"name" : {
Expand Down
1,549 changes: 1,045 additions & 504 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nmig",
"version": "6.1.0",
"version": "6.1.1",
"description": "The database migration app",
"author": "Anatoly Khaytovich<[email protected]>",
"license": "GPL-3.0",
Expand All @@ -12,30 +12,31 @@
"node": ">=18.16.1"
},
"dependencies": {
"@types/node": "20.4.4",
"@types/uuid": "9.0.2",
"@types/pg": "8.10.2",
"@types/pg-copy-streams": "1.2.2",
"@types/node": "22.5.1",
"@types/uuid": "10.0.0",
"@types/pg": "8.11.8",
"@types/pg-copy-streams": "1.2.5",
"json2csv": "5.0.7",
"mysql2": "3.5.2",
"pg": "8.11.1",
"mysql2": "3.11.0",
"pg": "8.12.0",
"pg-copy-streams": "6.0.6",
"uuid": "9.0.0"
"uuid": "10.0.0"
},
"devDependencies": {
"@types/tape": "5.6.0",
"@types/tape": "5.6.4",
"@typescript-eslint/eslint-plugin": "5.61.0",
"@typescript-eslint/parser": "5.61.0",
"eslint": "8.44.0",
"prettier": "3.0.0",
"tape": "5.6.6",
"typescript": "5.1.6"
"tape": "5.8.1",
"typescript": "5.5.4"
},
"scripts": {
"build": "tsc --incremental -p tsconfig.json",
"lint": "npx eslint . --ext .ts",
"format": "npx prettier . --ignore-path ./.prettierignore --write && git status",
"flb": "npm run format && npm run lint && npm run build",
"fb": "npm run format && npm run build",
"start": "node dist/src/Main.js",
"test": "node dist/test/Main.test.js"
},
Expand Down
23 changes: 6 additions & 17 deletions src/Conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,32 +232,21 @@ export default class Conversion {
this._allLogsPath = path.join(this._logsDirPath, 'all.log');
this._errorLogsPath = path.join(this._logsDirPath, 'errors-only.log');
this._notCreatedViewsPath = path.join(this._logsDirPath, 'not_created_views');
this._excludeTables =
this._config.exclude_tables === undefined ? [] : this._config.exclude_tables;
this._includeTables =
this._config.include_tables === undefined ? [] : this._config.include_tables;
this._excludeTables = this._config.exclude_tables || [];
this._includeTables = this._config.include_tables || [];
this._timeBegin = new Date();
this._encoding = this._config.encoding === undefined ? 'utf8' : this._config.encoding;
this._encoding = this._config.encoding || 'utf8';
this._0777 = '0777';
this._mysqlVersion = '5.6.21'; // Simply a default value.
this._extraConfig =
this._config.extraConfig === undefined ? false : this._config.extraConfig;
this._extraConfig = this._config.extraConfig;
this._tablesToMigrate = [];
this._viewsToMigrate = [];
this._dataPool = [];
this._dicTables = new Map<string, Table>();
this._mySqlDbName = this._sourceConString.database;
this._streamsHighWaterMark = +(this._config.streams_high_water_mark || 16384);

this._streamsHighWaterMark =
this._config.streams_high_water_mark === undefined
? 16384
: +this._config.streams_high_water_mark;

this._schema =
this._config.schema === undefined || this._config.schema === ''
? this._mySqlDbName
: this._config.schema;

this._schema = this._config.schema || this._mySqlDbName;
const isValidMaxEachDbConnectionPoolSize: boolean =
this._config.max_each_db_connection_pool_size !== undefined &&
Conversion._isIntNumeric(this._config.max_each_db_connection_pool_size);
Expand Down
12 changes: 11 additions & 1 deletion src/DataChunksProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default async (
tableName,
true,
);

const logTitle = 'DataChunksProcessor::default';
const arrTableColumns = (conversion._dicTables.get(tableName) as Table).arrTableColumns;
const selectFieldList: string = arrangeColumnsData(
Expand Down Expand Up @@ -76,7 +77,16 @@ export default async (
_rowsCnt: rowsCnt,
_selectFieldList: selectFieldList,
_copyColumnNamesList: arrTableColumns
.map((column: any): string => `"${column.Field}"`)
.map((column: any): string => {
const columnName = extraConfigProcessor.getColumnName(
conversion,
originalTableName,
column.Field,
false,
);

return `"${columnName}"`;
})
.join(','),
});

Expand Down
26 changes: 14 additions & 12 deletions src/ExtraConfigProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,35 @@ export const getTableName = (

/**
* Retrieves current column's name.
*
* !!!Note, 're-namings' defined in extra-config under 'tables' -> 'columns'
* will take precedence over 'lowerCaseAllColumnNames: true'.
*/
export const getColumnName = (
conversion: Conversion,
originalTableName: string,
currentColumnName: string,
shouldGetOriginal: boolean,
): string => {
let retVal: string = currentColumnName;

if (conversion._extraConfig !== null) {
if (conversion._extraConfig) {
if ('tables' in conversion._extraConfig) {
for (let i = 0; i < conversion._extraConfig.tables.length; ++i) {
const isOriginal: boolean =
const tableFound: boolean =
conversion._extraConfig.tables[i].name.original === originalTableName &&
'columns' in conversion._extraConfig.tables[i];

if (isOriginal) {
if (tableFound) {
for (
let columnsCount = 0;
columnsCount < conversion._extraConfig.tables[i].columns.length;
++columnsCount
) {
if (
const columnFound: boolean =
conversion._extraConfig.tables[i].columns[columnsCount].original ===
currentColumnName
) {
retVal = shouldGetOriginal
currentColumnName;

if (columnFound) {
return shouldGetOriginal
? conversion._extraConfig.tables[i].columns[columnsCount].original
: conversion._extraConfig.tables[i].columns[columnsCount].new;
}
Expand All @@ -82,12 +84,12 @@ export const getColumnName = (
}
}

if (conversion._extraConfig.lowerCaseAllColumnNames && !shouldGetOriginal) {
retVal = retVal.toLowerCase();
if (conversion._extraConfig.lowerCaseAllColumnNames) {
return currentColumnName.toLowerCase();
}
}

return retVal;
return currentColumnName;
};

/**
Expand Down

0 comments on commit 06417d1

Please sign in to comment.