Skip to content

Instantly share code, notes, and snippets.

@berstend
Last active August 15, 2018 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berstend/7901092be57d1ab1f27539d70de750da to your computer and use it in GitHub Desktop.
Save berstend/7901092be57d1ab1f27539d70de750da to your computer and use it in GitHub Desktop.
Prisma GraphQL Introspection Automation (Extract Postgres schema to datamodel.graphql and deploy to prisma server)

Prisma is amazing but unfortunately in it's early days and I got tired of having to manually enter my database info each time I ran prisma introspect.

With a little help of autoexpect this task is now automated.

Hint: On macOS you might need to install expect from brew, as the macOS version doesn't include autoexpect.

Usage

Modify script.exp to use your Postgres credentials.

Run ./introspect.sh

#!/usr/bin/env bash
set timeout -1
FORCE_COLOR=0 ./script.exp
mv -f datamodel-*.graphql datamodel.graphql
prisma deploy
endpoint: http://localhost:4466
datamodel: datamodel.graphql
#!/usr/local/bin/expect -f
#
# This Expect script was generated by autoexpect on Wed Aug 15 12:07:16 2018
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
#
# 2) differing output - Some programs produce different output each time
# they run. The "date" command is an obvious example. Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer. If this causes a problem, delete these patterns or replace
# them with wildcards. An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt). The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don
set timeout -1
spawn prisma introspect
match_max 100000
expect -exact "\[?25l? What kind of database do you want to introspect? (Use arrow keys)\r
❯ PostgreSQL PostgreSQL database \[40D\[40C"
send -- "\r"
expect -exact "\[2K\[1A\[2K\[G? What kind of database do you want to introspect? PostgreSQL\[61D\[61C\r
\[?25h? Enter database host (localhost) \[34D\[34C"
send -- "localhost"
expect -exact "\[2K\[G? Enter database host (localhost) l\[35D\[35C\[2K\[G? Enter database host (localhost) lo\[36D\[36C\[2K\[G? Enter database host (localhost) loc\[37D\[37C\[2K\[G? Enter database host (localhost) loca\[38D\[38C\[2K\[G? Enter database host (localhost) local\[39D\[39C\[2K\[G? Enter database host (localhost) localh\[40D\[40C\[2K\[G? Enter database host (localhost) localho\[41D\[41C\[2K\[G? Enter database host (localhost) localhos\[42D\[42C\[2K\[G? Enter database host (localhost) localhost\[43D\[43C"
send -- "\r"
expect -exact "\[2K\[G? Enter database host localhost\[31D\[31C\r
? Enter database port (5432) \[29D\[29C"
send -- "5432"
expect -exact "\[2K\[G? Enter database port (5432) 5\[30D\[30C\[2K\[G? Enter database port (5432) 54\[31D\[31C\[2K\[G? Enter database port (5432) 543\[32D\[32C\[2K\[G? Enter database port (5432) 5432\[33D\[33C"
send -- "\r"
expect -exact "\[2K\[G? Enter database port 5432\[26D\[26C\r
? Enter database user \[22D\[22C"
send -- "typeormtest2"
expect -exact "\[2K\[G? Enter database user t\[23D\[23C\[2K\[G? Enter database user ty\[24D\[24C\[2K\[G? Enter database user typ\[25D\[25C\[2K\[G? Enter database user type\[26D\[26C\[2K\[G? Enter database user typeo\[27D\[27C\[2K\[G? Enter database user typeor\[28D\[28C\[2K\[G? Enter database user typeorm\[29D\[29C\[2K\[G? Enter database user typeormt\[30D\[30C\[2K\[G? Enter database user typeormte\[31D\[31C\[2K\[G? Enter database user typeormtes\[32D\[32C\[2K\[G? Enter database user typeormtest\[33D\[33C\[2K\[G? Enter database user typeormtest2\[34D\[34C"
send -- "\r"
expect -exact "\[2K\[G? Enter database user typeormtest2\[34D\[34C\r
? Enter database password \[26D\[26C"
send -- "password"
expect -exact "\[2K\[G? Enter database password p\[27D\[27C\[2K\[G? Enter database password pa\[28D\[28C\[2K\[G? Enter database password pas\[29D\[29C\[2K\[G? Enter database password pass\[30D\[30C\[2K\[G? Enter database password passw\[31D\[31C\[2K\[G? Enter database password passwo\[32D\[32C\[2K\[G? Enter database password passwor\[33D\[33C\[2K\[G? Enter database password password\[34D\[34C"
send -- "\r"
expect -exact "\[2K\[G? Enter database password password\[34D\[34C\r
? Enter name of existing database \[34D\[34C"
send -- "typeormtest2"
expect -exact "\[2K\[G? Enter name of existing database t\[35D\[35C\[2K\[G? Enter name of existing database ty\[36D\[36C\[2K\[G? Enter name of existing database typ\[37D\[37C\[2K\[G? Enter name of existing database type\[38D\[38C\[2K\[G? Enter name of existing database typeo\[39D\[39C\[2K\[G? Enter name of existing database typeor\[40D\[40C\[2K\[G? Enter name of existing database typeorm\[41D\[41C\[2K\[G? Enter name of existing database typeormt\[42D\[42C\[2K\[G? Enter name of existing database typeormte\[43D\[43C\[2K\[G? Enter name of existing database typeormtes\[44D\[44C\[2K\[G? Enter name of existing database typeormtest\[45D\[45C\[2K\[G? Enter name of existing database typeormtest2\[46D\[46C"
send -- "\r"
expect -exact "\[2K\[G? Enter name of existing database typeormtest2\[46D\[46C\r
? Use SSL? (Y/n) \[17D\[17C"
send -- "n"
expect -exact "\[2K\[G? Use SSL? (Y/n) n\[18D\[18C"
send -- "\r"
expect -exact "\[2K\[G? Use SSL? No\[13D\[13C\r
? Enter name of existing schema \[32D\[32C"
send -- "public"
expect -exact "\[2K\[G? Enter name of existing schema p\[33D\[33C\[2K\[G? Enter name of existing schema pu\[34D\[34C\[2K\[G? Enter name of existing schema pub\[35D\[35C\[2K\[G? Enter name of existing schema publ\[36D\[36C\[2K\[G? Enter name of existing schema publi\[37D\[37C\[2K\[G? Enter name of existing schema public\[38D\[38C"
send -- "\r"
expect eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment