github.com/iasthc/atlas/cmd/atlas@v0.0.0-20230523071841-73246df3f88d/internal/sqlparse/sqliteparse/sqliteparse_test.go (about)

     1  // Copyright 2021-present The Atlas Authors. All rights reserved.
     2  // This source code is licensed under the Apache 2.0 license found
     3  // in the LICENSE file in the root directory of this source tree.
     4  
     5  package sqliteparse_test
     6  
     7  import (
     8  	"strconv"
     9  	"testing"
    10  
    11  	"github.com/iasthc/atlas/cmd/atlas/internal/sqlparse/sqliteparse"
    12  	"github.com/iasthc/atlas/sql/migrate"
    13  	"github.com/iasthc/atlas/sql/schema"
    14  
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestFixChange_RenameColumns(t *testing.T) {
    19  	var p sqliteparse.FileParser
    20  	_, err := p.FixChange(
    21  		nil,
    22  		"ALTER TABLE t RENAME COLUMN c1 TO c2",
    23  		nil,
    24  	)
    25  	require.Error(t, err)
    26  
    27  	_, err = p.FixChange(
    28  		nil,
    29  		"ALTER TABLE t RENAME COLUMN c1 TO c2",
    30  		schema.Changes{&schema.AddTable{}},
    31  	)
    32  	require.Error(t, err)
    33  
    34  	changes, err := p.FixChange(
    35  		nil,
    36  		"ALTER TABLE t RENAME COLUMN c1 TO c2",
    37  		schema.Changes{
    38  			&schema.ModifyTable{
    39  				Changes: schema.Changes{
    40  					&schema.DropColumn{C: schema.NewColumn("c1")},
    41  					&schema.AddColumn{C: schema.NewColumn("c2")},
    42  				},
    43  			},
    44  		},
    45  	)
    46  	require.NoError(t, err)
    47  	require.Equal(
    48  		t,
    49  		schema.Changes{
    50  			&schema.ModifyTable{
    51  				Changes: schema.Changes{
    52  					&schema.RenameColumn{From: schema.NewColumn("c1"), To: schema.NewColumn("c2")},
    53  				},
    54  			},
    55  		},
    56  		changes,
    57  	)
    58  }
    59  
    60  func TestFixChange_RenameTable(t *testing.T) {
    61  	var p sqliteparse.FileParser
    62  	changes, err := p.FixChange(
    63  		nil,
    64  		"ALTER TABLE t1 RENAME TO t2",
    65  		schema.Changes{
    66  			&schema.DropTable{T: schema.NewTable("t1")},
    67  			&schema.AddTable{T: schema.NewTable("t2")},
    68  			&schema.AddTable{T: schema.NewTable("t3")},
    69  		},
    70  	)
    71  	require.NoError(t, err)
    72  	require.Equal(
    73  		t,
    74  		schema.Changes{
    75  			&schema.RenameTable{From: schema.NewTable("t1"), To: schema.NewTable("t2")},
    76  			&schema.AddTable{T: schema.NewTable("t3")},
    77  		},
    78  		changes,
    79  	)
    80  }
    81  
    82  func TestColumnFilledBefore(t *testing.T) {
    83  	for i, tt := range []struct {
    84  		file       string
    85  		pos        int
    86  		wantFilled bool
    87  		wantErr    bool
    88  	}{
    89  		{
    90  			file: `UPDATE t SET c = NULL;`,
    91  			pos:  100,
    92  		},
    93  		{
    94  			file:       "UPDATE `t` SET c = 2;",
    95  			pos:        100,
    96  			wantFilled: true,
    97  		},
    98  		{
    99  			file:       `UPDATE t SET c = 2 WHERE c IS NULL;`,
   100  			pos:        100,
   101  			wantFilled: true,
   102  		},
   103  		{
   104  			file:       "UPDATE `t` SET `c` = 2 WHERE `c` IS NULL;",
   105  			pos:        100,
   106  			wantFilled: true,
   107  		},
   108  		{
   109  			file:       `UPDATE t SET c = 2 WHERE c IS NOT NULL;`,
   110  			pos:        100,
   111  			wantFilled: false,
   112  		},
   113  		{
   114  			file:       `UPDATE t SET c = 2 WHERE c <> NULL`,
   115  			pos:        100,
   116  			wantFilled: false,
   117  		},
   118  		{
   119  			file: `
   120  UPDATE t1 SET c = 2 WHERE c IS NULL;
   121  UPDATE t SET c = 2 WHERE c IS NULL;
   122  `,
   123  			pos:        2,
   124  			wantFilled: false,
   125  		},
   126  		{
   127  			file: `
   128  UPDATE t SET c = 2 WHERE c IS NULL;
   129  UPDATE t1 SET c = 2 WHERE c IS NULL;
   130  `,
   131  			pos:        30,
   132  			wantFilled: true,
   133  		},
   134  	} {
   135  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   136  			var (
   137  				p sqliteparse.FileParser
   138  				f = migrate.NewLocalFile("file", []byte(tt.file))
   139  			)
   140  			filled, err := p.ColumnFilledBefore(f, schema.NewTable("t"), schema.NewColumn("c"), tt.pos)
   141  			require.Equal(t, err != nil, tt.wantErr, err)
   142  			require.Equal(t, filled, tt.wantFilled)
   143  		})
   144  	}
   145  }
   146  
   147  func TestCreateViewAfter(t *testing.T) {
   148  	for i, tt := range []struct {
   149  		file        string
   150  		pos         int
   151  		wantCreated bool
   152  		wantErr     bool
   153  	}{
   154  		{
   155  			file: `
   156  ALTER TABLE old RENAME TO new;
   157  CREATE VIEW old AS SELECT * FROM new;
   158  `,
   159  			pos:         1,
   160  			wantCreated: true,
   161  		},
   162  		{
   163  			file: `
   164  ALTER TABLE old RENAME TO new;
   165  CREATE VIEW old AS SELECT * FROM users;
   166  `,
   167  			pos: 1,
   168  		},
   169  		{
   170  			file: `
   171  ALTER TABLE old RENAME TO new;
   172  CREATE VIEW old AS SELECT * FROM new JOIN new;
   173  `,
   174  			pos: 1,
   175  		},
   176  		{
   177  			file: `
   178  ALTER TABLE old RENAME TO new;
   179  CREATE VIEW old AS SELECT * FROM new;
   180  `,
   181  			pos: 100,
   182  		},
   183  		{
   184  			file: `
   185  ALTER TABLE old RENAME TO new;
   186  CREATE VIEW old AS SELECT a, b, c FROM new;
   187  `,
   188  			wantCreated: true,
   189  		},
   190  	} {
   191  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   192  			var (
   193  				p sqliteparse.FileParser
   194  				f = migrate.NewLocalFile("file", []byte(tt.file))
   195  			)
   196  			created, err := p.CreateViewAfter(f, "old", "new", tt.pos)
   197  			require.Equal(t, err != nil, tt.wantErr, err)
   198  			require.Equal(t, created, tt.wantCreated)
   199  		})
   200  	}
   201  }