Repository: ariga/atlas
Pull request #914 (2022-07-07 11:42:46)

all: miscellaneous changes while running manual testing


Patch:
diff --git a/cmd/atlas/internal/cmdapi/migrate.go b/cmd/atlas/internal/cmdapi/migrate.go
index eb9bdbf7..e38aaec7 100644
--- a/cmd/atlas/internal/cmdapi/migrate.go
+++ b/cmd/atlas/internal/cmdapi/migrate.go
@@ -99,7 +99,7 @@ to re-hash the contents and resolve the error
 	}
 	// MigrateApplyCmd represents the 'atlas migrate apply' subcommand.
 	MigrateApplyCmd = &cobra.Command{
-		Use:   "apply",
+		Use:   "apply [flags] [count]",
 		Short: "Applies pending migration files on the connected database.",
 		Long: `'atlas migrate apply' reads the migration state of the connected database and computes what migrations are pending.
 It then attempts to apply the pending migration files in the correct order onto the database. 
@@ -108,19 +108,20 @@ As a safety measure 'atlas migrate apply' will abort with an error, if:
   - the migration directory is not in sync with the 'atlas.sum' file
   - the migration and database history do not match each other`,
 		Example: `  atlas migrate apply --to mysql://user:pass@localhost:3306/dbname
-  atlas migrate apply 1 --dir file:///path/to/migration/directory --to mysql://user:pass@localhost:3306/dbname`,
+  atlas migrate apply --dir file:///path/to/migration/directory --to mysql://user:pass@localhost:3306/dbname 1`,
 		Args: cobra.MaximumNArgs(1),
 		RunE: CmdMigrateApplyRun,
 	}
 	// MigrateDiffCmd represents the 'atlas migrate diff' subcommand.
 	MigrateDiffCmd = &cobra.Command{
-		Use:   "diff",
-		Short: "Compute the diff between the migration directory and a connected database and create a new migration file.",
-		Long: `'atlas migrate diff' uses the dev-database to re-run all migration files in the migration
-directory and compares it to a given desired state and create a new migration file containing SQL statements to migrate 
-the migration directory state to the desired schema. The desired state can be another connected database or an HCL file.`,
-		Example: `  atlas migrate diff --dev-url mysql://user:pass@localhost:3306/dev --to mysql://user:pass@localhost:3306/dbname
-  atlas migrate diff --dev-url mysql://user:pass@localhost:3306/dev --to file://atlas.hcl`,
+		Use:   "diff [flags] [name]",
+		Short: "Compute the diff between the migration directory and a desired state and create a new migration file.",
+		Long: `'atlas migrate diff' uses the dev-database to re-run all migration files in the migration directory, compares
+it to a given desired state and create a new migration file containing SQL statements to migrate the migration
+directory state to the desired schema. The desired state can be another connected database or an HCL file.`,
+		Example: `  atlas migrate diff --dev-url mysql://user:pass@localhost:3306/dev --to file://atlas.hcl
+  atlas migrate diff --dev-url mysql://user:pass@localhost:3306/dev --to file://atlas.hcl add_users_table
+  atlas migrate diff --dev-url mysql://user:pass@localhost:3306/dev --to mysql://user:pass@localhost:3306/dbname`,
 		Args:    cobra.MaximumNArgs(1),
 		PreRunE: migrateFlagsFromEnv,
 		RunE:    CmdMigrateDiffRun,
@@ -160,7 +161,8 @@ executed on the connected database in order to validate SQL semantics.`,
 	}
 	// MigrateLintCmd represents the 'atlas migrate Lint' command.
 	MigrateLintCmd = &cobra.Command{
-		Use: "lint",
+		Use:   "lint",
+		Short: "Run analysis on the migration directory",
 		Example: `  atlas migrate lint
   atlas migrate lint --dir file:///path/to/migration/directory --dev-url mysql://root:pass@localhost:3306 --latest 1
   atlas migrate lint --dir file:///path/to/migration/directory --dev-url mysql://root:pass@localhost:3306 --git-base master
@@ -198,6 +200,7 @@ func init() {
 	MigrateApplyCmd.Flags().StringVarP(&MigrateFlags.LogFormat, migrateFlagLog, "", logFormatTTY, "log format to use")
 	MigrateApplyCmd.Flags().StringVarP(&MigrateFlags.RevisionSchema, migrateFlagRevisionsSchema, "", "", "schema name where the revisions table is to be created")
 	toURL(MigrateApplyCmd.Flags())
+	cobra.CheckErr(MigrateApplyCmd.MarkFlagRequired(migrateFlagTo))
 	// Diff flags.
 	devURL(MigrateDiffCmd.Flags())
 	toURL(MigrateDiffCmd.Flags())
diff --git a/doc/md/getting-started/02-inspecting.mdx b/doc/md/getting-started/02-inspecting.mdx
index 827c91df..bf82ac70 100644
--- a/doc/md/getting-started/02-inspecting.mdx
+++ b/doc/md/getting-started/02-inspecting.mdx
@@ -19,7 +19,7 @@ atlas schema inspect --help
 ### Inspecting our database
 
 To inspect our locally-running MySQL instance from the [previous](01-introduction.mdx) section,
-use the `-u` flag and write output to a file named `atlas.hcl`:
+use the `-u` flag and write output to a file named `schema.hcl`:
 
 <Tabs
 defaultValue="mysql"
@@ -32,12 +32,12 @@ values={[
 <TabItem value="mysql">
 
 ```shell
-atlas schema inspect -u "mysql://root:pass@localhost:3306/example" > atlas.hcl
+atlas schema inspect -u "mysql://root:pass@localhost:3306/example" > schema.hcl
 ```
 
 Then, view the contents of the file:
 
-```hcl title="atlas.hcl"
+```hcl title="schema.hcl"
 schema "example" {
   charset = "utf8mb4"
   collate = "utf8mb4_0900_ai_ci"
@@ -48,12 +48,12 @@ schema "example" {
 <TabItem value="maria">
 
 ```shell
-atlas schema inspect -u "maria://root:pass@localhost:3306/example" > atlas.hcl
+atlas schema inspect -u "maria://root:pass@localhost:3306/example" > schema.hcl
 ```
 
 Then, view the contents of the file:
 
-```hcl title="atlas.hcl"
+```hcl title="schema.hcl"
 schema "example" {
   charset = "utf8mb4"
   collate = "utf8mb4_0900_ai_ci"
@@ -64,12 +64,12 @@ schema "example" {
 <TabItem value="postgres">
 
 ```shell
-atlas schema inspect -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" > atlas.hcl
+atlas schema inspect -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" > schema.hcl
 ```
 
 Then, view the contents of the file:
 
-```hcl title="atlas.hcl"
+```hcl title="schema.hcl"
 schema "public" {
 }
 ```
@@ -78,12 +78,12 @@ schema "public" {
 <TabItem value="sqlite">
 
 ```shell
-atlas schema inspect -u "sqlite://file.db" > atlas.hcl
+atlas schema inspect -u "sqlite://file.db" > schema.hcl
 ```
 
 Then, view the contents of the file:
 
-```hcl title="atlas.hcl"
+```hcl title="schema.hcl"
 schema "main" {
 }
 ```
@@ -138,28 +138,28 @@ values={[
 <TabItem value="mysql">
 
 ```shell
-atlas schema inspect -u "mysql://root:pass@localhost:3306/example" > atlas.hcl
+atlas schema inspect -u "mysql://root:pass@localhost:3306/example" > schema.hcl
 ```
 
 </TabItem>
 <TabItem value="maria">
 
 ```shell
-atlas schema inspect -u "maria://root:pass@localhost:3306/example" > atlas.hcl
+atlas schema inspect -u "maria://root:pass@localhost:3306/example" > schema.hcl
 ```
 
 </TabItem>
 <TabItem value="postgres">
 
 ```shell
-atlas schema inspect -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" > atlas.hcl
+atlas schema inspect -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" > schema.hcl
 ```
 
 </TabItem>
 <TabItem value="sqlite">
 
 ```shell
-atlas schema inspect -u "sqlite://file.db" > atlas.hcl
+atlas schema inspect -u "sqlite://file.db" > schema.hcl
 ```
 
 </TabItem>
diff --git a/doc/md/getting-started/03-applying.mdx b/doc/md/getting-started/03-applying.mdx
index 599302b4..c367fb5a 100644
--- a/doc/md/getting-started/03-applying.mdx
+++ b/doc/md/getting-started/03-applying.mdx
@@ -35,7 +35,7 @@ relationship between blog posts and categories:
 
 ![Blog ERD](https://atlasgo.io/uploads/images/blog-erd-2.png)
 
-First, let's store the existing schema in a file named `atlas.hcl`:
+First, let's store the existing schema in a file named `schema.hcl`:
 
 <Tabs
 defaultValue="mysql"
@@ -48,28 +48,28 @@ values={[
 <TabItem value="mysql">
 
 ```shell
-atlas schema inspect -u "mysql://root:pass@localhost:3306/example" > atlas.hcl
+atlas schema inspect -u "mysql://root:pass@localhost:3306/example" > schema.hcl
 ```
 
 </TabItem>
 <TabItem value="maria">
 
 ```shell
-atlas schema inspect -u "maria://root:pass@localhost:3306/example" > atlas.hcl
+atlas schema inspect -u "maria://root:pass@localhost:3306/example" > schema.hcl
 ```
 
 </TabItem>
 <TabItem value="postgres">
 
 ```shell
-atlas schema inspect -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" > atlas.hcl
+atlas schema inspect -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" > schema.hcl
 ```
 
 </TabItem>
 <TabItem value="sqlite">
 
 ```shell
-atlas schema inspect -u "sqlite://file.db" > atlas.hcl
+atlas schema inspect -u "sqlite://file.db" > schema.hcl
 ```
 
 </TabItem>
@@ -107,28 +107,28 @@ values={[
 <TabItem value="mysql">
 
 ```shell
-atlas schema apply -u "mysql://root:pass@localhost:3306/example" -f atlas.hcl
+atlas schema apply -u "mysql://root:pass@localhost:3306/example" -f schema.hcl
 ```
 
 </TabItem>
 <TabItem value="maria">
 
 ```shell
-atlas schema apply -u "maria://root:pass@localhost:3306/example" -f atlas.hcl
+atlas schema apply -u "maria://root:pass@localhost:3306/example" -f schema.hcl
 ```
 
 </TabItem>
 <TabItem value="postgres">
 
 ```shell
-atlas schema apply -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" -f atlas.hcl
+atlas schema apply -u "postgres://postgres:pass@localhost:5432/example?sslmode=disable" -f schema.hcl
 ```
 
 </TabItem>
 <TabItem value="sqlite">
 
 ```shell
-atlas schema apply -u "sqlite://file.db" -f atlas.hcl
+atlas schema apply -u "sqlite://file.db" -f schema.hcl
 ```
 
 </TabItem>
@@ -166,7 +166,7 @@ mysql> show create table categories;
 ```
 
 Amazing! Our new table was created. Next, let's define our association table,
-add the following block to our `atlas.hcl` file:
+add the following block to our `schema.hcl` file:
 ```hcl
 table "post_categories" {
     schema = schema.example
@@ -192,7 +192,7 @@ and `categories` tables.
 
 Let's try to apply the schema again, this time with the updated schema:
 ```text
-atlas schema apply -u "mysql://root:pass@localhost:3306/example" -f atlas.hcl
+atlas schema apply -u "mysql://root:pass@localhost:3306/example" -f schema.hcl
 -- Planned Changes:
 -- Create "post_categories" table
 CREATE TABLE `example`.`post_categories` (`post_id` int NOT NULL, `category_id` int NOT NULL, CONSTRAINT `post_category_post` FOREIGN KEY (`post_id`) REFERENCES `example`.`blog_posts` (`id`), CONSTRAINT `post_category_category` FOREIGN KEY (`category_id`) REFERENCES `example`.`categories` (`id`))
diff --git a/sql/sqlite/inspect.go b/sql/sqlite/inspect.go
index 1641ae6e..19d77134 100644
--- a/sql/sqlite/inspect.go
+++ b/sql/sqlite/inspect.go
@@ -393,7 +393,7 @@ func (i *inspect) databases(ctx context.Context, opts *schema.InspectRealmOption
 		query = databasesQuery
 	)
 	if opts != nil && len(opts.Schemas) > 0 {
-		query += " WHERE name IN (" + strings.Repeat("?, ", len(opts.Schemas)-1) + "?)"
+		query = fmt.Sprintf(databasesQueryArgs, strings.Repeat("?, ", len(opts.Schemas)-1)+"?")
 		for _, s := range opts.Schemas {
 			args = append(args, s)
 		}
@@ -704,7 +704,8 @@ const (
 	// Name of main database file.
 	mainFile = "main"
 	// Query to list attached database files.
-	databasesQuery = "SELECT `name`, `file` FROM pragma_database_list()"
+	databasesQuery     = "SELECT `name`, `file` FROM pragma_database_list() WHERE `name` <> 'temp'"
+	databasesQueryArgs = "SELECT `name`, `file` FROM pragma_database_list() WHERE `name` IN (%s)"
 	// Query to list database tables.
 	tablesQuery = "SELECT `name`, `sql` FROM sqlite_master WHERE `type` = 'table' AND `name` NOT LIKE 'sqlite_%'"
 	// Query to list table information.


Test patch:
diff --git a/sql/sqlite/inspect_test.go b/sql/sqlite/inspect_test.go
index f8ee9d79..09f5f7e6 100644
--- a/sql/sqlite/inspect_test.go
+++ b/sql/sqlite/inspect_test.go
@@ -520,7 +520,7 @@ func (m mock) systemVars(version string) {
 }
 
 func (m mock) tableExists(table string, exists bool, stmt ...string) {
-	m.ExpectQuery(sqltest.Escape(databasesQuery + " WHERE name IN (?)")).
+	m.ExpectQuery(sqltest.Escape(fmt.Sprintf(databasesQueryArgs, "?"))).
 		WithArgs("main").
 		WillReturnRows(sqltest.Rows(`
  name |   file