NAME
DBIO::PostgreSQL::Diff::Sequence - Diff operations for PostgreSQL sequences
VERSION
version 0.900007
DESCRIPTION
Represents the sequence operations the diff has: CREATE SEQUENCE for a sequence the target has and the database lacks, and DROP SEQUENCE for a sequence no column owns that stands in a schema the diff drops. Instances are produced by "diff" and consumed by DBIO::PostgreSQL::Diff.
It is here for the columns that count from a sequence. A serial column is, in the catalog, a column with the default nextval('t_id_seq'::regclass) and a sequence the column owns, and the diff writes a column as the catalog has it: CREATE TABLE, ADD COLUMN and SET DEFAULT all name the sequence, and PostgreSQL takes none of them before the sequence is there (relation "t_id_seq" does not exist). So the sequence is created first, ahead of every table and column ("ORDER OF OPERATIONS" in DBIO::PostgreSQL::Diff), and tied to its column once that is there (DBIO::PostgreSQL::Diff::SequenceOwner) -- three statements where the install script has the one word serial:
CREATE SEQUENCE public.t_id_seq AS integer INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1 NO CYCLE;
CREATE TABLE public.t (id integer NOT NULL DEFAULT nextval('t_id_seq'::regclass));
ALTER SEQUENCE public.t_id_seq OWNED BY public.t.id;
The diff does not write serial back. serial picks the name of the sequence itself, a number behind it where the name is taken and a shorter one where table and column are long; it cannot be given to a column that is there; and it says nothing of a sequence that counts otherwise. The three statements make what the target has, under the name the default calls, and are what the same sequence needs when no column owns it.
What is not done
A sequence is created, and given to its column, and nothing else:
A sequence only the database has is left where it is. The one a column owns goes with that column --
DROP COLUMNandDROP TABLEtake it -- and one whose column loses its default (serialmade a plaininteger, or an identity column) stays behind, owned by the column and called by nothing. The one exception is a sequence no column owns in a schema that is dropped ("diff"):DROP SCHEMAis written withoutCASCADE, so it would find the sequence in the schema.A sequence both sides have is not compared: another type (
serialmadebigserialalters the column and leaves its sequence counting ininteger), another increment, bounds, cache or cycle are not seen, and noALTER SEQUENCEis written for them. Only what it belongs to is (DBIO::PostgreSQL::Diff::SequenceOwner).Where a sequence stands is data and not in the model. One the diff creates starts at its
start_value, also when it is for a column of a table that has rows:ADD COLUMNnumbers those rows from it,SET DEFAULTon a column that is there does not, and the next row counts from the start.
The sequence of an identity column is none of this class's: it is a part of its column (DBIO::PostgreSQL::Introspect::Sequences, is_identity), made by GENERATED ... AS IDENTITY as DBIO::PostgreSQL::Diff::Table and DBIO::PostgreSQL::Diff::Column write it.
ATTRIBUTES
schema_name
The schema the sequence is in.
sequence_name
The name of the sequence.
sequence_info
The target's entry for the sequence ("fetch" in DBIO::PostgreSQL::Introspect::Sequences): what it counts in and how.
METHODS
is_own
next unless DBIO::PostgreSQL::Diff::Sequence->is_own($entry);
True for an entry of the sequences section that is a sequence of its own, false for the sequence of an identity column (is_identity), which is a part of that column, and for no entry at all.
diff
my @ops = DBIO::PostgreSQL::Diff::Sequence->diff(
$source, $target, $source_schemas, $target_schemas);
Compares two sequences sections (as from DBIO::PostgreSQL::Introspect::Sequences) and returns a create operation for every sequence of its own ("is_own") that the target has and the source lacks, in the order of their keys. Nothing is returned for a sequence both have, or for one only the source has: see "What is not done", except that a sequence of its own ("is_own") that no column owns and that stands in a schema the source has and the target lacks (the optional schemas sections of the two models) gets a drop, since the schema goes without CASCADE.
One case is refused, with refusing to diff: ... and no statement: a sequence to create whose name the database has as the sequence of an identity column -- what a column that was GENERATED ... AS IDENTITY and is declared serial now comes to, since both name their sequence <table>_<column>_seq. CREATE SEQUENCE finds the name taken until DROP IDENTITY has run, and that is a statement for the column, which comes later. The message names the sequence and the column and the way out, ALTER TABLE ... ALTER COLUMN ... DROP IDENTITY.
qualified_name
my $fqn = $op->qualified_name; # 'public.t_id_seq'
The schema and the name joined with a dot, unquoted: what "summary" prints. It is not SQL.
as_sql
Returns DROP SEQUENCE for a drop, and CREATE SEQUENCE for a create, with every parameter the sequence_info has: AS its data_type, INCREMENT BY, MINVALUE, MAXVALUE, START WITH, CACHE, and CYCLE or NO CYCLE. A model read from the catalog has them all, so the sequence is made as the target has it whatever the server's defaults are; one built by hand that leaves a parameter out gets the server's default for it. Schema and name are written as identifiers by the server's rule ("quote_ident" in DBIO::PostgreSQL::Util), the parameters as given.
For an action this class does not know it dies ("throw" in DBIO::PostgreSQL::Diff::NoStatement), and for a sequence without a name.
summary
Returns a one-line description such as +sequence: public.t_id_seq, or -sequence: ... for a drop.
SEE ALSO
DBIO::PostgreSQL::Diff::SequenceOwner - gives a sequence to its column
DBIO::PostgreSQL::Introspect::Sequences - what the model holds of a sequence
AUTHOR
DBIO & DBIx::Class Authors
COPYRIGHT AND LICENSE
Copyright (C) 2026 DBIO Authors Portions Copyright (C) 2005-2025 DBIx::Class Authors Based on DBIx::Class, heavily modified.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.