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

Applying .sql files without tracking #248

Closed
timtonk opened this issue Apr 24, 2018 · 3 comments
Closed

Applying .sql files without tracking #248

timtonk opened this issue Apr 24, 2018 · 3 comments

Comments

@timtonk
Copy link

timtonk commented Apr 24, 2018

Hey.
First of all, thanks guys for this tool, it is extremely simple and helpful for me. Using it since v.2.

I have just noticed PR #246. This one could be useful for me, but I need one small feature on the top of it.
Background: I have a bunch of raw sql files with postgres functions inside. It doesn't make sense to store them in migrations as in case of changes I would need to copy-paste the most part of it to another file. It also doesn't allow me to use diff tool for change tracking. Storing them in files resolves most of these troubles except for one - it is implemented outside of pg-migrate project and requires psql on the machine where it is supposed to be run.
To be more precise, instead of incremental changes I set the actual state of the DB from the provided SQL files. I feel like this approach is extremely reasonable and handy in use for functions and policies.

Thus, I would like to have an option to apply all .sql files from a specified migration dir, but doesn't store any information about them in the migration table. It will make possible to run this process multiple time.

Cheers.

@dolezel
Copy link
Contributor

dolezel commented Apr 24, 2018

Hi, thanks for thanks :) and input but I do not think this belongs to migration library as it is completely bypassing it.
I do not know what you have in your sql files but wouldn't something like

  "scripts": {
    "migrateup": "node-pg-migrate up",
    "postmigrateup": "psql $DATABASE_URL < sql_files/*"
  }

in package.json do the stuff?

@timtonk
Copy link
Author

timtonk commented Apr 24, 2018

It would, but requires psql to be installed on the client machine and ignores pg-migrate settings.

@timtonk timtonk closed this as completed Apr 26, 2018
@dolezel
Copy link
Contributor

dolezel commented Apr 26, 2018

So something like

cat sql_files/* | node -e '
  let sql = "";
  process.stdin.on("data", data => { sql = `${sql}${data}` });
  process.stdin.on("end", () => {
    const client = new (require("pg").Client)({ connectionString: process.env.DATABASE_URL });
    client.connect()
      .then(() => client.query(sql))
      .catch(() => process.exit(1));
  });
'

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

No branches or pull requests

2 participants