-
Notifications
You must be signed in to change notification settings - Fork 13
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
Sequence migration: nextval('new_seq') = nextval('old_seq') #10
Comments
I have now had a chance to apply the suggested migration to the database in question. The result is that the next insert in the
Which I guess is caused by the last The expected error appears if I remove the
|
It appears that the existing sequences aren't found due to this piece of code:
The existing sequences use a different naming schema (with a single underscore as separator -- e.g. Could the solution perhaps be to change the SELECT column_default, table_name, column_name
FROM information_schema.columns
WHERE column_default LIKE 'nextval(''_%''::regclass)' and then, inside This would require something akin to the following changes for sequencesQ :: Pg.Query
sequencesQ = fromString
"SELECT column_default, table_name, column_name FROM information_schema.columns WHERE column_default LIKE 'nextval(''%''::regclass)'" getSequence ::
Sequences ->
(Text, Text, Text) ->
IO Sequences
getSequence allSeqs (columnDefault, tName, cName) =
case T.stripPrefix "nextval('" columnDefault >>= T.stripSuffix "'::regclass)" of
Just seqName ->
pure $ M.insert (SequenceName seqName) (Sequence (TableName tName) (ColumnName cName)) allSeqs
Nothing -> pure allSeqs |
Alternatively, the string matching could be done entirely in postgres using the following query. SELECT c.relname, i.table_name, i.column_name
FROM information_schema.columns i
JOIN pg_class c ON EXISTS (
SELECT *
FROM regexp_matches(i.column_default, 'nextval\(''([_a-z][a-z0-9_]*)''::regclass\)') x(y)
WHERE y[1] = c.relname
AND c.relkind = 'S'
); which has the advantage that we make sure the given sequence (extracted from |
I have a table that looks as follows:
The current schema looks like this:
When I let
beam-automigrate
manage the schema, it suggests the following migrations:So, the migration:
id
column fromINT
toNUMERIC(10)
id
(runs_id_seq
) with a new sequence (runs___id___seq
)I have not executed the migration yet, but as far as I can see this will not work because
nextval
for the new sequence will not return the same asnextval
for the old sequence -- thus causing a primary key conflict on the next insert intoruns
with a default value forid
.Secondly, while grepping for
nextval
in the code base, I came across:beam-automigrate/src/Database/Beam/AutoMigrate/Generic.hs
Lines 312 to 313 in a1c3dde
The text was updated successfully, but these errors were encountered: