Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always run migration on datetime column when using TiDB #2163

Closed
2 tasks done
Thearas opened this issue Nov 24, 2021 · 7 comments
Closed
2 tasks done

Always run migration on datetime column when using TiDB #2163

Thearas opened this issue Nov 24, 2021 · 7 comments
Labels
bug Something isn't working migration

Comments

@Thearas
Copy link

Thearas commented Nov 24, 2021

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 馃槸

Ent always run migration on datetime column when using TiDB, even if the schema in the database is already up to date.

Expected Behavior 馃

No migration.

Steps to Reproduce 馃暪

Steps:

  1. Create a schema with fields created_at datetime(6):
// ent/schema/user.go
type User struct {
	ent.Schema
}

func (User) Fields() []ent.Field {
	return []ent.Field{field.Time("created_at").
		Immutable().
		Default(time.Now).
		SchemaType(map[string]string{
				dialect.MySQL: "datetime(6)",
		}),
	}
}
  1. Generate DB migration in offline mode, and then apply the SQLs to TiDB:
BEGIN;
CREATE TABLE IF NOT EXISTS `users`(`id` bigint AUTO_INCREMENT NOT NULL, `created_at` datetime(6) NOT NULL, PRIMARY KEY(`id`)) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
COMMIT;
  1. Repeat step 2. But every time I execute step 2, I got the same SQLs:
BEGIN;
ALTER TABLE `users` MODIFY COLUMN `created_at` datetime(6) NOT NULL;
COMMIT;

Your Environment 馃寧

Tech Version
Go 1.17.2
Ent 0.9.2
Database TiDB v5.1.2
Driver https://github.com/go-sql-driver/mysql
@Thearas

This comment was marked as resolved.

@godcong
Copy link
Contributor

godcong commented Nov 25, 2021

Have you tried it under mysql?
If there is no such exception, it is possible that there is a compatibility issue with tidb.
(I am using the same environment as you: ent+tidb, :)

@Thearas
Copy link
Author

Thearas commented Nov 25, 2021

Have you tried it under mysql?

It works fine on MySQL 8.0.27.

@yonidavidson yonidavidson added migration bug Something isn't working labels Feb 3, 2022
@hedwigz
Copy link
Contributor

hedwigz commented Mar 13, 2022

Hey @Thearas 馃槃
With the new migration engine, Atlas, this is now resolved.
Upgrade to entgo.io/ent@master and use schema.WithAtlas(true)

client.Schema.Create(context.Background(), schema.WithAtlas(true))

@Thearas
Copy link
Author

Thearas commented Mar 14, 2022

Great! It works for me! @hedwigz

However, I found that Atlas does not support WithForeignKeys(false):

ent/migrate: sql/schema: WithForeignKeys(false) does not work in Atlas migration

Any plans to support WithForeignKeys(false)?

@Thearas Thearas closed this as completed Mar 14, 2022
@zeevmoney
Copy link
Contributor

Hi @Thearas 馃憢馃徎

Soon this will be solved via #2404

@zeevmoney
Copy link
Contributor

@Thearas updating you that the PR was merged :)
#2404

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working migration
Projects
None yet
Development

No branches or pull requests

5 participants