Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config file and type shorthands #91

Merged
merged 2 commits into from
Jun 20, 2017
Merged

Config file and type shorthands #91

merged 2 commits into from
Jun 20, 2017

Conversation

dolezel
Copy link
Contributor

@dolezel dolezel commented Jun 16, 2017

Fixes #87

Copy link

@roman-kaspar roman-kaspar left a comment

Choose a reason for hiding this comment

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

small details to fix in the comments.
otherwise LGTM.

README.md Outdated
@@ -76,11 +79,26 @@ You can adjust defaults by passing arguments to `pg-migrate`:

See all by running `pg-migrate --help`.

Most of configuration options can be also specified in `node-config` configuration file.
Most of configuration options can be also specified in [config](https://www.npmjs.com/package/config) configuration file.

Choose a reason for hiding this comment

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

config configuration file?
---> I would omit the middle "configuration" word here.


* `migrations-dir`, `migrations-schema`, `migrations-table`, `check-order` - same as above

* either `url` or [`user`], [`password`], `host` (defaults to localhost), `port` (defaults to 5432), `name` - for connection details

Choose a reason for hiding this comment

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

why is user and password in square brackets here?

should it read:
either url, or [user, password, host (...), port, name] - for connection details
?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It means that user and password are optional (you can have user without password and if you do not specify user, logged in user is used)

Choose a reason for hiding this comment

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

aha, OK :-)


* `type-shorthands` - for column type shorthands

You can specify custom types which will be expanded to column definition (e.g. for `module.exports = { "type-shorthands": { id: { type: 'uuid', primaryKey: true }, createdAt: { type: 'timestamp', notNull: true, default: new require('node-pg-migrate').PgLiteral('current_timestamp') } } }`

Choose a reason for hiding this comment

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

the bracket before "e.g." is not closed here.

Choose a reason for hiding this comment

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

Can someone provide a real example of how to use this feature? I have been trying in different ways without any success.

Thank you

README.md Outdated
* `type-shorthands` - for column type shorthands

You can specify custom types which will be expanded to column definition (e.g. for `module.exports = { "type-shorthands": { id: { type: 'uuid', primaryKey: true }, createdAt: { type: 'timestamp', notNull: true, default: new require('node-pg-migrate').PgLiteral('current_timestamp') } } }`
will in `pgm.createTable('test', { id: 'id', createdAt: 'createdAt' });` produce SQL `CREATE TABLE "test" ("id" uuid PRIMARY KEY, "createdAt" timestamp DEFAULT current_timestamp NOT NULL);`.

Choose a reason for hiding this comment

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

here I would say "it will in ..." instead of just "will in".

@dolezel dolezel merged commit d384e65 into master Jun 20, 2017
@dolezel dolezel deleted the config branch June 20, 2017 07:29
@dolezel
Copy link
Contributor Author

dolezel commented Oct 2, 2017

@mzuniga84
This is a working custom config file configuration with type shorthands:

const PgLiteral = require('node-pg-migrate').PgLiteral;
module.exports = {
  "type-shorthands": {
    id: {
      type: 'uuid',
      primaryKey: true
    },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: new PgLiteral('current_timestamp')
    }
  }
};

Run it as pg-migrate -f ./config.js

@flux627
Copy link
Contributor

flux627 commented Feb 2, 2018

Would there be any way to "version" this shorthand file in tandem with migrations? As in, I want to record in my migrations when these shorthands change, as changing this file necessarily breaks all migrations dependent on it.

@dolezel
Copy link
Contributor Author

dolezel commented Feb 5, 2018

That's true :(
Thinking about it, I think that it can be version in a way, that if type-shorthands contains numeric index it is treated as timestamp and for migrations before this timestamp this configuration is taken.
E.g.

const PgLiteral = require('node-pg-migrate').PgLiteral;
module.exports = {
  "type-shorthands": {
    1517827323: {
      id: {
        type: 'uuid',
        primaryKey: true
      },
      createdAt: {
        type: 'timestamp',
        notNull: true,
      }
    },
    id: {
      type: 'uuid',
      primaryKey: true
    },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: new PgLiteral('current_timestamp')
    }
  }
};

What do you think @flux627 ?

@flux627
Copy link
Contributor

flux627 commented Feb 5, 2018

I would prefer a multi-file solution to both match the migrations pattern and keep things clear.

Maybe if you specify a directory instead of a file, it can parse a timestamp at the beginning of the string and use the most recent definitions for a given migration timestamp.

At that point though, I think it would make much more sense to define these shorthands within the migration files themselves. Something like:

exports.shorthands = {
  // Shorthands go here
}

exports.up = function up(pgm) {
  ...
}
exports.down = function down(pgm) {
  ...
}

It then would share these definitions for subsequent migrations, unless they are updated. If this was implemented I would be a very happy camper :)

@flux627
Copy link
Contributor

flux627 commented Feb 5, 2018

With this, if you wanted to, you could even have migrations files dedicated to shorthand updates, which would arguably be much cleaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants