dbsrgits / sql-translator

SQL::Translator (SQLFairy)
http://sqlfairy.sourceforge.net/
82 stars 90 forks source link

apply `is_auto_increment` for already existing field #107

Open KES777 opened 6 years ago

KES777 commented 6 years ago

When applying is_auto_increment for already existing field

--- a/lib/HyperMouse/Schema/Result/Language.pm
+++ b/lib/HyperMouse/Schema/Result/Language.pm
@@ -33,6 +33,7 @@ $Z->add_columns(
   },
   language_id => {
     data_type => "integer",
+    is_auto_increment => 1,
     extra => { unsigned => 1 },
   },
     known_from => {

the generated upgrade script is:

ALTER TABLE language ALTER COLUMN language_id TYPE serial;

which fails with error ERROR: type "serial" does not exist at:

$(which dbic-migration) --schema_class HyperMouse::Schema --database PostgreSQL -Ilib upgrade
Reading configurations from /home/kes/work/projects/tucha/monkeyman/share/fixtures/13/conf
failed to run SQL in /home/kes/work/projects/tucha/monkeyman/share/migrations/PostgreSQL/upgrade/13-14/001-auto.sql: DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator::try {...} (): DBI Exception: DBD::Pg::db do failed: ERROR:  type "serial" does not exist at inline delegation in DBIx::Class::DeploymentHandler for deploy_method->upgrade_single_step (attribute declared in /home/kes/work/projects/tucha/monkeyman/local/lib/perl5/DBIx/Class/DeploymentHandler/WithApplicatorDumple.pm at line 51) line 18
 (running line 'ALTER TABLE language ALTER COLUMN language_id TYPE serial') at /home/kes/work/projects/tucha/monkeyman/local/lib/perl5/DBIx/Class/DeploymentHandler/DeployMethod/SQL/Translator.pm line 248.
DBIx::Class::Storage::TxnScopeGuard::DESTROY(): A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back. at /home/kes/work/projects/tucha/monkeyman/local/bin/dbic-migration line 0
DBIx::Class::Storage::TxnScopeGuard::DESTROY(): A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back. at /home/kes/work/projects/tucha/monkeyman/local/bin/dbic-migration line 0
Makefile:132: recipe for target 'dbup' failed
make: *** [dbup] Error 255

For already existing field there should be few commands:

CREATE SEQUENCE foo_a_seq OWNED BY foo.a;
SELECT setval('foo_a_seq', coalesce(max(a), 0)) FROM foo;
ALTER TABLE foo ALTER COLUMN a SET DEFAULT nextval('foo_a_seq');