npm db-migrate SQLite3 Windows Instructions don

2019-09-03 05:52发布

问题:

I don't know where to put the database, because the instructions are for linux/mac. I don't know where to run the migration file. The instructions are just not good enough for a beginner. I am so confused. I was unsuccessful at creating a database that I could connect to. Later, in the instructions, the tutorial walks you through creating the database using db-migration. Yay, I thought that might help. Nope. I just can't create a SQLite database that I can connect to using NodeJS.

This is what I have so far:

database.json

{
    "test": {
        "driver": "sqlite3",
        "filename": "/data/keyword-wrangler2.test.sqlite"
    },

    "dev": {
        "driver": "sqlite3",
        "filename": "/data/keyword-wrangler2.dev.sqlite"
    },

    "production": {
        "driver": "mysql",
        "host": "localhost",
        "user": "root",
        "password": "root",
        "database": "keyword_wrangler"
    }

}

The instructions have the filename = "/var/tmp/keyword-wrangler.sqlite". We don't have a /var folder on Windows. Should I create one or can I put the database where I want it?

20160304212317-createKeywordAndCategoryTable.js

Then I ran this command: db-migrate create createKeywordAndCategoryTable --env test and made the changes according to the instructions. Following is the migration file:

'use strict';

var dbm = require('db-migrate');
var type = dbm.dataType;
var async = require('async');
var seed;

/**
  * 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;
};

exports.up = function(db, callback) {
    async.series(
        [
            db.createTable.bind(db, 'keyword', {
                id: { type: 'int', primaryKey: true, autoIncrement: true, notNull: true },
                value: { type: 'string', length: '128', notNull: true, unique: true },
                categoryID: { type: 'int', notNull: true }
            }),

            db.createTable.bind(db, 'category', {
                id: { type: 'int', primaryKey: true, autoIncrement: true, notNull: true },
                name: { type: 'string', length: '128', notNull: true }
            })
        ], callback);
};

exports.down = function(db, callback) {
    async.series(
        [
            db.dropTable.bind(db, 'keyword'),
            db.dropTable.bind(db, 'category')
        ], callback);
};

According to the instructions I should be able to run this command: db-migrate up --env test and the file should be processed and the database should exist.

From where? If I run the command from the root folder of my project, I receive this error:

db-migrate up --env test
[ERROR] Error: SQLITE_CANTOPEN: unable to open database file
    at Error (native)

If I run the command from the migrations folder of my project, I receive this error:

\migrations>db-migrate up --env test
[ERROR] Error: Could not find database config file 'C:\Users\pdl\Projects\keyword-wrangler\keyword-wrangler3\migrations/database.json'
    at Object.exports.loadFile (C:\Users\pdl\Projects\keyword-wrangler\keyword-wrangler3\node_modules\db-migrate\lib\config.js:45:11)
    at loadConfig (C:\Users\pdl\Projects\keyword-wrangler\keyword-wrangler3\node_modules\db-migrate\api.js:508:18)
    at Object.dbmigrate (C:\Users\pdl\Projects\keyword-wrangler\keyword-wrangler3\node_modules\db-migrate\api.js:67:17)
    at Object.module.exports.getInstance (C:\Users\pdl\Projects\keyword-wrangler\keyword-wrangler3\node_modules\db-migrate\index.js:56:10)
    at C:\Users\pdl\AppData\Roaming\npm\node_modules\db-migrate\bin\db-migrate:34:23
    at C:\Users\pdl\AppData\Roaming\npm\node_modules\db-migrate\node_modules\resolve\lib\async.js:44:21
    at ondir (C:\Users\pdl\AppData\Roaming\npm\node_modules\db-migrate\node_modules\resolve\lib\async.js:187:31)
    at onex (C:\Users\pdl\AppData\Roaming\npm\node_modules\db-migrate\node_modules\resolve\lib\async.js:93:22)
    at C:\Users\pdl\AppData\Roaming\npm\node_modules\db-migrate\node_modules\resolve\lib\async.js:24:18
    at FSReqWrap.oncomplete (fs.js:82:15)

Can somebody please clue me in on the obvious steps or information I am missing?

Totally Frustrated!

回答1:

It is a path problem--one of the many issues with using Xnix-centered tools on Windows. (I truly sympathize with you; I recently switched to a Mac, and all these problems instantly went away.) You might want to consider using Cygwin to work around this class of issues if you don't have access to a Mac or Linux machine.

Your db-migrate error message starts out with:

Could not find database config file 'C:\Users\pdl\Projects\keyword-wrangler\keyword-wrangler3\migrations/database.json'

Looking at your GitHub project, I see that your database.json file is at the project root, not in your migrations folder. I don't have a Windows machine handy to test this myself, but you might try either moving that database.json file into your migrations folder, or run the migration from your project root and specify the path to migration file via the db-migrate docs.

I hope this helps.



回答2:

Just change your path from

/data/keyword-wrangler2.dev.sqlite

to

./data/keyword-wrangler2.dev.sqlite

and it will work. Do this change for both paths.



回答3:

This package gave me a lot of trouble so I thought I would add my answer here in case anyone else ran into some of the same issues. For the first issue, you can store your database anywhere you want. I store the database within the scope of the project and add it to my .gitignore.

  "dev": {
      "driver": "sqlite3",
      "filename": "db/dev.db"
   },

Before running db:migrate, make sure that you create the file specified in the "filename" property. db-migrate will not create that file for you.

For the main issue of where you're supposed to be running these commands. Taking a look at the repo they use process.cwd() as the basePath. So the path set for your "database.json" should be relative to where you're invoking db-migrate.

For example, I have the following folder structure.

├── package.json
├── config
│   └── database.json
└── db
    ├── dev.db
    └── migrations

To be able to run my migrations I added a script to "scripts" in my package.json.

"db:rollback": "db-migrate --migrations-dir db/migrations --config config/database.json down --env dev",

If you do not specify --migrations-dir and --config it will look for those files within the same directory that db-migrate was invoked.

Here's a snippet of the default values used from the source.

    .default({
      verbose: false,
      table: 'migrations',
      'seeds-table': 'seeds',
      'force-exit': false,
      'sql-file': false,
      'non-transactional': false,
      config: internals.configFile || internals.cwd + '/database.json', //cwd -- current working directory
      'migrations-dir': internals.cwd + '/migrations',
      'vcseeder-dir': internals.cwd + '/VCSeeder',
      'staticseeder-dir': internals.cwd + '/Seeder',
      'ignore-completed-migrations': false
    })

https://github.com/db-migrate/node-db-migrate/blob/master/lib/commands/set-default-argv.js