@aceitadev/adatabase 0.5.5 → 0.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,13 +29,13 @@ class SchemaManager {
29
29
  const table = (0, Table_1.getTableName)(model);
30
30
  if (!table)
31
31
  continue;
32
- const { columns, indexes } = this.getSchemaFromModel(model);
32
+ const { columns, indexes, primaryKey } = this.getSchemaFromModel(model);
33
33
  const existing = yield this.getExistingColumns(table);
34
34
  if (Object.keys(existing).length === 0) {
35
35
  yield this.createTable(table, columns, indexes);
36
36
  }
37
37
  else {
38
- yield this.updateTable(table, columns, indexes, existing);
38
+ yield this.updateTable(table, columns, indexes, existing, primaryKey);
39
39
  }
40
40
  }
41
41
  this.printMigrationSummary();
@@ -58,7 +58,7 @@ class SchemaManager {
58
58
  const isLastChange = changeIndex === changes.length - 1;
59
59
  const changeLinePrefix = isLastTable ? " " : "│ ";
60
60
  const changeConnector = isLastChange ? "└─" : "├─";
61
- console.log(` ${changeLinePrefix}${changeConnector} ${change}`);
61
+ console.log(`${changeLinePrefix} ${changeConnector} ${change}`);
62
62
  });
63
63
  });
64
64
  }
@@ -95,12 +95,16 @@ class SchemaManager {
95
95
  }
96
96
  });
97
97
  }
98
- updateTable(table, desired, indexes, existing) {
98
+ updateTable(table, desired, indexes, existing, primaryKey) {
99
99
  return __awaiter(this, void 0, void 0, function* () {
100
100
  const tableChanges = [];
101
101
  const conn = yield (0, Database_1.getConnection)();
102
102
  try {
103
103
  for (const [col, type] of Object.entries(desired)) {
104
+ // Se a coluna for a chave primária, pule a verificação para evitar o erro.
105
+ if (col === primaryKey) {
106
+ continue;
107
+ }
104
108
  if (!existing.hasOwnProperty(col)) {
105
109
  const sql = `ALTER TABLE \`${table}\` ADD COLUMN \`${col}\` ${type};`;
106
110
  yield (0, Database_1.execute)(sql, [], conn);
@@ -109,10 +113,9 @@ class SchemaManager {
109
113
  else {
110
114
  const normalize = (t) => t.toLowerCase().replace(/\s/g, '').replace('character varying', 'varchar');
111
115
  const existingType = normalize(existing[col]);
116
+ // Compara apenas o tipo base, ignorando extras como NOT NULL
112
117
  const desiredType = normalize(type.split(' ')[0]);
113
118
  if (existingType !== desiredType) {
114
- // NOTE: Type modification is database-specific and can be risky.
115
- // This implementation assumes MySQL's MODIFY COLUMN syntax.
116
119
  const sql = `ALTER TABLE \`${table}\` MODIFY COLUMN \`${col}\` ${type};`;
117
120
  yield (0, Database_1.execute)(sql, [], conn);
118
121
  tableChanges.push(`~ ${col} (tipo alterado: ${existing[col].toUpperCase()} → ${type.split(' ')[0].toUpperCase()})`);
@@ -131,11 +134,12 @@ class SchemaManager {
131
134
  getSchemaFromModel(model) {
132
135
  const columns = {};
133
136
  const indexes = [];
137
+ let primaryKey = null;
134
138
  const colMeta = (0, Column_1.getColumnMeta)(model);
135
139
  const nullableMeta = (0, Nullable_1.getNullableMeta)(model);
136
140
  const adapter = (0, Database_1.getAdapter)();
137
141
  if (!colMeta) {
138
- return { columns, indexes };
142
+ return { columns, indexes, primaryKey };
139
143
  }
140
144
  for (const [prop, opts] of colMeta.entries()) {
141
145
  const colName = (opts === null || opts === void 0 ? void 0 : opts.name) ? opts.name : camelToSnake(prop);
@@ -143,6 +147,7 @@ class SchemaManager {
143
147
  indexes.push(colName);
144
148
  }
145
149
  if (opts === null || opts === void 0 ? void 0 : opts.id) {
150
+ primaryKey = colName;
146
151
  const type = opts.type || Number;
147
152
  if (adapter.type === 'postgres') {
148
153
  columns[colName] = "SERIAL PRIMARY KEY";
@@ -185,7 +190,7 @@ class SchemaManager {
185
190
  sqlType += " UNIQUE";
186
191
  columns[colName] = sqlType;
187
192
  }
188
- return { columns, indexes };
193
+ return { columns, indexes, primaryKey };
189
194
  }
190
195
  getSqlTypeForClass(type) {
191
196
  const adapterType = (0, Database_1.getAdapter)().type;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aceitadev/adatabase",
3
- "version": "0.5.5",
3
+ "version": "0.5.7",
4
4
  "description": "Uma biblioteca para facilitar a interação com bancos de dados MySQL e PostgreSQL em projetos TypeScript/Node.js.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",