Repository: ariga/atlas
Pull request #524 (2022-01-28 09:22:17)

Fix/cli tests


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e0dab797..c3692585 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -15,6 +15,30 @@ jobs:
         with:
           version: v1.42.1
           args: --verbose --enable revive
+  generate-cmp:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2.4.0
+      - uses: actions/setup-go@v2
+        with:
+          go-version: '1.17'
+      - uses: actions/cache@v2.1.5
+        with:
+          path: ~/go/pkg/mod
+          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
+          restore-keys: |
+            ${{ runner.os }}-go-
+      - name: run "go generate ./..."
+        run: go generate ./...
+      - name: Verify generated files are checked in properly
+        run: |
+          status=$(git status --porcelain)
+          if [ -n "$status" ]; then
+            echo "you need to run 'go generate ./...' and commit the changes"
+            echo "$status"
+            exit 1
+          fi
+
   unit:
     runs-on: ubuntu-latest
     strategy:
@@ -37,6 +61,9 @@ jobs:
       - name: Run schema tests
         run: go test -race ./...
         working-directory: schema
+      - name: Run cli tests
+        run: go test -race ./...
+        working-directory: cmd
 
   integration:
     runs-on: ubuntu-latest
diff --git a/cmd/action/mux.go b/cmd/action/mux.go
index 832e01b2..ae33aecd 100644
--- a/cmd/action/mux.go
+++ b/cmd/action/mux.go
@@ -102,7 +102,7 @@ func SchemaNameFromDSN(url string) (string, error) {
 func postgresSchema(dsn string) (string, error) {
 	url, err := url.Parse(dsn)
 	if err != nil {
-		return "", nil
+		return "", err
 	}
 	// lib/pq supports setting default schemas via the `search_path` parameter
 	// in a dsn.
diff --git a/doc/md/contributing.md b/doc/md/contributing.md
new file mode 100644
index 00000000..64b8a57b
--- /dev/null
+++ b/doc/md/contributing.md
@@ -0,0 +1,74 @@
+---
+title: Contributing
+id: contributing
+slug: /contributing
+---
+Atlas is a community project, we welcome contributions of all kinds and sizes!
+
+Here are some ways in which you can help:
+* File well-written and descriptive bug reports or feature requests in the [Issues page](https://github.com/ariga/atlas/issues).
+* Tweet about your experience with Atlas on [Twitter](https://twitter.com), don't forget to mention
+  [@ariga_io](https://twitter.com/ariga_io) and link to [atlasgo.io](https://atlasgo.io) if you do.
+* Write educational content on your personal blog or websites such as [dev.to](https://dev.to) or 
+  [Medium](https://medium.com). If you do, don't hesitate to reach out to us via Discord (link below)
+  for help proof-reading your text and 
+  using our social-media channels for distributing it to readers.
+* Join our [Discord Server](https://discord.com/invite/QhsmBAWzrC) to answer questions of other users
+  and find out other ways in which you can contribute by talking to the community there!
+* Contribute bug-fixes or new features to the [codebase](https://github.com/ariga/atlas).
+
+### Contributing code to Atlas
+
+As we are still starting out, we don't have an official code-style or guidelines on composing your
+code. As general advice, read through the area of the code that you are modifying and try to keep your code
+similar to what others have written in the same place.  
+
+#### Code-generation
+
+Some of the code in the Atlas repository is generated. The CI process verifies that
+all generated files are checked in by running `go generate ./...` and then running
+`git status --porcelain`. Therefore, before committing changes to Atlas, please run:
+```shell
+go generate ./...
+```
+
+#### Linting
+
+Your code will be linted using `golangci-lint` during CI. To install in locally,
+[follow this guide](https://golangci-lint.run/usage/install/#local-installation). 
+
+To run it locally:
+```shell
+golangci-lint run
+```
+
+#### Formatting 
+Format your code using the standard `fmt` command:
+```shell
+go fmt ./...
+```
+
+#### Unit-tests
+
+Your code should be covered in unit-tests, see the codebase for examples. To run tests:
+```shell
+go test ./...
+```
+
+#### Integration tests
+
+Some features, especially those that interact directly with a database must be verified
+in an integration test. There is extensive infrastructure for integration tests under
+`internal/integration/` that runs tests under a matrix of database dialect (Postres, MySQL, etc.)
+and versions. To run the integration tests, first use the `docker-compose.yml` file to spin up
+databases to test against:
+
+```shell
+cd internal/integration 
+docker-compose up -d
+```
+
+Then run the tests, from with the `integration` directory:
+```shell
+go test ./...
+```
\ No newline at end of file
diff --git a/doc/website/sidebars.js b/doc/website/sidebars.js
index ab28326e..c7a63807 100644
--- a/doc/website/sidebars.js
+++ b/doc/website/sidebars.js
@@ -37,6 +37,10 @@ module.exports = {
         {
             type: 'doc',
             id: 'ui/atlas-ui-intro',
+        },
+        {
+            type: 'doc',
+            id: 'contributing',
         }
     ],
     aboutSidebar: [


Test patch:
diff --git a/cmd/action/mux_test.go b/cmd/action/mux_test.go
index 86a0535e..639fa849 100644
--- a/cmd/action/mux_test.go
+++ b/cmd/action/mux_test.go
@@ -121,6 +121,7 @@ func Test_PostgresSchemaDSN(t *testing.T) {
 	var tests = []struct {
 		dsn      string
 		expected string
+		err      bool
 	}{
 		{
 			dsn:      "postgres://localhost:5432/dbname?search_path=foo",
@@ -128,21 +129,26 @@ func Test_PostgresSchemaDSN(t *testing.T) {
 		},
 		{
 			dsn:      "postgres://localhost:5432/dbname",
-			expected: "public",
+			expected: "",
 		},
 		{
 			dsn:      "postgres://(bad:host)?search_path=foo",
-			expected: "public",
+			expected: "",
+			err:      true,
 		},
 		{
 			dsn:      "postgres://localhost:5432/dbname?search_path=",
-			expected: "public",
+			expected: "",
 		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.dsn, func(t *testing.T) {
 			schema, err := action.SchemaNameFromDSN(tt.dsn)
-			require.NoError(t, err)
+			if tt.err {
+				require.Error(t, err)
+			} else {
+				require.NoError(t, err)
+			}
 			require.Equal(t, tt.expected, schema)
 		})
 	}
diff --git a/internal/integration/testdata/mysql/column-time-precision-maria.txt b/internal/integration/testdata/mysql/column-time-precision-maria.txt
index 62c64865..385f3827 100644
--- a/internal/integration/testdata/mysql/column-time-precision-maria.txt
+++ b/internal/integration/testdata/mysql/column-time-precision-maria.txt
@@ -17,6 +17,12 @@ table "foo" {
     null = false
     type = char(36)
   }
+  column "precision_default" {
+    null      = false
+    type      = timestamp
+    default   = sql("CURRENT_TIMESTAMP")
+    on_update = sql("CURRENT_TIMESTAMP")
+  }
   column "create_time" {
     null    = false
     type    = timestamp(6)
@@ -36,6 +42,7 @@ table "foo" {
 -- 1.sql --
 CREATE TABLE `foo` (
   `id` char(36) NOT NULL,
+  `precision_default` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
   `create_time` timestamp(6) NOT NULL DEFAULT current_timestamp(6),
   `update_time` datetime(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
   PRIMARY KEY (`id`)
diff --git a/internal/integration/testdata/mysql/column-time-precision-mysql.txt b/internal/integration/testdata/mysql/column-time-precision-mysql.txt
index 5060eb98..93335667 100644
--- a/internal/integration/testdata/mysql/column-time-precision-mysql.txt
+++ b/internal/integration/testdata/mysql/column-time-precision-mysql.txt
@@ -17,6 +17,12 @@ table "foo" {
     null = false
     type = char(36)
   }
+  column "precision_default" {
+    null      = false
+    type      = timestamp
+    default   = sql("CURRENT_TIMESTAMP")
+    on_update = sql("CURRENT_TIMESTAMP")
+  }
   column "create_time" {
     null    = false
     type    = timestamp(6)
@@ -36,6 +42,7 @@ table "foo" {
 -- 1.sql --
 CREATE TABLE `foo` (
   `id` char(36) NOT NULL,
+  `precision_default` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   `create_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
   `update_time` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
   PRIMARY KEY (`id`)