github.com/deso-protocol/core@v1.2.9/migrate/20210623152412_create_tables.go (about)

     1  package migrate
     2  
     3  import (
     4  	"github.com/go-pg/pg/v10/orm"
     5  	migrations "github.com/robinjoseph08/go-pg-migrations/v3"
     6  )
     7  
     8  func init() {
     9  	up := func(db orm.DB) error {
    10  		_, err := db.Exec(`
    11  			CREATE TABLE pg_chains (
    12                  name     TEXT  NOT NULL PRIMARY KEY,
    13  				tip_hash BYTEA NOT NULL
    14  			)
    15  		`)
    16  		if err != nil {
    17  			return err
    18  		}
    19  
    20  		_, err = db.Exec(`
    21  			CREATE TABLE pg_blocks (
    22  				hash              BYTEA PRIMARY KEY,
    23  				parent_hash       BYTEA,
    24  				height            BIGINT NOT NULL,
    25  				difficulty_target BYTEA  NOT NULL,
    26  				cum_work          BYTEA  NOT NULL,
    27  				status            TEXT   NOT NULL,
    28  				tx_merkle_root    BYTEA  NOT NULL,
    29  				timestamp         BIGINT NOT NULL,
    30  				nonce             BIGINT NOT NULL,
    31  				extra_nonce       BIGINT,
    32  				version           INT,
    33  				notified          BOOL NOT NULL
    34  			)
    35  		`)
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		_, err = db.Exec(`
    41  			CREATE TABLE pg_transactions (
    42  				hash       BYTEA PRIMARY KEY,
    43  				block_hash BYTEA NOT NULL,
    44  				type       SMALLINT NOT NULL,
    45  				public_key BYTEA,
    46  				extra_data JSONB,
    47  				r          BYTEA,
    48  				s          BYTEA
    49  			);
    50  		`)
    51  		if err != nil {
    52  			return err
    53  		}
    54  
    55  		_, err = db.Exec(`
    56  			CREATE TABLE pg_transaction_outputs (
    57  				output_hash  BYTEA    NOT NULL,
    58  				output_index INT      NOT NULL,
    59  				output_type  SMALLINT NOT NULL,
    60  				height       BIGINT   NOT NULL,
    61  				public_key   BYTEA    NOT NULL,
    62  				amount_nanos BIGINT   NOT NULL,
    63  				spent        BOOL     NOT NULL,
    64  				input_hash   BYTEA,
    65  				input_index  INT,
    66  
    67  				PRIMARY KEY (output_hash, output_index)
    68  			);
    69  
    70  			CREATE INDEX pg_transaction_outputs_public_key ON pg_transaction_outputs(public_key);
    71  		`)
    72  		if err != nil {
    73  			return err
    74  		}
    75  
    76  		_, err = db.Exec(`
    77  			CREATE TABLE pg_metadata_block_rewards (
    78  				transaction_hash BYTEA PRIMARY KEY,
    79  				extra_data       BYTEA NOT NULL
    80  			);
    81  		`)
    82  		if err != nil {
    83  			return err
    84  		}
    85  
    86  		_, err = db.Exec(`
    87  			CREATE TABLE pg_metadata_bitcoin_exchanges (
    88  				transaction_hash    BYTEA PRIMARY KEY,
    89  				bitcoin_block_hash  BYTEA NOT NULL,
    90  				bitcoin_merkle_root BYTEA NOT NULL
    91  			);
    92  		`)
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		_, err = db.Exec(`
    98  			CREATE TABLE pg_metadata_private_messages (
    99  				transaction_hash     BYTEA PRIMARY KEY,
   100  				recipient_public_key BYTEA  NOT NULL,
   101  				encrypted_text       BYTEA  NOT NULL,
   102  				timestamp_nanos      BIGINT NOT NULL
   103  			);
   104  		`)
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		_, err = db.Exec(`
   110  			CREATE TABLE pg_metadata_submit_posts (
   111  				transaction_hash    BYTEA PRIMARY KEY,
   112  				post_hash_to_modify BYTEA  NOT NULL,
   113  				parent_stake_id     BYTEA  NOT NULL,
   114  				body                BYTEA  NOT NULL,
   115  				timestamp_nanos     BIGINT NOT NULL,
   116  				is_hidden           BOOL   NOT NULL
   117  			);
   118  		`)
   119  		if err != nil {
   120  			return err
   121  		}
   122  
   123  		_, err = db.Exec(`
   124  			CREATE TABLE pg_metadata_update_exchange_rates (
   125  				transaction_hash      BYTEA PRIMARY KEY,
   126  				usd_cents_per_bitcoin BIGINT NOT NULL
   127  			);
   128  		`)
   129  		if err != nil {
   130  			return err
   131  		}
   132  
   133  		_, err = db.Exec(`
   134  			CREATE TABLE pg_metadata_update_profiles (
   135  				transaction_hash         BYTEA PRIMARY KEY,
   136  				profile_public_key       BYTEA,
   137  				new_username             BYTEA,
   138  				new_description          BYTEA,
   139  				new_profile_pic          BYTEA,
   140  				new_creator_basis_points BIGINT NOT NULL
   141  			);
   142  		`)
   143  		if err != nil {
   144  			return err
   145  		}
   146  
   147  		_, err = db.Exec(`
   148  			CREATE TABLE pg_metadata_follows (
   149  				transaction_hash    BYTEA PRIMARY KEY,
   150  				followed_public_key BYTEA NOT NULL,
   151  				is_unfollow         BOOL NOT NULL
   152  			);
   153  		`)
   154  		if err != nil {
   155  			return err
   156  		}
   157  
   158  		_, err = db.Exec(`
   159  			CREATE TABLE pg_metadata_likes (
   160  				transaction_hash BYTEA PRIMARY KEY,
   161  				liked_post_hash  BYTEA NOT NULL,
   162  				is_unlike        BOOL NOT NULL
   163  			);
   164  		`)
   165  		if err != nil {
   166  			return err
   167  		}
   168  
   169  		_, err = db.Exec(`
   170  			CREATE TABLE pg_metadata_creator_coins (
   171  				transaction_hash                BYTEA PRIMARY KEY,
   172  				profile_public_key              BYTEA NOT NULL,
   173  				operation_type                  SMALLINT NOT NULL,
   174  				de_so_to_sell_nanos         BIGINT NOT NULL,
   175  				creator_coin_to_sell_nanos      BIGINT NOT NULL,
   176  				de_so_to_add_nanos          BIGINT NOT NULL,
   177  				min_de_so_expected_nanos    BIGINT NOT NULL,
   178  				min_creator_coin_expected_nanos BIGINT NOT NULL
   179  			);
   180  		`)
   181  		if err != nil {
   182  			return err
   183  		}
   184  
   185  		_, err = db.Exec(`
   186  			CREATE TABLE pg_metadata_swap_identities (
   187  				transaction_hash BYTEA PRIMARY KEY,
   188  				from_public_key  BYTEA NOT NULL,
   189  				to_public_key    BYTEA NOT NULL
   190  			);
   191  		`)
   192  		if err != nil {
   193  			return err
   194  		}
   195  
   196  		_, err = db.Exec(`
   197  			CREATE TABLE pg_metadata_creator_coin_transfers (
   198  				transaction_hash               BYTEA PRIMARY KEY,
   199  				profile_public_key             BYTEA NOT NULL,
   200  				creator_coin_to_transfer_nanos BIGINT NOT NULL,
   201  				receiver_public_key            BYTEA NOT NULL
   202  			);
   203  		`)
   204  		if err != nil {
   205  			return err
   206  		}
   207  
   208  		_, err = db.Exec(`
   209  			CREATE TABLE pg_metadata_create_nfts (
   210  				transaction_hash             BYTEA PRIMARY KEY,
   211  				nft_post_hash                BYTEA NOT NULL,
   212  				num_copies                   BIGINT NOT NULL,
   213  				has_unlockable               BOOL NOT NULL,
   214  				is_for_sale                  BOOL NOT NULL,
   215  				min_bid_amount_nanos         BIGINT NOT NULL,
   216  				creator_royalty_basis_points BIGINT NOT NULL,
   217  				coin_royalty_basis_points    BIGINT NOT NULL
   218  			);
   219  		`)
   220  		if err != nil {
   221  			return err
   222  		}
   223  
   224  		_, err = db.Exec(`
   225  			CREATE TABLE pg_metadata_update_nfts (
   226  				transaction_hash     BYTEA PRIMARY KEY,
   227  				nft_post_hash        BYTEA NOT NULL,
   228  				serial_number        BIGINT NOT NULL,
   229  				is_for_sale          BOOL NOT NULL,
   230  				min_bid_amount_nanos BIGINT NOT NULL
   231  			);
   232  		`)
   233  		if err != nil {
   234  			return err
   235  		}
   236  
   237  		_, err = db.Exec(`
   238  			CREATE TABLE pg_metadata_accept_nft_bids (
   239  				transaction_hash BYTEA PRIMARY KEY,
   240  				nft_post_hash    BYTEA NOT NULL,
   241  				serial_number    BIGINT NOT NULL,
   242  				bidder_pkid      BYTEA NOT NULL,
   243  				bid_amount_nanos BIGINT NOT NULL,
   244  				unlockable_text  BYTEA NOT NULL
   245  			);
   246  		`)
   247  		if err != nil {
   248  			return err
   249  		}
   250  
   251  		_, err = db.Exec(`
   252  			CREATE TABLE pg_metadata_bid_inputs (
   253  				transaction_hash BYTEA NOT NULL,
   254  				input_hash       BYTEA NOT NULL,
   255  				input_index      BIGINT NOT NULL,
   256  
   257  				PRIMARY KEY (transaction_hash, input_hash, input_index)
   258  			);
   259  		`)
   260  		if err != nil {
   261  			return err
   262  		}
   263  
   264  		_, err = db.Exec(`
   265  			CREATE TABLE pg_metadata_nft_bids (
   266  				transaction_hash BYTEA PRIMARY KEY,
   267  				nft_post_hash    BYTEA NOT NULL,
   268  				serial_number    BIGINT NOT NULL,
   269  				bid_amount_nanos BIGINT NOT NULL
   270  			);
   271  		`)
   272  		if err != nil {
   273  			return err
   274  		}
   275  
   276  		_, err = db.Exec(`
   277  			CREATE TABLE pg_notifications (
   278  				transaction_hash BYTEA PRIMARY KEY,
   279  				mined            BOOL NOT NULL,
   280  				to_user          BYTEA NOT NULL,
   281  				from_user        BYTEA NOT NULL,
   282  				other_user       BYTEA,
   283  				type             SMALLINT NOT NULL,
   284  				amount           BIGINT,
   285  				post_hash        BYTEA,
   286  				timestamp        BIGINT NOT NULL
   287  			);
   288  		`)
   289  		if err != nil {
   290  			return err
   291  		}
   292  
   293  		_, err = db.Exec(`
   294  			CREATE TABLE pg_profiles (
   295  				pkid                       BYTEA PRIMARY KEY,
   296  				public_key                 BYTEA NOT NULL,
   297                  username                   TEXT,
   298  				description                TEXT,
   299  				profile_pic                BYTEA,
   300  				creator_basis_points       BIGINT,
   301  				de_so_locked_nanos     BIGINT,
   302  				number_of_holders          BIGINT,
   303  				coins_in_circulation_nanos BIGINT,
   304  				coin_watermark_nanos       BIGINT
   305  			);
   306  
   307  			CREATE INDEX pg_profiles_public_key ON pg_profiles(public_key);
   308  			CREATE INDEX pg_profiles_username ON pg_profiles(username);
   309  			CREATE INDEX pg_profiles_lower_username ON pg_profiles(LOWER(username));
   310  		`)
   311  		if err != nil {
   312  			return err
   313  		}
   314  
   315  		_, err = db.Exec(`
   316  			CREATE TABLE pg_posts (
   317  				post_hash                    BYTEA PRIMARY KEY,
   318  				poster_public_key            BYTEA NOT NULL,
   319  				parent_post_hash             BYTEA,
   320                  body                         TEXT,
   321  				reposted_post_hash          BYTEA,
   322  				quoted_repost               BOOL,
   323  				timestamp                    BIGINT,
   324  				hidden                       BOOL,
   325  				like_count                   BIGINT,
   326  				repost_count                BIGINT,
   327  				quote_repost_count          BIGINT,
   328  				diamond_count                BIGINT,
   329  				comment_count                BIGINT,
   330  				pinned                       BOOL,
   331  				nft                          BOOL,
   332  				num_nft_copies               BIGINT,
   333  				unlockable                   BOOL,
   334  				creator_royalty_basis_points BIGINT,
   335  				coin_royalty_basis_points    BIGINT,
   336  				extra_data                   JSONB
   337  			);
   338  		`)
   339  		if err != nil {
   340  			return err
   341  		}
   342  
   343  		_, err = db.Exec(`
   344  			CREATE TABLE pg_likes (
   345  				liker_public_key BYTEA,
   346  				liked_post_hash  BYTEA,
   347  
   348  				PRIMARY KEY (liker_public_key, liked_post_hash)
   349  			);
   350  		`)
   351  		if err != nil {
   352  			return err
   353  		}
   354  
   355  		_, err = db.Exec(`
   356  			CREATE TABLE pg_follows (
   357  				follower_pkid BYTEA,
   358  				followed_pkid BYTEA,
   359  
   360  				PRIMARY KEY (follower_pkid, followed_pkid)
   361  			);
   362  		`)
   363  		if err != nil {
   364  			return err
   365  		}
   366  
   367  		_, err = db.Exec(`
   368  			CREATE TABLE pg_diamonds (
   369  				sender_pkid       BYTEA,
   370  				receiver_pkid     BYTEA,
   371  				diamond_post_hash BYTEA,
   372  				diamond_level     SMALLINT,
   373  
   374  				PRIMARY KEY (sender_pkid, receiver_pkid, diamond_post_hash)
   375  			);
   376  		`)
   377  		if err != nil {
   378  			return err
   379  		}
   380  
   381  		_, err = db.Exec(`
   382  			CREATE TABLE pg_messages (
   383  				message_hash         BYTEA PRIMARY KEY,
   384  				sender_public_key    BYTEA,
   385  				recipient_public_key BYTEA,
   386  				encrypted_text       BYTEA,
   387  				timestamp_nanos      BIGINT
   388  			);
   389  		`)
   390  		if err != nil {
   391  			return err
   392  		}
   393  
   394  		_, err = db.Exec(`
   395  			CREATE TABLE pg_creator_coin_balances (
   396  				holder_pkid   BYTEA,
   397  				creator_pkid  BYTEA,
   398  				balance_nanos BIGINT,
   399  				has_purchased BOOL,
   400  
   401  				PRIMARY KEY (holder_pkid, creator_pkid)
   402  			);
   403  		`)
   404  		if err != nil {
   405  			return err
   406  		}
   407  
   408  		_, err = db.Exec(`
   409  			CREATE TABLE pg_balances (
   410  				public_key    BYTEA PRIMARY KEY,
   411  				balance_nanos BIGINT
   412  			);
   413  		`)
   414  		if err != nil {
   415  			return err
   416  		}
   417  
   418  		_, err = db.Exec(`
   419  			CREATE TABLE pg_global_params (
   420  				id                           BIGSERIAL PRIMARY KEY,
   421  				usd_cents_per_bitcoin        BIGINT,
   422  				create_profile_fee_nanos     BIGINT,
   423  				create_nft_fee_nanos         BIGINT,
   424  				max_copies_per_nft           BIGINT,
   425  				min_network_fee_nanos_per_kb BIGINT
   426  			);
   427  		`)
   428  		if err != nil {
   429  			return err
   430  		}
   431  
   432  		_, err = db.Exec(`
   433  			CREATE TABLE pg_reposts (
   434  				reposter_public_key BYTEA,
   435  				reposted_post_hash  BYTEA,
   436  				repost_post_hash    BYTEA,
   437  
   438  				PRIMARY KEY (reposter_public_key, reposted_post_hash)
   439  			);
   440  		`)
   441  		if err != nil {
   442  			return err
   443  		}
   444  
   445  		_, err = db.Exec(`
   446  			CREATE TABLE pg_forbidden_keys (
   447  				public_key BYTEA PRIMARY KEY
   448  			);
   449  		`)
   450  		if err != nil {
   451  			return err
   452  		}
   453  
   454  		_, err = db.Exec(`
   455  			CREATE TABLE pg_nfts (
   456  				nft_post_hash                  BYTEA,
   457  				serial_number                  BIGINT,
   458  				last_owner_pkid                BYTEA,
   459  				owner_pkid                     BYTEA,
   460  				for_sale                       BOOL,
   461  				min_bid_amount_nanos           BIGINT,
   462  				unlockable_text                TEXT,
   463  				last_accepted_bid_amount_nanos BIGINT,
   464  
   465  				PRIMARY KEY (nft_post_hash, serial_number)
   466  			);
   467  		`)
   468  		if err != nil {
   469  			return err
   470  		}
   471  
   472  		_, err = db.Exec(`
   473  			CREATE TABLE pg_nft_bids (
   474  				bidder_pkid      BYTEA,
   475  				nft_post_hash    BYTEA,
   476  				serial_number    BIGINT,
   477  				bid_amount_nanos BIGINT,
   478  				accepted         BOOL,
   479  
   480  				PRIMARY KEY (bidder_pkid, nft_post_hash, serial_number)
   481  			);
   482  		`)
   483  		if err != nil {
   484  			return err
   485  		}
   486  
   487  		return nil
   488  	}
   489  
   490  	down := func(db orm.DB) error {
   491  		_, err := db.Exec(`
   492  			DROP TABLE pg_chains;
   493  			DROP TABLE pg_blocks;
   494  			DROP TABLE pg_transactions;
   495  			DROP TABLE pg_transaction_outputs;
   496  			DROP TABLE pg_metadata_block_rewards;
   497  			DROP TABLE pg_metadata_bitcoin_exchanges;
   498  			DROP TABLE pg_metadata_private_messages;
   499  			DROP TABLE pg_metadata_submit_posts;
   500  			DROP TABLE pg_metadata_update_exchange_rates;
   501  			DROP TABLE pg_metadata_update_profiles;
   502  			DROP TABLE pg_metadata_follows;
   503  			DROP TABLE pg_metadata_likes;
   504  			DROP TABLE pg_metadata_creator_coins;
   505  			DROP TABLE pg_metadata_swap_identities;
   506  			DROP TABLE pg_metadata_creator_coin_transfers;
   507  			DROP TABLE pg_metadata_create_nfts;
   508  			DROP TABLE pg_metadata_update_nfts;
   509  			DROP TABLE pg_metadata_accept_nft_bids;
   510  			DROP TABLE pg_metadata_bid_inputs;
   511  			DROP TABLE pg_metadata_nft_bids;
   512  			DROP TABLE pg_notifications;
   513  			DROP TABLE pg_profiles;
   514  			DROP TABLE pg_posts;
   515  			DROP TABLE pg_likes;
   516  			DROP TABLE pg_follows;
   517  			DROP TABLE pg_diamonds;
   518  			DROP TABLE pg_messages;
   519  			DROP TABLE pg_creator_coin_balances;
   520  			DROP TABLE pg_balances;
   521  			DROP TABLE pg_global_params;
   522  			DROP TABLE pg_reposts;
   523  			DROP TABLE pg_forbidden_keys;
   524  			DROP TABLE pg_nfts;
   525  			DROP TABLE pg_nft_bids;
   526  		`)
   527  		return err
   528  	}
   529  
   530  	opts := migrations.MigrationOptions{}
   531  
   532  	migrations.Register("20210623152412_create_tables", up, down, opts)
   533  }