Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/controllers/locais-coleta-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ export const cadastrarFaseSucessional = (request, response, next) => {
throw new BadRequestExeption(306);
}
})
.then(() => FaseSucessional.max('numero', { transaction }))
.then(maxNumero => {
const proximoNumero = (Number(maxNumero) || 0) + 1;
return FaseSucessional.create({ numero: proximoNumero, nome }, transaction);
});
.then(() => FaseSucessional.create({ nome }, { transaction }));

sequelize.transaction(callback)
.then(faseCriada => {
Expand All @@ -196,7 +192,7 @@ export const buscarFasesSucessionais = (request, response, next) => {

Promise.resolve()
.then(() => FaseSucessional.findAndCountAll({
attributes: ['numero', 'nome'],
attributes: ['id', 'nome'],
where,
order: [['nome', 'ASC']],
}))
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/pendencias-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ export const aprovarPendencia = async (alteracao, hcf, transaction) => {
if (alteracao.fase_sucessional_id !== undefined) {
if (alteracao.fase_sucessional_id !== null) {
const faseSucessional = await FaseSucessional.findOne({
where: { numero: alteracao.fase_sucessional_id },
where: { id: alteracao.fase_sucessional_id },
transaction,
raw: true,
nest: true,
Expand Down Expand Up @@ -1850,7 +1850,7 @@ export async function visualizar(request, response, next) {
}

if (objetoAlterado.fase_sucessional_id !== undefined) {
parametros.faseSucessional = await FaseSucessional.findOne({ where: { numero: objetoAlterado.fase_sucessional_id }, raw: true, nest: true });
parametros.faseSucessional = await FaseSucessional.findOne({ where: { id: objetoAlterado.fase_sucessional_id }, raw: true, nest: true });
}

if (objetoAlterado.vegetacao_id !== undefined) {
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const cadastro = (request, response, next) => {
if (paisagem && paisagem.fase_sucessional_id) {
return FaseSucessional.findOne({
where: {
numero: paisagem.fase_sucessional_id,
id: paisagem.fase_sucessional_id,
},
transaction,
});
Expand Down Expand Up @@ -950,7 +950,7 @@ export const getDadosCadTombo = (request, response, next) => {
retorno.vegetacoes = vegetacoes.rows;
})
.then(() => FaseSucessional.findAndCountAll({
attributes: ['numero', 'nome'],
attributes: ['id', 'nome'],
order: [['nome', 'ASC']],
transaction,
}))
Expand Down Expand Up @@ -1256,7 +1256,7 @@ export const obterTombo = async (request, response, next) => {
relevoInicial: tombo.relevo !== null ? tombo.relevo?.nome : '',
idVegetacaoInicial: tombo.vegetaco !== null ? tombo.vegetaco?.id : '',
vegetacaoInicial: tombo.vegetaco !== null ? tombo.vegetaco?.nome : '',
idFaseInicial: tombo.fase_sucessional !== null ? tombo.fase_sucessional?.numero : '',
idFaseInicial: tombo.fase_sucessional !== null ? tombo.fase_sucessional?.id : '',
faseInicial: tombo.fase_sucessional !== null ? tombo.fase_sucessional?.nome : '',
colecaoInicial: tombo.colecoes_anexa !== null ? tombo.colecoes_anexa?.tipo : '',
complementoInicial: tombo.localizacao !== null && tombo.localizacao !== undefined ? tombo.localizacao?.complemento : '',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Knex } from 'knex'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JosueModesto eu tentei rodar esta migração com um dump de prod e deu este erro

$ tsx --env-file=.env src/database/cli.ts migration:apply
Error applying migration 20260826120203_rename_fase_sucessional_numero_to_id {"length":104,"name":"error","severity":"ERROR","code":"42P16","file":"tablecmds.c","line":"14158","routine":"dropconstraint_internal"}
/home/edvaldo/Workspace/Herbario/hcf-api/node_modules/pg-protocol/src/parser.ts:394
      : new DatabaseError(messageValue, LATEINIT_LENGTH, name)
        ^


error: alter table "fase_sucessional" alter column "id" drop not null - column "id" is in a primary key
    at parseErrorMessage (/home/edvaldo/Workspace/Herbario/hcf-api/node_modules/pg-protocol/src/parser.ts:394:9)
    at Parser.handlePacket (/home/edvaldo/Workspace/Herbario/hcf-api/node_modules/pg-protocol/src/parser.ts:212:19)
    at Parser.parse (/home/edvaldo/Workspace/Herbario/hcf-api/node_modules/pg-protocol/src/parser.ts:105:30)
    at Socket.<anonymous> (/home/edvaldo/Workspace/Herbario/hcf-api/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (node:events:524:28)
    at addChunk (node:internal/streams/readable:561:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
    at Readable.push (node:internal/streams/readable:392:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:189:23) {
  length: 104,
  severity: 'ERROR',
  code: '42P16',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'tablecmds.c',
  line: '14158',
  routine: 'dropconstraint_internal'
}

Node.js v22.13.1
error Command failed with exit code 1.

abaixo está o arquivo que eu usei

dump-herbario_dev-202608282142.zip


export async function run(knex: Knex): Promise<void> {
await knex.schema.alterTable('fase_sucessional', table => {
table.renameColumn('numero', 'id')
})

await knex.raw(`
CREATE SEQUENCE IF NOT EXISTS fase_sucessional_id_seq;
`)

await knex.raw(`
SELECT setval(
'fase_sucessional_id_seq',
COALESCE((SELECT MAX(id) FROM fase_sucessional), 1),
true
)
`)

await knex.schema.alterTable('fase_sucessional', table => {
table.bigInteger('id')
.notNullable()
.defaultTo(knex.raw('nextval(\'fase_sucessional_id_seq\')'))
.alter()
})

await knex.raw(`
ALTER SEQUENCE fase_sucessional_id_seq
OWNED BY fase_sucessional.id
`)
}
2 changes: 1 addition & 1 deletion src/models/FaseSucessional.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function associate(/* modelos */) {
export default (Sequelize, DataTypes) => {

const attributes = {
numero: {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
Expand Down
38 changes: 34 additions & 4 deletions test/integration/setup/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ ALTER SEQUENCE public.familias_id_seq OWNED BY public.familias.id;
--

CREATE TABLE public.fase_sucessional (
numero bigint NOT NULL,
id bigint NOT NULL,
nome character varying(200) NOT NULL,
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
Expand All @@ -575,6 +575,28 @@ CREATE TABLE public.fase_sucessional (

--
-- TOC entry 244 (class 1259 OID 30988)
-- Name: fase_sucessional_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.fase_sucessional_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- TOC entry 5014 (class 0 OID 0)
-- Dependencies: 244
-- Name: fase_sucessional_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.fase_sucessional_id_seq OWNED BY public.fase_sucessional.id;


--
-- TOC entry 245 (class 1259 OID 30988)
-- Name: generos; Type: TABLE; Schema: public; Owner: -
--

Expand Down Expand Up @@ -1897,7 +1919,15 @@ ALTER TABLE ONLY public.familias
--

ALTER TABLE ONLY public.fase_sucessional
ADD CONSTRAINT idx_41101_primary PRIMARY KEY (numero);
ADD CONSTRAINT idx_41101_primary PRIMARY KEY (id);


--
-- TOC entry 4530 (class 2604 OID 32603)
-- Name: fase_sucessional id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.fase_sucessional ALTER COLUMN id SET DEFAULT nextval('public.fase_sucessional_id_seq'::regclass);


--
Expand Down Expand Up @@ -2548,7 +2578,7 @@ CREATE INDEX idx_41296_fk_variedades_genero ON public.variedades USING btree (ge
--

ALTER TABLE ONLY public.locais_coleta
ADD CONSTRAINT fk_99i0itontmoklfxmoo8armtnv FOREIGN KEY (fase_numero) REFERENCES public.fase_sucessional(numero) ON UPDATE RESTRICT ON DELETE RESTRICT;
ADD CONSTRAINT fk_99i0itontmoklfxmoo8armtnv FOREIGN KEY (fase_numero) REFERENCES public.fase_sucessional(id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
Expand Down Expand Up @@ -2665,7 +2695,7 @@ ALTER TABLE ONLY public.locais_coleta
--

ALTER TABLE ONLY public.locais_coleta
ADD CONSTRAINT fk_locais_coleta_fase_sucessional1 FOREIGN KEY (fase_sucessional_id) REFERENCES public.fase_sucessional(numero) ON UPDATE RESTRICT ON DELETE RESTRICT;
ADD CONSTRAINT fk_locais_coleta_fase_sucessional1 FOREIGN KEY (fase_sucessional_id) REFERENCES public.fase_sucessional(id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
Expand Down
Loading