github.com/aleksanderaleksic/tgmigrate@v0.1.7/migration/migration_reverter_test.go (about)

     1  package migration
     2  
     3  import (
     4  	"github.com/aleksanderaleksic/tgmigrate/common"
     5  	"github.com/aleksanderaleksic/tgmigrate/history"
     6  	"github.com/stretchr/testify/assert"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestReverter_getMigrationWithName_SingleExpectedMigration(t *testing.T) {
    12  
    13  	expected := history.AppliedStorageHistoryObject{
    14  		SchemaVersion: "v1",
    15  		Applied:       common.JSONTime(time.Now()),
    16  		Hash:          "sample_hash",
    17  		Name:          "V1__expected.hcl",
    18  		Metadata: history.MetadataWrapper{
    19  			S3Metadata: nil,
    20  		},
    21  	}
    22  
    23  	slice := []history.AppliedStorageHistoryObject{
    24  		expected,
    25  	}
    26  
    27  	actual := getMigrationWithName(slice, expected.Name)
    28  	assert.Equal(t, expected, *actual)
    29  }
    30  
    31  func TestReverter_getMigrationWithName_SingleUnexpectedMigration(t *testing.T) {
    32  	slice := []history.AppliedStorageHistoryObject{
    33  		{
    34  			SchemaVersion: "v1",
    35  			Applied:       common.JSONTime(time.Now()),
    36  			Hash:          "sample_hash",
    37  			Name:          "V1__unexpected.hcl",
    38  			Metadata: history.MetadataWrapper{
    39  				S3Metadata: nil,
    40  			},
    41  		},
    42  	}
    43  
    44  	actual := getMigrationWithName(slice, "V1__expected.hcl")
    45  	assert.Nil(t, actual)
    46  }
    47  
    48  func TestReverter_getMigrationWithName_MultipleMigrationsWithExpected(t *testing.T) {
    49  
    50  	expected := history.AppliedStorageHistoryObject{
    51  		SchemaVersion: "v1",
    52  		Applied:       common.JSONTime(time.Now()),
    53  		Hash:          "sample_hash",
    54  		Name:          "V1__expected.hcl",
    55  		Metadata: history.MetadataWrapper{
    56  			S3Metadata: nil,
    57  		},
    58  	}
    59  
    60  	slice := []history.AppliedStorageHistoryObject{
    61  		expected,
    62  		{
    63  			SchemaVersion: "v1",
    64  			Applied:       common.JSONTime(time.Now()),
    65  			Hash:          "sample_hash",
    66  			Name:          "V2__unexpected.hcl",
    67  			Metadata: history.MetadataWrapper{
    68  				S3Metadata: nil,
    69  			},
    70  		},
    71  	}
    72  
    73  	actual := getMigrationWithName(slice, expected.Name)
    74  	assert.Equal(t, expected, *actual)
    75  }
    76  
    77  func TestReverter_getMigrationWithName_MultipleMigrationsWithoutExpected(t *testing.T) {
    78  	slice := []history.AppliedStorageHistoryObject{
    79  		{
    80  			SchemaVersion: "v1",
    81  			Applied:       common.JSONTime(time.Now()),
    82  			Hash:          "sample_hash",
    83  			Name:          "V1__unexpected.hcl",
    84  			Metadata: history.MetadataWrapper{
    85  				S3Metadata: nil,
    86  			},
    87  		},
    88  		{
    89  			SchemaVersion: "v1",
    90  			Applied:       common.JSONTime(time.Now()),
    91  			Hash:          "sample_hash",
    92  			Name:          "V2__unexpected.hcl",
    93  			Metadata: history.MetadataWrapper{
    94  				S3Metadata: nil,
    95  			},
    96  		},
    97  	}
    98  
    99  	actual := getMigrationWithName(slice, "V1__expected.hcl")
   100  	assert.Nil(t, actual)
   101  }
   102  
   103  func TestReverter_migrationsToRevert_(t *testing.T) {
   104  	baseTime := time.Now()
   105  	inclusive := history.AppliedStorageHistoryObject{
   106  		SchemaVersion: "v1",
   107  		Applied:       common.JSONTime(baseTime.Add(1 * time.Hour)),
   108  		Hash:          "sample_hash",
   109  		Name:          "V2__include_second.hcl",
   110  		Metadata: history.MetadataWrapper{
   111  			S3Metadata: nil,
   112  		},
   113  	}
   114  	slice := []history.AppliedStorageHistoryObject{
   115  		{
   116  			SchemaVersion: "v1",
   117  			Applied:       common.JSONTime(baseTime.Add(2 * time.Hour)),
   118  			Hash:          "sample_hash",
   119  			Name:          "V3__include_first.hcl",
   120  			Metadata: history.MetadataWrapper{
   121  				S3Metadata: nil,
   122  			},
   123  		},
   124  		inclusive,
   125  		{
   126  			SchemaVersion: "v1",
   127  			Applied:       common.JSONTime(baseTime),
   128  			Hash:          "sample_hash",
   129  			Name:          "V1__unexpected.hcl",
   130  			Metadata: history.MetadataWrapper{
   131  				S3Metadata: nil,
   132  			},
   133  		},
   134  	}
   135  
   136  	expected := []history.AppliedStorageHistoryObject{
   137  		slice[0],
   138  		slice[1],
   139  	}
   140  
   141  	actual := migrationsToRevert(slice, inclusive)
   142  
   143  	assert.Equal(t, expected, actual)
   144  }