Repository: ariga/atlas
Pull request #572 (2022-02-13 07:34:14)

sql/mysql: marshal inspected auto_increment


Patch:
diff --git a/doc/md/getting-started/04-ui.md b/doc/md/getting-started/04-ui.md
new file mode 100644
index 00000000..f9c59d53
--- /dev/null
+++ b/doc/md/getting-started/04-ui.md
@@ -0,0 +1,111 @@
+---
+id: getting-started-management-ui
+title: Using the Management UI
+slug: /cli/getting-started/using-the-management-ui
+---
+
+### Using the Management UI
+Aside from being a command line tool, Atlas also has a user interface that allows us to visually manage our schemas. 
+
+After being introduced to the CLI’s capabilities, let’s see what we can do with the Management UI.
+
+###Inspecting
+We will start off by inspecting our database with the `-w` flag. To do so, run:
+
+`atlas schema inspect -d "mysql://root:pass@tcp(localhost:3306)/example" -w`
+
+Our browser will open automatically, and we should see a similar output in the CLI:
+```
+Atlas UI available at: http://127.0.0.1:5800/projects/25769803777/schemas/1 
+Press Ctrl+C to stop
+```
+
+When the UI opens, we are taken to the schema page. This screen displays a high-level view of our Atlas schema.
+
+![Atlas Schema Synced](https://atlasgo.io/uploads/images/docs/synced-atlas-schema.png)
+
+
+On the top-left (1) we can see a box with information about our schema. The "Activity & History" panel (2) holds an audit history for all changes made to the schema. Each log is clickable, allowing us to view the diff between the schema and its previous state. On the bottom-right (3), the schema is described using the Atlas DDL.  On the bottom-left part of the screen (4), the UI displays an ERD (Entity-relation diagram) showing the different tables and the connections between them (via foreign-keys). By clicking the "expand" icon on the top-right corner of the ERD panel, we can open a more detailed view of our schema.
+
+![Schema ERD open](https://atlasgo.io/uploads/images/docs/schema-erd-open.png)
+
+### Running a migration
+
+Now that we have seen how Atlas can visualize our database schema in the UI, let’s see how to initiate a migration.
+
+First, we will make changes to our schema by clicking on the "Edit Schema" button on the top right-hand corner.
+
+![Edit Schema Button](https://atlasgo.io/uploads/images/docs/edit-schema-button.png)
+
+Next, add the table `likes` to the schema:
+```
+table "likes" {
+ schema = schema.example
+ column "id" {
+   null = false
+   type = int
+ }
+ column "user_id" {
+   null = false
+   type = int
+ }
+ column "blog_post_id" {
+   null = false
+   type = int
+ }
+ primary_key {
+   columns = [table.likes.column.id]
+ }
+ foreign_key "like_blog_post" {
+   columns     = [table.likes.column.blog_post_id]
+   ref_columns = [table.blog_posts.column.id]
+   on_update   = "RESTRICT"
+   on_delete   = "RESTRICT"
+ }
+ foreign_key "like_user" {
+   columns     = [table.likes.column.user_id]
+   ref_columns = [table.users.column.id]
+   on_update   = "RESTRICT"
+   on_delete   = "RESTRICT"
+ }
+ index "like_blog_post" {
+   columns = [table.likes.column.blog_post_id]
+ }
+ index "like_user" {
+   columns = [table.likes.column.user_id]
+ }
+}
+```
+
+Click "save" on the top right-hand corner.
+
+![Save Schema Button](https://atlasgo.io/uploads/images/docs/save-schema.png)
+
+Going back to our schema page, we can notice a few changes. First, our schema is now out of sync. This means we have created a change in the desired state that isn't yet reflected in the database. When a schema is out of sync, two separate schemas appear: desired and inspected.
+The _desired_ schema is the one being edited in the UI. The _inspected_ schema is the
+true, current state of the database. By clicking the toggle and changing the view to "unified", the schema is shown as one with a highlighted diff.
+
+Looking at the ERD, we can see that our table `likes` is green and has a plus-sign (+) next to it, showing that it needs to be added. This means the change has been recorded, but not yet applied.
+
+![Atlas Schema Not Synced Split](https://atlasgo.io/uploads/images/docs/schema-out-of-sync-unified.png)
+
+Now, let’s run a migration!
+Click "Migrate Schema" in the Schema Status box to apply the changes we want to make to our schema.
+
+The migration screen will open up, running the setup automatically. After the setup succeeds, we will see the migration plan.
+The diff in the schema in HCL on the left pane, and the planned SQL statements on the right.
+
+![Atlas Schema Not Synced Split](https://atlasgo.io/uploads/images/docs/migrate-plan-step.png)
+
+To start the migration, click "Apply". If you ever wish to not go through with a migration, you can click "Abort".
+
+![Atlas Schema Not Synced Split](https://atlasgo.io/uploads/images/docs/migrate-apply-step.png)
+
+Congrats, we have successfully completed a migration!
+
+As expected, after executing our migration plan, our database and desired schema are now synced.
+
+![Atlas Schema Not Synced Split](https://atlasgo.io/uploads/images/docs/synced-schema-after-migration.png)
+
+### Conclusion
+In this section we learned how to use the Management UI to inspect our database and run migrations.
diff --git a/sql/mysql/sqlspec.go b/sql/mysql/sqlspec.go
index ee39f96b..cc15d6f6 100644
--- a/sql/mysql/sqlspec.go
+++ b/sql/mysql/sqlspec.go
@@ -229,6 +229,9 @@ func columnSpec(c *schema.Column, t *schema.Table) (*sqlspec.Column, error) {
 	if o := (OnUpdate{}); sqlx.Has(c.Attrs, &o) {
 		col.Extra.Attrs = append(col.Extra.Attrs, specutil.RawAttr("on_update", o.A))
 	}
+	if sqlx.Has(c.Attrs, &AutoIncrement{}) {
+		col.Extra.Attrs = append(col.Extra.Attrs, specutil.BoolAttr("auto_increment", true))
+	}
 	return col, nil
 }
 


Test patch:
diff --git a/sql/mysql/sqlspec_test.go b/sql/mysql/sqlspec_test.go
index 8e9d9b8a..2cb9b443 100644
--- a/sql/mysql/sqlspec_test.go
+++ b/sql/mysql/sqlspec_test.go
@@ -466,6 +466,41 @@ schema "test" {
 	require.EqualValues(t, expected, string(buf))
 }
 
+func TestMarshalSpec_AutoIncrement(t *testing.T) {
+	s := &schema.Schema{
+		Name: "test",
+		Tables: []*schema.Table{
+			{
+				Name: "users",
+				Columns: []*schema.Column{
+					{
+						Name: "id",
+						Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}},
+						Attrs: []schema.Attr{
+							&AutoIncrement{V: 1000},
+						},
+					},
+				},
+			},
+		},
+	}
+	s.Tables[0].Schema = s
+	buf, err := MarshalSpec(s, hclState)
+	require.NoError(t, err)
+	const expected = `table "users" {
+  schema = schema.test
+  column "id" {
+    null           = false
+    type           = bigint
+    auto_increment = true
+  }
+}
+schema "test" {
+}
+`
+	require.EqualValues(t, expected, string(buf))
+}
+
 func TestMarshalSpec_Check(t *testing.T) {
 	s := schema.New("test").
 		AddTables(