-
Notifications
You must be signed in to change notification settings - Fork 301
Expose Structured Mock Migrations #1600
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
Expose Structured Mock Migrations #1600
Conversation
| @@ -28,13 +28,10 @@ module Database.Persist.Postgresql | |||
| ( withPostgresqlPool | |||
| , withPostgresqlPoolWithVersion | |||
| , withPostgresqlPoolWithConf | |||
|
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of these are due to Fourmolu auto-formatting.
| -- | ||
| -- @since 2.17.0.0 | ||
| data AlterDB | ||
| = AddTable EntityNameDB EntityIdDef [Column] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This constructor has changed to store table information instead of the raw text.
| idtxt = | ||
| case entityId of | ||
| EntityIdNaturalKey pdef -> | ||
| T.concat | ||
| [ " PRIMARY KEY (" | ||
| , T.intercalate "," $ map (escapeF . fieldDB) $ NEL.toList $ compositeFields pdef | ||
| , ")" | ||
| ] | ||
| EntityIdField field -> | ||
| let | ||
| defText = defaultAttribute $ fieldAttrs field | ||
| sType = fieldSqlType field | ||
| in | ||
| T.concat | ||
| [ escapeF $ fieldDB field | ||
| , maySerial sType defText | ||
| , " PRIMARY KEY UNIQUE" | ||
| , mayDefault defText | ||
| ] | ||
| rawText = | ||
| T.concat | ||
| -- Lower case e: see Database.Persist.Sql.Migration | ||
| [ "CREATe TABLE " -- DO NOT FIX THE CAPITALIZATION! | ||
| , escapeE name | ||
| , "(" | ||
| , idtxt | ||
| , if null nonIdCols then "" else "," | ||
| , T.intercalate "," $ map showColumn nonIdCols | ||
| , ")" | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We move the code that generates the AddTable migration text down here, but it's the same logic.
|
oof i should have just mass formatted everything in one commit instead of doing it incrementally. apologies |
parsonsmatt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Versions should be 2.17.1.0.
I have a few questions/refactors- if we're exposing things (even internally) it may be nice to make these refactors ahead of time. Nothing blocking though.
The more structured representations are very nice!
|
|
||
| -- | Indicates whether a Postgres Column is safe to drop. | ||
| -- | ||
| -- @since 2.17.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to go out in at least 2.17.1.0
Also, if we're exposing it, let's newtype it, or make it a separate datatype.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are done.
| = ChangeType Column SqlType Text | ||
| | IsNull Column | ||
| | NotNull Column | ||
| | Add' Column |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this AddColumn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to AddColumn
| | Drop Column SafeToRemove | ||
| | Default Column Text | ||
| | NoDefault Column | ||
| | Update' Column Text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would also like to see a more descriptive name on here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to UpdateNullToValue
| -- | ||
| -- @since 2.17.0.0 | ||
| data AlterTable | ||
| = AddUniqueConstraint ConstraintNameDB [FieldNameDB] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it valid to have [] here? Or can we do NonEmpty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed for AddReference - it's a little more annoying for AddUniqueConstraint. But I don't think it's valid to have it empty here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we can just do followup issue then
| -> EntityDef | ||
| -> IO (Either [Text] [AlterDB]) | ||
| mockMigrateStructured allDefs entity = do | ||
| case partitionEithers [] of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to modify this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why that was there, but fixed: #1600 (comment)
| mockMigrate allDefs _ entity = fmap (fmap $ map showAlterDb) $ do | ||
| case partitionEithers [] of | ||
| ([], old'') -> return $ Right $ migrationText False old'' | ||
| (errs, _) -> return $ Left errs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where the partitionEithers is coming from - not sure why it was doing this, but I guess I can clean it up.
|
Ran the tests, here's the end of the output: |
| -- | ||
| -- @since 2.17.0.0 | ||
| data AlterTable | ||
| = AddUniqueConstraint ConstraintNameDB [FieldNameDB] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, we can just do followup issue then
| :: [EntityDef] | ||
| -> EntityDef | ||
| -> IO (Either [Text] [AlterDB]) | ||
| mockMigrateStructured allDefs entity = return $ Right migrationText |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels odd to me - if we're always succeeding with Right, why not just do that? Same re IO. Is this suppose dto be doing more?
| "RESTRICT" -> | ||
| Just Restrict | ||
| _ -> | ||
| error $ "Unexpected value in parseCascade: " <> show txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feels like this should throwError instead of error
parsonsmatt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code all lgtm. Let's test in the work repo and ensure this does what we want before merging.
persistent/ChangeLog.md
Outdated
| # 2.17.1.0 | ||
|
|
||
| * [#1600](https://github.com/yesodweb/persistent/pull/1600) | ||
| * Add `migrateStructured` to `Database.Persist.Postgresql.Internal`. | ||
| This allows you to access a structured representation of the proposed migrations | ||
| for use in your application. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh shoot. All the changes in this PR are actually in persistent-postgresql package. So this CHANGELOG entry needs to be relocated into persistent-postgresql/CHANGELOG.md and the relevant versioning followed.
Context
We'd like to be able to generate a structured representation of the proposed Postgres migrations when mocking migrations, instead of only having access to the raw text of statements.
This PR adds
migrateStructuredtoDatabase.Persist.Postgresql.Internalto generateAlterDBdata, which the existing implementation then converts to text as before.It also modifies the structure of
AlterDB(AddTable)to store structured information about the table that we're generating, instead of the raw text of the DB statements.This PR does not change any semantic behavior of the existing system, and only moves code around into Internal.
I've run the Postgres test suite locally and it fully passed.
Checklist
Before submitting your PR, check that you've:
@sincedeclarations to the Haddockfourmoluon any changed files (restyledwill do this for you, soaccept the suggested changes if it makes them)
.editorconfigandfourmolu.yamlfiles for details)After submitting your PR:
(unreleased)on the Changelog