golang-migrate / migrate

Database migrations. CLI and Golang library.
Other
15.48k stars 1.41k forks source link

Support Default on Google Cloud Spanner #918

Open zaitera opened 1 year ago

zaitera commented 1 year ago

Is your feature request related to a problem? Please describe. We can't run migrations with DEFAULT (for example adding column and setting a default value for the existing record)

Describe the solution you'd like Being able to execute statements like the following: ALTER TABLE t1 ADD COLUMN c1 STRING(MAX) DEFAULT (''); Google cloud spanner already supports this syntax, and I've tried it on the cloud console

Describe alternatives you've considered Disabling CleanStatements makes it possible to execute that statement, but then we lose the option to have comments or multiple statements in the migration file

Additional context Add any other context or screenshots about the feature request here.

IDispose commented 1 year ago

This will be more useful if the following is also supported ALTER TABLE t1 ADD COLUMN c1 STRING(MAX) NOT NULL DEFAULT ('')

Currently this throws an error saying Cannot add non-null columns to an existing table

IDispose commented 9 months ago

For anyone looking for a workaround, this works.

ALTER TABLE t1 ADD COLUMN c1 STRING(MAX) DEFAULT ('');
ALTER TABLE t1 ALTER COLUMN c1 STRING(MAX) NOT NULL DEFAULT ('')

put those together and your table DDL will look like this

CREATE TABLE 
    t 1 (
        -- other columns
        c1 STRING(MAX) NOT NULL DEFAULT ('')
    )