@@ -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,12 +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- }
155-
156158impl fmt:: Display for TableConstraint {
157159 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
158160 match self {
@@ -162,7 +164,8 @@ impl fmt::Display for TableConstraint {
162164 TableConstraint :: Check ( constraint) => constraint. fmt ( f) ,
163165 TableConstraint :: Index ( constraint) => constraint. fmt ( f) ,
164166 TableConstraint :: FulltextOrSpatial ( constraint) => constraint. fmt ( f) ,
165- TableConstraint :: ConstraintUsingIndex ( constraint) => constraint. fmt ( f) ,
167+ TableConstraint :: PrimaryKeyUsingIndex ( c) => c. fmt_with_keyword ( f, "PRIMARY KEY" ) ,
168+ TableConstraint :: UniqueUsingIndex ( c) => c. fmt_with_keyword ( f, "UNIQUE" ) ,
166169 }
167170 }
168171}
@@ -563,26 +566,21 @@ impl crate::ast::Spanned for UniqueConstraint {
563566pub struct ConstraintUsingIndex {
564567 /// Optional constraint name.
565568 pub name : Option < Ident > ,
566- /// Whether this is a `PRIMARY KEY` (true) or `UNIQUE` (false) constraint.
567- pub is_primary_key : bool ,
568569 /// The name of the existing unique index to promote.
569570 pub index_name : Ident ,
570571 /// Optional characteristics like `DEFERRABLE`.
571572 pub characteristics : Option < ConstraintCharacteristics > ,
572573}
573574
574- impl fmt:: Display for ConstraintUsingIndex {
575- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
575+ impl ConstraintUsingIndex {
576+ /// Format as `[CONSTRAINT name] <keyword> USING INDEX index_name [characteristics]`.
577+ pub fn fmt_with_keyword ( & self , f : & mut fmt:: Formatter , keyword : & str ) -> fmt:: Result {
576578 use crate :: ast:: ddl:: { display_constraint_name, display_option_spaced} ;
577579 write ! (
578580 f,
579581 "{}{} USING INDEX {}" ,
580582 display_constraint_name( & self . name) ,
581- if self . is_primary_key {
582- "PRIMARY KEY"
583- } else {
584- "UNIQUE"
585- } ,
583+ keyword,
586584 self . index_name,
587585 ) ?;
588586 write ! ( f, "{}" , display_option_spaced( & self . characteristics) ) ?;
0 commit comments