sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

Weird / wrong indentation with --reindent flag

Open scrouthtv opened this issue 5 years ago • 9 comments

I have this sql file:

CREATE TABLE `fehlermeldung` (
	`fehlermeldung-id` int(10) UNSIGNED NOT NULL,
	`datum` date NOT NULL,
	`beschreibung` text COLLATE utf8mb4_unicode_ci NOT NULL,
	`melder` int(11) UNSIGNED NOT NULL, --> `Lehrer`.`Lehrer-ID`
	PRIMARY KEY (`fehlermeldung-id`)
	KEY `melder` (`melder`)
) ENGINE=innodb DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Which I'd say is somewhat reasonably formatted and indented.

I am running sqlformat --version 0.3.1.

When I run sqlformat test.sql I get the same well-formatted file:

 ~/autoformat > sqlformat --indent_width 2 --keywords upper test.sql 
CREATE TABLE `fehlermeldung` (
	`fehlermeldung-id` int(10) UNSIGNED NOT NULL,
	`datum` date NOT NULL,
	`beschreibung` text COLLATE utf8mb4_unicode_ci NOT NULL,
	`melder` int(11) UNSIGNED NOT NULL, --> `Lehrer`.`Lehrer-ID`
	PRIMARY KEY (`fehlermeldung-id`)
	KEY `melder` (`melder`)
) ENGINE=innodb DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

However, if I add the --reindent flag, the output is gets messy:

 ~/autoformat > sqlformat --reindent --indent_width 2 --keywords upper test.sql
CREATE TABLE `fehlermeldung` (`fehlermeldung-id` int(10) UNSIGNED NOT NULL,
                                                                  `datum` date NOT NULL,
                                                                               `beschreibung` text COLLATE utf8mb4_unicode_ci NOT NULL,
                                                                                                                              `melder` int(11) UNSIGNED NOT NULL, --> `Lehrer`.`Lehrer-ID`
 PRIMARY KEY (`fehlermeldung-id`) KEY `melder` (`melder`)) ENGINE=innodb DEFAULT
CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

How can I fix this? Should I simply not call the --reindent flag?

scrouthtv avatar Jul 12 '20 16:07 scrouthtv

I'm seeing the same issue with PostgreSQL code. The original looks like this:

CREATE TABLE public.tag
(
    id integer NOT NULL DEFAULT nextval('tag_id_seq'::regclass),
    name character varying(50) COLLATE pg_catalog."default",
    CONSTRAINT tag_pkey PRIMARY KEY (id),
    CONSTRAINT tag_name_key UNIQUE (name)
)

TABLESPACE pg_default;

ALTER TABLE public.tag
    OWNER TO chatterbot;

With reindent turned on (4 spaces), it gets formatted as:

CREATE TABLE public.tag ( id integer NOT NULL DEFAULT nextval('tag_id_seq'::regclass),
                                                      name character varying(50) COLLATE pg_catalog."default",
                                                                                         CONSTRAINT tag_pkey PRIMARY KEY (id), CONSTRAINT tag_name_key UNIQUE (name)) TABLESPACE pg_default;


ALTER TABLE public.tag OWNER TO chatterbot;

dpage-edb avatar Jul 31 '20 13:07 dpage-edb

how about this issue finally?

jjsty1e avatar Jun 10 '21 09:06 jjsty1e

Maybe e575ae2c37359ab51b23ce44ccda4bb9bbfd3a5f fixed it, I'll check later

scrouthtv avatar Jun 14 '21 06:06 scrouthtv

Still bugged on sqlformat --version 0.4.1

scrouthtv avatar Jun 14 '21 15:06 scrouthtv

Is omitting the reindent flag the only workaround now?

wiwiiwiii avatar Aug 30 '21 03:08 wiwiiwiii

I am not aware of any other workaround

scrouthtv avatar Aug 30 '21 06:08 scrouthtv

@scrouthtv I see that you mentioned this in another issue of vim-autoformat some time ago, so I assume that you are using this program as the formatter for sql files via the vim-autoformat plugin.

In that case, I have just found another program for dealing with this, which is an npm package called sql-formatter.

let g:formatdef_sqlformatter_sql = "'sql-formatter -l mysql -i '.shiftwidth().' --uppercase --lines-between-queries 2'"
let g:formatters_mysql      = ['sqlformatter_sql']
let g:formatters_sql        = ['sqlformatter_sql']

Hope this helps. Probably this repo is not doing much to enhance its functionality. Time to close this issue, bruh.

wiwiiwiii avatar Aug 30 '21 09:08 wiwiiwiii

Still bugged on sqlformat --version 0.4.2

howcrazy avatar Nov 20 '21 05:11 howcrazy