@@ -102,13 +102,21 @@ pub enum TableConstraint {
102102 /// [2]: https://dev.mysql.com/doc/refman/8.0/en/spatial-types.html
103103 FulltextOrSpatial ( FullTextOrSpatialConstraint ) ,
104104 /// PostgreSQL [definition][1] for promoting an existing unique index to a
105- /// `PRIMARY KEY` or `UNIQUE` constraint:
105+ /// `PRIMARY KEY` constraint:
106106 ///
107- /// `[ CONSTRAINT constraint_name ] { UNIQUE | PRIMARY KEY } USING INDEX index_name
107+ /// `[ CONSTRAINT constraint_name ] PRIMARY KEY USING INDEX index_name
108108 /// [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]`
109109 ///
110110 /// [1]: https://www.postgresql.org/docs/current/sql-altertable.html
111- ConstraintUsingIndex ( ConstraintUsingIndex ) ,
111+ PrimaryKeyUsingIndex ( ConstraintUsingIndex ) ,
112+ /// PostgreSQL [definition][1] for promoting an existing unique index to a
113+ /// `UNIQUE` constraint:
114+ ///
115+ /// `[ CONSTRAINT constraint_name ] UNIQUE USING INDEX index_name
116+ /// [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]`
117+ ///
118+ /// [1]: https://www.postgresql.org/docs/current/sql-altertable.html
119+ UniqueUsingIndex ( ConstraintUsingIndex ) ,
112120}
113121
114122impl From < UniqueConstraint > for TableConstraint {
@@ -147,11 +155,6 @@ impl From<FullTextOrSpatialConstraint> for TableConstraint {
147155 }
148156}
149157
150- impl From < ConstraintUsingIndex > for TableConstraint {
151- fn from ( constraint : ConstraintUsingIndex ) -> Self {
152- TableConstraint :: ConstraintUsingIndex ( constraint)
153- }
154- }
155158
156159impl fmt:: Display for TableConstraint {
157160 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -162,7 +165,10 @@ impl fmt::Display for TableConstraint {
162165 TableConstraint :: Check ( constraint) => constraint. fmt ( f) ,
163166 TableConstraint :: Index ( constraint) => constraint. fmt ( f) ,
164167 TableConstraint :: FulltextOrSpatial ( constraint) => constraint. fmt ( f) ,
165- TableConstraint :: ConstraintUsingIndex ( constraint) => constraint. fmt ( f) ,
168+ TableConstraint :: PrimaryKeyUsingIndex ( c) => {
169+ c. fmt_with_keyword ( f, "PRIMARY KEY" )
170+ }
171+ TableConstraint :: UniqueUsingIndex ( c) => c. fmt_with_keyword ( f, "UNIQUE" ) ,
166172 }
167173 }
168174}
@@ -563,26 +569,21 @@ impl crate::ast::Spanned for UniqueConstraint {
563569pub struct ConstraintUsingIndex {
564570 /// Optional constraint name.
565571 pub name : Option < Ident > ,
566- /// Whether this is a `PRIMARY KEY` (true) or `UNIQUE` (false) constraint.
567- pub is_primary_key : bool ,
568572 /// The name of the existing unique index to promote.
569573 pub index_name : Ident ,
570574 /// Optional characteristics like `DEFERRABLE`.
571575 pub characteristics : Option < ConstraintCharacteristics > ,
572576}
573577
574- impl fmt:: Display for ConstraintUsingIndex {
575- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
578+ impl ConstraintUsingIndex {
579+ /// Format as `[CONSTRAINT name] <keyword> USING INDEX index_name [characteristics]`.
580+ pub fn fmt_with_keyword ( & self , f : & mut fmt:: Formatter , keyword : & str ) -> fmt:: Result {
576581 use crate :: ast:: ddl:: { display_constraint_name, display_option_spaced} ;
577582 write ! (
578583 f,
579584 "{}{} USING INDEX {}" ,
580585 display_constraint_name( & self . name) ,
581- if self . is_primary_key {
582- "PRIMARY KEY"
583- } else {
584- "UNIQUE"
585- } ,
586+ keyword,
586587 self . index_name,
587588 ) ?;
588589 write ! ( f, "{}" , display_option_spaced( & self . characteristics) ) ?;
0 commit comments