Package: tusker
TuskerA PostgreSQL specific migration toolElevator pitchDo you want to write your database schema directly as SQL
which is understood by PostgreSQL?Do you want to be able to make changes to this schema and
generate the SQL which is required to migrate between the
old and new schema version?Tusker does exactly this.InstallationpipxinstalltuskerNow you should be able to run tusker. Give it a try:tusker--helpGetting startedOnce tusker is installed create a new file calledschema.sql:CREATETABLEfruit(idBIGINTGENERATEDBYDEFAULTASIDENTITY,nameTEXTNOTNULLUNIQUE);You probably want to create an emptymigrationsdirectory, too:mkdirmigrationsNow you should be able to create your first migration:tusker diffThe migration is printed to the console and all you need to do is
copy and paste the output into a new file in the migrations directory.
Alternatively you can also pipe the output oftusker diffinto the
target file:tusker diff > migrations/0001_initial.sqlAfter that check that yourschema.sqland yourmigrationsare in sync:tusker diffThis should give you an empty output. This means that there is no difference
between applying the migrations in order and the target schema.Alternatively you can run the check command:tusker checkIf you want to change the schema in the future simply change theschema.sqland runtusker diffto create the migration for you.Give it a try and change theschema.sql:CREATETABLEfruit(idBIGINTGENERATEDBYDEFAULTASIDENTITY,nameTEXTNOTNULLUNIQUE,colorTEXTNOTNULLDEFAULT'');Create a new migration:tusker diff > migrations/0002_fruit_color.sqlCongratulations! You are now using SQL to write your migrations. You are no longer limited by a 3rd party data definition language or an object relational wrapper.ConfigurationIn order to run tusker you do not need a configuration file. The following
defaults are assumed:The file containing your database schema is calledschema.sqlThe directory containing the migrations is calledmigrationsYour current user can connect to the database using a unix
domain socket without a password.You can also create a configuration file calledtusker.toml. The default
configuration looks like that:[schema]filename="schema.sql"[migrations]filename="migrations/*.sql"[database]#host = ""#port = 5432#user = ""#password = ""dbname="tusker"[migra]safe=falseprivileges=falseInstead of the exploded form ofhost,port, etc. it
is also possible to pass a connection URL:[schema]filename="schema.sql"[migrations]filename="migrations/*.sql"[database]url="postgresql:///my_awesome_db"You can also use an environment variable in place of a hard-coded value:[database]url="${DATABASE_URL}"How can I use the generated SQL files?The resulting SQL files can either be applied to the database by hand
or by using one of the many great tools and libraries which support
applying SQL files in order.Some recommendations are:NodeJS:marvRust:refineryHow does it work?Upon startuptuskerreads all files from themigrationsdirectory
and runs them on an empty database. Another empty database is created
and the target schema is created. Then those two schemas are
diffed using the excellentmigratool and the output printed to the console.Tusker isunsafeby defaultUnlikemigrathetuskercommand by default does not throw an
exception when adrop-statement is generated. Always check your
generated migrations prior to running them. If you want the same
behavior as migra you can either use the--safeargument or set
themigra.safeconfiguration option toTruein yourtusker.tomlfile.FAQIs it possible to split the schema into multiple files?Yes. This feature has been added in 0.3. You can now useglobpatterns as
part of theschema.filenamesetting. e.g.:[schema]filename="schema/*.sql"As of 0.4.5 recursive glob patterns are supported as well:[schema]filename="schema/**/*.sql"Is it possible to diff the schema and/or migrations against an existing database?Yes. This feature has been added in 0.2. You can pass afromandtoargument to thetusker diffcommand. Check the output oftusker diff --helpfor
more details.Tusker printed an error and left the temporary databases behind. How can I remove them?Runtusker clean. This will remove all databases which were created
by previous runs of tusker. Tusker only removes databases which are
marked with aCREATED BY TUSKERcomment.What does thedbnamesetting intusker.tomlmean?When diffing against a ready migrated database this database name is used. This
command will print out the difference between the current database schema and
the target schema:tuskerdiffdatabaseTusker also needs to create temporary databases when diffing against theschemaand/ormigrations. The two databases are called{dbname}_{timestamp}_schemaand{dbname}_{timestamp}_migrations.