Skip to content

Postgres examples #732

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

peterbecich
Copy link
Contributor

@peterbecich peterbecich commented Dec 14, 2024

This PR adds two executables to the Postgres Pagila example.

  1. cabal run prints the migration to the screen to help understanding.
$ cabal run
Migration steps:
Initial commit
Add film actor, inventory, rental table
-------------
For each migration step, the sequence of SQL scripts:
Initial commit
SQL command type: PgCommandTypeDdl
SQL script: 
CREATE TABLE "actor" ("actor_id" SERIAL, "first_name" VARCHAR(45) NOT NULL, "last_name" VARCHAR(45) NOT NULL UNIQUE, "last_update" TIMESTAMP DEFAULT NOW() NOT NULL, PRIMARY KEY("actor_id")) 
SQL command type: PgCommandTypeDdl
SQL script: 
CREATE TABLE "address" ("address_id" SMALLSERIAL, "address" VARCHAR(50) NOT NULL, "address2" VARCHAR(50), "district" VARCHAR(20) NOT NULL, "city_id" SMALLINT NOT NULL, "postal_code" VARCHAR(10), "phone" VARCHAR(20) NOT NULL, "last_update" TIMESTAMP DEFAULT NOW() NOT NULL, PRIMARY KEY("address_id")) 
SQL command type: PgCommandTypeDdl
SQL script: 
CREATE TABLE "city" ("city_id" SMALLINT, "city" VARCHAR(50) NOT NULL, "country_id" SMALLINT NOT NULL, "last_update" TIMESTAMP DEFAULT NOW() NOT NULL, PRIMARY KEY("city_id")) 
SQL command type: PgCommandTypeDdl
SQL script: 
CREATE TABLE "country" ("country_id" SMALLINT, "country" VARCHAR(50) NOT NULL, "last_update" TIMESTAMP DEFAULT NOW() NOT NULL, PRIMARY KEY("country_id")) 
.
.
.
.
  1. cabal run pagila-migration
    This applies the migration to a Postgres instance. It will overwrite the data in this Postgres instance. This is documented: https://github.com/haskell-beam/beam/blob/867fc8ae1ebbcc3b5832300a7492516902af3206/beam-postgres/examples/readme.md#pagila-example

The migration example will generate countries and staff and insert them into the database after migration V0001. Then V0002 migration is applied and the staff are migrated to V0002.NewStaff.

@peterbecich peterbecich force-pushed the postgres-example-executable branch from 01c842e to 867fc8a Compare December 23, 2024 08:48
@peterbecich peterbecich changed the title Postgres example executable Postgres examples Dec 23, 2024
@LaurentRDC LaurentRDC self-requested a review December 24, 2024 19:00
@peterbecich peterbecich force-pushed the postgres-example-executable branch 6 times, most recently from d2a78e4 to 4bd1773 Compare December 25, 2024 22:16
@peterbecich
Copy link
Contributor Author

@LaurentRDC , this PR makes the example schema usable. The user must provide the instance of Postgres to target.

It also generates random data to populate the database with rows. There is a safety check for the user before any change is applied to the database.

@peterbecich
Copy link
Contributor Author

@LaurentRDC please review, can I help in any way? Do you want the data generation removed?

putStrLn "-------------"
putStrLn "For each migration step, the sequence of SQL scripts:"
let
renderer :: BeamSqlBackendSyntax Postgres -> String
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned in the documentation for backendMigrationStepsScript, why not use displaySyntax, which has type Syntax -> String?

Recall that BeamSqlBackendSyntax Postgres is PgSyntax, and PgSyntax has an instance of Sql92DisplaySyntax such that displaySyntax will work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need more time to understand this, can it be moved to a follow-up issue?

Copy link
Member

Choose a reason for hiding this comment

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

Iinsist on using displaySyntax. We want to show the appropriate, canonical way to do things in this example.

@@ -0,0 +1,3 @@
# Pagila example

`cabal run` to see rendering of Postgres migration.
Copy link
Member

Choose a reason for hiding this comment

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

In the future, if there are more executables, cabal run won't be able to determine which executable is implied.

From the root of the repository, you refer users to run cabal run pagila instead of cabal run

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, it should be cabal run pagila-print. Fixed. There is no target for cabal run.

@LaurentRDC
Copy link
Member

Hi Peter, I started the review weeks ago, but apparently never pressed the "finish review" button. Sorry!

@peterbecich peterbecich force-pushed the postgres-example-executable branch from 4bd1773 to ba070df Compare February 23, 2025 07:14
@peterbecich
Copy link
Contributor Author

Thank you @LaurentRDC

-- | Given a function to convert a command to a 'String', produce a script that
-- will execute the given migration steps. Usually, the function you provide
-- eventually calls 'displaySyntax' to render the command.
backendMigrationStepsScript :: BeamSqlBackend be
Copy link
Member

Choose a reason for hiding this comment

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

Can you mention backendMigrationScript in the docstring for backendMigrationStepsScript, and vice-versa? This way, users can more easily navigate the documentation

Comment on lines +11 to +12
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think deriving strategies are used anymore in this changeset

@@ -1,4 +1,4 @@
**Note**: The code used in this guide is in `beam-postgres/examples/Pagila/Schema/CustomMigrateExample.hs`.
**Note**: The code used in this guide is in the `pagila.cabal` package, located in `beam-postgres/examples/`. `cabal run pagila` to see a rendering of the example schema.
Copy link
Member

Choose a reason for hiding this comment

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

I think you mean cabal run pagila-print to see a rendering of ....

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.

2 participants