db-migrate / node-db-migrate

Database migration framework for node
Other
2.32k stars 360 forks source link

relation "migrations" already exists #589

Closed Mojo90 closed 5 years ago

Mojo90 commented 5 years ago

I'm submitting a...

Current behavior

$ npm run db-migrate

> backend@0.0.0 db-migrate /Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend
> db-migrate up -e pg

received data: --
-- PostgreSQL database dump
--

-- Dumped from database version 10.5
-- Dumped by pg_dump version 10.5

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--

COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: containers; Type: TABLE; Schema: public; Owner: binando
--

CREATE TABLE public.containers (
    id text NOT NULL,
    "idGroup" text NOT NULL,
    "containerObj" jsonb NOT NULL
);

ALTER TABLE public.containers OWNER TO binando;

--
-- Name: locations; Type: TABLE; Schema: public; Owner: binando
--

CREATE TABLE public.locations (
    "idGroup" text NOT NULL,
    "startCords" jsonb,
    "stationCords" jsonb,
    "endCords" jsonb
);

ALTER TABLE public.locations OWNER TO binando;

--
-- Name: migrations; Type: TABLE; Schema: public; Owner: binando
--

CREATE TABLE public.migrations (
    id integer NOT NULL,
    name character varying(255) NOT NULL,
    run_on timestamp without time zone NOT NULL
);

ALTER TABLE public.migrations OWNER TO binando;

--
-- Name: migrations_id_seq; Type: SEQUENCE; Schema: public; Owner: binando
--

CREATE SEQUENCE public.migrations_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE public.migrations_id_seq OWNER TO binando;

--
-- Name: migrations_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: binando
--

ALTER SEQUENCE public.migrations_id_seq OWNED BY public.migrations.id;

--
-- Name: session; Type: TABLE; Schema: public; Owner: binando
--

CREATE TABLE public.session (
    sid character varying NOT NULL,
    sess json NOT NULL,
    expire timestamp(6) without time zone NOT NULL
);

ALTER TABLE public.session OWNER TO binando;

--
-- Name: timestamp; Type: TABLE; Schema: public; Owner: binando
--

CREATE TABLE public."timestamp" (
    "idGroup" text NOT NULL,
    "timestamp" timestamp without time zone
);

ALTER TABLE public."timestamp" OWNER TO binando;

--
-- Name: trucks; Type: TABLE; Schema: public; Owner: binando
--

CREATE TABLE public.trucks (
    id text NOT NULL,
    "truckObj" jsonb NOT NULL,
    "idGroup" text NOT NULL
);

ALTER TABLE public.trucks OWNER TO binando;

--
-- Name: migrations id; Type: DEFAULT; Schema: public; Owner: binando
--

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

--
-- Name: containers containers_id_key; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public.containers
    ADD CONSTRAINT containers_id_key UNIQUE (id);

--
-- Name: containers containers_pkey; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public.containers
    ADD CONSTRAINT containers_pkey PRIMARY KEY (id);

--
-- Name: locations locations_pkey; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public.locations
    ADD CONSTRAINT locations_pkey PRIMARY KEY ("idGroup");

--
-- Name: migrations migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public.migrations
    ADD CONSTRAINT migrations_pkey PRIMARY KEY (id);

--
-- Name: session session_pkey; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public.session
    ADD CONSTRAINT session_pkey PRIMARY KEY (sid);

--
-- Name: trucks trucks_id_key; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public.trucks
    ADD CONSTRAINT trucks_id_key UNIQUE (id);

--
-- Name: trucks trucks_pkey; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public.trucks
    ADD CONSTRAINT trucks_pkey PRIMARY KEY (id);

--
-- Name: timestamp untitled_table_pkey; Type: CONSTRAINT; Schema: public; Owner: binando
--

ALTER TABLE ONLY public."timestamp"
    ADD CONSTRAINT untitled_table_pkey PRIMARY KEY ("idGroup");

--
-- PostgreSQL database dump complete
--

[ERROR] AssertionError [ERR_ASSERTION]: ifError got unwanted exception: relation "migrations" already exists
    at /Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/db-migrate/lib/commands/on-complete.js:15:14
    at tryCatcher (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/util.js:16:23)
    at Promise.successAdapter (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/nodeify.js:22:30)
    at Promise._settlePromise (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/promise.js:566:21)
    at Promise._settlePromiseCtx (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/promise.js:606:10)
    at _drainQueueStep (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/async.js:142:12)
    at _drainQueue (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:693:18)
    at tryOnImmediate (timers.js:664:5)
    at processImmediate (timers.js:646:5)
    at Connection.parseE (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/pg/lib/connection.js:545:11)
    at Connection.parseMessage (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/pg/lib/connection.js:370:19)
    at Socket.<anonymous> (/Users/mo/Documents/Workspace/binando/binando-routing-simulation/backend/node_modules/pg/lib/connection.js:113:22)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onread (net.js:639:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! backend@0.0.0 db-migrate: `db-migrate up -e pg`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the backend@0.0.0 db-migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/mo/.npm/_logs/2018-10-05T19_34_07_930Z-debug.log

My database.json:

{
  "pg": {
    "driver": "pg",
    "user": "binando",
    "password": "mo",
    "host": "127.0.0.1",
    "database": "simulation_test",
    "schema": "public"
  },
  "sql-file" : true
}

And the database is also just created so no input there. The migration.js:

'use strict';

var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;

/**
  * We receive the dbmigrate dependency from dbmigrate initially.
  * This enables us to not have to rely on NODE_PATH.
  */
exports.setup = function(options, seedLink) {
  dbm = options.dbmigrate;
  type = dbm.dataType;
  seed = seedLink;
  Promise = options.Promise;
};

exports.up = function(db) {
  var filePath = path.join(__dirname, 'sqls', '20181005181501-initial-db-up.sql');
  return new Promise( function( resolve, reject ) {
    fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
      if (err) return reject(err);
      console.log('received data: ' + data);

      resolve(data);
    });
  })
  .then(function(data) {
    return db.runSql(data);
  });
};

exports.down = function(db) {
  var filePath = path.join(__dirname, 'sqls', '20181005181501-initial-db-down.sql');
  return new Promise( function( resolve, reject ) {
    fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
      if (err) return reject(err);
      console.log('received data: ' + data);

      resolve(data);
    });
  })
  .then(function(data) {
    return db.runSql(data);
  });
};

exports._meta = {
  "version": 1
};

Expected behavior

successful migration

Minimal reproduction of the problem with instructions

see above

What is the motivation / use case for changing the behavior?

Environment


db-migrate version: 0.11.3
plugins with versions: 
db-migrate driver with versions: `"db-migrate-pg": "^0.4.0"`

Additional information:
- Node version: v10.8.0
- Platform:  Mac

Others:

Postgres Version: 10.5
stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Mojo90 commented 5 years ago

Up

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Mojo90 commented 5 years ago

Up

wzrdtales commented 5 years ago

@Mojo90 The failure is on your side. Look through your sql dump. This should NEVER include the migrations table. This is ok as long as you don't restore your dump via db-migrate, but in this case you try to manipulate the migrations table from a migration itself. This will of course fail. Just remove this portion and it should work.

Mojo90 commented 5 years ago

Ah ok I understand. Thanks for the hint!