@@ -572,7 +572,11 @@ func (a *Atlas) StateReader(tables ...*Table) migrate.StateReaderFunc {
572572 if err != nil {
573573 return nil , err
574574 }
575- return & schema.Realm {Schemas : []* schema.Schema {{Tables : ts }}}, nil
575+ vs , err := a .views (tables )
576+ if err != nil {
577+ return nil , err
578+ }
579+ return & schema.Realm {Schemas : []* schema.Schema {{Tables : ts , Views : vs }}}, nil
576580 }
577581}
578582
@@ -878,8 +882,14 @@ func (d *db) ExecContext(ctx context.Context, query string, args ...any) (sql.Re
878882
879883// tables converts an Ent table slice to an atlas table slice
880884func (a * Atlas ) tables (tables []* Table ) ([]* schema.Table , error ) {
881- ts := make ([]* schema.Table , len (tables ))
882- for i , et := range tables {
885+ var (
886+ byT = make (map [* Table ]* schema.Table )
887+ ts = make ([]* schema.Table , 0 , len (tables ))
888+ )
889+ for _ , et := range tables {
890+ if et .View {
891+ continue
892+ }
883893 at := schema .NewTable (et .Name )
884894 if et .Comment != "" {
885895 at .SetComment (et .Comment )
@@ -898,10 +908,14 @@ func (a *Atlas) tables(tables []*Table) ([]*schema.Table, error) {
898908 if err := a .aIndexes (et , at ); err != nil {
899909 return nil , err
900910 }
901- ts [i ] = at
911+ ts = append (ts , at )
912+ byT [et ] = at
902913 }
903- for i , t1 := range tables {
904- t2 := ts [i ]
914+ for _ , t1 := range tables {
915+ if t1 .View {
916+ continue
917+ }
918+ t2 := byT [t1 ]
905919 for _ , fk1 := range t1 .ForeignKeys {
906920 fk2 := schema .NewForeignKey (fk1 .Symbol ).
907921 SetTable (t2 ).
@@ -938,6 +952,30 @@ func (a *Atlas) tables(tables []*Table) ([]*schema.Table, error) {
938952 return ts , nil
939953}
940954
955+ // tables converts an Ent table slice to an atlas table slice
956+ func (a * Atlas ) views (tables []* Table ) ([]* schema.View , error ) {
957+ vs := make ([]* schema.View , 0 , len (tables ))
958+ for _ , et := range tables {
959+ // Not a view, or the view defined externally.
960+ if ! et .View || et .Annotation == nil || (et .Annotation .ViewAs == "" && et .Annotation .ViewFor [a .dialect ] == "" ) {
961+ continue
962+ }
963+ def := et .Annotation .ViewFor [a .dialect ]
964+ if def == "" {
965+ def = et .Annotation .ViewAs
966+ }
967+ av := schema .NewView (et .Name , def )
968+ if et .Comment != "" {
969+ av .SetComment (et .Comment )
970+ }
971+ if err := a .aVColumns (et , av ); err != nil {
972+ return nil , err
973+ }
974+ vs = append (vs , av )
975+ }
976+ return vs , nil
977+ }
978+
941979func (a * Atlas ) aColumns (et * Table , at * schema.Table ) error {
942980 for _ , c1 := range et .Columns {
943981 c2 := schema .NewColumn (c1 .Name ).
@@ -965,6 +1003,27 @@ func (a *Atlas) aColumns(et *Table, at *schema.Table) error {
9651003 return nil
9661004}
9671005
1006+ func (a * Atlas ) aVColumns (et * Table , at * schema.View ) error {
1007+ for _ , c1 := range et .Columns {
1008+ c2 := schema .NewColumn (c1 .Name ).
1009+ SetNull (c1 .Nullable )
1010+ if c1 .Collation != "" {
1011+ c2 .SetCollation (c1 .Collation )
1012+ }
1013+ if c1 .Comment != "" {
1014+ c2 .SetComment (c1 .Comment )
1015+ }
1016+ if err := a .sqlDialect .atTypeC (c1 , c2 ); err != nil {
1017+ return err
1018+ }
1019+ if err := a .atDefault (c1 , c2 ); err != nil {
1020+ return err
1021+ }
1022+ at .AddColumns (c2 )
1023+ }
1024+ return nil
1025+ }
1026+
9681027func (a * Atlas ) atDefault (c1 * Column , c2 * schema.Column ) error {
9691028 if c1 .Default == nil || ! a .sqlDialect .supportsDefault (c1 ) {
9701029 return nil
0 commit comments